Skip to main content
 首页 » 编程设计

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

2024年11月01日10pengyingh

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