使用字典使用 matplotlib 绘制条形图 [英] Plot a bar using matplotlib using a dictionary
本文介绍了使用字典使用 matplotlib 绘制条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以使用 matplotlib
使用直接来自 dict 的数据绘制条形图?
我的字典看起来像这样:
D = {u'Label1':26, u'Label2': 17, u'Label3':30}
我期待
fig = plt.figure(figsize=(5.5,3),dpi=300)ax = fig.add_subplot(111)bar = ax.bar(D,range(1,len(D)+1,1),0.5)
工作,但它没有.
这里是错误:
<预><代码>>>>ax.bar(D,range(1,len(D)+1,1),0.5)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/usr/local/lib/python2.7/site-packages/matplotlib/axes.py",第4904行,条形self.add_patch(r)文件/usr/local/lib/python2.7/site-packages/matplotlib/axes.py",第 1570 行,在 add_patchself._update_patch_limits(p)文件/usr/local/lib/python2.7/site-packages/matplotlib/axes.py",第 1588 行,在 _update_patch_limitsxys = patch.get_patch_transform().transform(vertices)文件/usr/local/lib/python2.7/site-packages/matplotlib/patches.py",第 580 行,在 get_patch_transformself._update_patch_transform()_update_patch_transform 中的文件/usr/local/lib/python2.7/site-packages/matplotlib/patches.py",第 576 行bbox = transforms.Bbox.from_bounds(x, y, width, height)文件/usr/local/lib/python2.7/site-packages/matplotlib/transforms.py",第 786 行,在 from_boundsreturn Bbox.from_extents(x0, y0, x0 + width, y0 + height)类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到浮点数 解决方案
您可以通过先绘制条形图然后设置适当的刻度线来分两行完成:
将 matplotlib.pyplot 导入为 pltD = {u'Label1':26, u'Label2': 17, u'Label3':30}plt.bar(range(len(D)), list(D.values()), align='center')plt.xticks(范围(len(D)),列表(D.keys()))# # 对于 python 2.x:# plt.bar(range(len(D)), D.values(), align='center') # python 2.x# plt.xticks(range(len(D)), D.keys()) # 在 python 2.x 中plt.show()
注意python3中倒数第二行应该是plt.xticks(range(len(D)), list(D.keys()))
,因为D.keys()
返回一个生成器,matplotlib 不能直接使用.
Is there any way to plot a bar plot using matplotlib
using data directly from a dict?
My dict looks like this:
D = {u'Label1':26, u'Label2': 17, u'Label3':30}
I was expecting
fig = plt.figure(figsize=(5.5,3),dpi=300)
ax = fig.add_subplot(111)
bar = ax.bar(D,range(1,len(D)+1,1),0.5)
to work, but it does not.
Here is the error:
>>> ax.bar(D,range(1,len(D)+1,1),0.5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 4904, in bar
self.add_patch(r)
File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1570, in add_patch
self._update_patch_limits(p)
File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1588, in _update_patch_limits
xys = patch.get_patch_transform().transform(vertices)
File "/usr/local/lib/python2.7/site-packages/matplotlib/patches.py", line 580, in get_patch_transform
self._update_patch_transform()
File "/usr/local/lib/python2.7/site-packages/matplotlib/patches.py", line 576, in _update_patch_transform
bbox = transforms.Bbox.from_bounds(x, y, width, height)
File "/usr/local/lib/python2.7/site-packages/matplotlib/transforms.py", line 786, in from_bounds
return Bbox.from_extents(x0, y0, x0 + width, y0 + height)
TypeError: coercing to Unicode: need string or buffer, float found
解决方案
You can do it in two lines by first plotting the bar chart and then setting the appropriate ticks:
import matplotlib.pyplot as plt
D = {u'Label1':26, u'Label2': 17, u'Label3':30}
plt.bar(range(len(D)), list(D.values()), align='center')
plt.xticks(range(len(D)), list(D.keys()))
# # for python 2.x:
# plt.bar(range(len(D)), D.values(), align='center') # python 2.x
# plt.xticks(range(len(D)), D.keys()) # in python 2.x
plt.show()
Note that the penultimate line should read plt.xticks(range(len(D)), list(D.keys()))
in python3, because D.keys()
returns a generator, which matplotlib cannot use directly.
这篇关于使用字典使用 matplotlib 绘制条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文