我如何在 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/