绘制每个键具有多个值的字典 [英] Plotting a dictionary with multiple values per key

查看:66
本文介绍了绘制每个键具有多个值的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本这样的字典:

1: ['4026', '4024', '1940', '2912', '2916], 2: ['3139', '2464'], 3:['212']...

对于几百个键,我想用键的y来绘制它们的x值,我尝试了这段代码,在下面给出了错误:

For a few hundred keys, I'd like to plot them with the key as y for its set of x values, I tried this bit of code which gives the error underneath:

for rank, structs in b.iteritems():
    y = b.keys()
    x = b.values()
    ax.plot(x, y, 'ro')
    plt.show()

ValueError: setting an array element with a sequence

我对如何进行操作有点茫然,因此将不胜感激!

I'm at a bit of a loss on how to proceed so any help would be greatly appreciated!

推荐答案

您需要手动构建X和Y列表:

You need to construct your list of Xs and Ys manually:

In [258]: b={1: ['4026', '4024', '1940', '2912', '2916'], 2: ['3139', '2464'], 3:['212']}

In [259]: xs, ys=zip(*((int(x), k) for k in b for x in b[k]))

In [260]: xs, ys
Out[260]: ((4026, 4024, 1940, 2912, 2916, 3139, 2464, 212), (1, 1, 1, 1, 1, 2, 2, 3))

In [261]: plt.plot(xs, ys, 'ro')
     ...: plt.show()

结果:

这篇关于绘制每个键具有多个值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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