Stackplot使用字典值列表(Python 3.x) [英] Stackplot using list of dictionary values (Python 3.x)

查看:387
本文介绍了Stackplot使用字典值列表(Python 3.x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a stackplot from a dictionary where the value is a list of floats between 0 and 1 and the index of the value in the list is the time (t1, t2, ...tn) of measurement. All keys have the same number of values. For example:

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和a [5] = 0.15 ,等等在值列表的其他指标。

so that at t2: a[1] = 0.5, a[2] = 0.6, and a[5] = 0.15, and so on at the other indices of the list of values.

我要像一个这里,x轴上的值列表的索引和y轴上的该索引的a [i]的值,但是无法弄清楚如何调整该代码或一个字典的 matplotlib示例

I'm going for a stackplot like the one here with the indices of the value list on the x-axis and the value of a[i] at that index on the y-axis , but can't figure out how to adapt that code or the matplotlib example to a dictionary.

Python版本: 3.4

错误(对于我的数据和玩具数据集):
TypeError:ufunc'isfinite'不支持输入类型,输入不能安全地强制为任何支持的类型

Error (for both my data and toy data set): 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())

UPDATE - The error you are getting is because matplotlib is somehow not happy with the view you are getting from dict.values(). Note that this is only a problem of python 3.x as for python 2.x dict.values() returns a list. You can simply avoid this problem by converting the view into a normal list, so list(dict.values()).

以下是 matplotlib示例,使用 dict ,为python 2.x和3.x工作:

Here is the matplotlib example using a dict, working for both python 2.x and 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()

这篇关于Stackplot使用字典值列表(Python 3.x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆