我试图从字典中创建一个堆栈,其中值是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轴上的索引的ai值,但是不知道如何将代码或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''
有什么建议吗?
发布于 2015-07-31 19:37:24
UPDATE --您所得到的错误是由于matplotlib对您从dict.values()获得的视图不满意。注意,这只是python3.x的一个问题,因为python2.x dict.values()返回一个列表。您可以通过将视图转换为普通列表来避免这个问题,所以list(dict.values())。
下面是使用一个matplotlib示例的dict,用于python2.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()发布于 2015-07-31 19:38:42
还不完全清楚你想要实现什么,但如果我理解你的话,这应该是可行的:
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())

https://stackoverflow.com/questions/31753621
复制相似问题