python - 在python中的matplotlib中使用字典作为参数

标签 python matplotlib

我如何在 matplotlib 中使用字典键或值而不是元组

tuple=(1,2,3,4)
rects1 = ax.bar(ind, tuple, width, color='r')

但我想这样做

dic={'1':1,'2':2,'3':3,'4':4}
rects1 = ax.bar(ind, dic[key], width, color='r')

最佳答案

试试这个:

rects1 = ax.bar(ind, (dic[key] for key in dic.keys()), width, color='r')

甚至:

rects1 = ax.bar(ind, dic.values(), width, color='r')

根据 Amars 的评论,如果字典是这样的:

dic={'a':(1,2),'b':(2,2),'c':(3,2)}

并且您只想使用值 (1, 2, 3) 您需要创建一个有序集:

rects1 = ax.bar(ind, sorted(set(dic.values())), width, color='r')

关于python - 在python中的matplotlib中使用字典作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20331502/

相关文章:

python - 将 Jython 与 Django 一起使用?

python - 按 Pandas 分组计算同比增长

python - 如何裁剪具有正方形纵横比的 Axes3D 图?

Python:绘制时间戳数据框matplotlib

python - 如何使用 matplotlib 沿着指定半径的点 (x1,y1) 和 (x2,y2) 的长度绘制圆柱体?

python - 类和函数范围

python - 堆叠矩阵的最佳方式

python - 向子进程发送 'ESC' 或信号

python - 如何在matplotlib中变换坐标轴

python - Matplotlib,关闭循环的按钮,python