繁体   English   中英

使用字典值列表的Stackplot(Python 3.x)

[英]Stackplot using list of dictionary values (Python 3.x)

我正在尝试从字典制作一个堆栈图,其中的值是0到1之间的浮点列表,列表中值的索引是测量的时间(t1,t2,... tn)。 所有键具有相同数量的值。 例如:

a = {1:[0.3,0.5,0.7], 2:[0.4,0.6,0.8], 5:[0.1,0.15,0.20]}

因此在t2处: a[1] = 0.5, a[2] = 0.6, and a[5] = 0.15 ,依此类推,在值列表的其他索引处。

我要在这里使用类似的堆栈图,在x轴上使用值列表的索引,在y轴上使用索引的a [i]的值,但无法弄清楚该如何适应代码或matplotlib示例转换为字典。

Python版本: 3.4

错误(针对我的数据和玩具数据集): TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

建议?

更新 -您遇到的错误是因为matplotlib对从dict.values()获取的视图不满意。 请注意,这只是python 3.x的一个问题,就像python 2.x一样dict.values()返回一个列表。 您可以通过将视图转换为普通列表来简单地避免此问题,因此可以使用list(dict.values())

这是使用dictmatplotlib示例 ,适用于python 2.x和3.x:

import numpy as np
from matplotlib import pyplot as plt

fnx = lambda : np.random.randint(5, 50, 10).astype(np.float64)
d = {i: v for i, v in enumerate(np.row_stack((fnx(), fnx(), fnx())))}
# d looks basically like your a
x = range(len(d[0]))
y = list(d.values()) # d.values() returns a view in python 3.x
fig, ax = plt.subplots()
ax.stackplot(x, y)
plt.show()

尚不清楚您想要实现什么,但是如果我理解您的话,这应该可以工作:

import matplotlib.pyplot as plt
a = {1:[0.3,0.5,0.7], 2:[0.4,0.6,0.8], 5:[0.1,0.15,0.20]}
plt.stackplot(a.keys(),a.values())

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM