Có cách nào để vẽ đồ thị thanh sử dụng matplotlib
sử dụng dữ liệu trực tiếp từ dict không?python: vẽ một thanh bằng cách sử dụng matplotlib sử dụng từ điển
dict của tôi trông như thế này:
D = {u'Label1':26, u'Label2': 17, u'Label3':30}
tôi đã mong
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)
để làm việc, nhưng nó không.
Dưới đây là lỗi:
>>> 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
bạn có thể chia sẻ cụ thể những gì không hoạt động? Bạn có ngoại lệ không? Ngoại lệ là gì? Chia sẻ càng nhiều thông tin càng tốt. –
@InbarRose xin lỗi, tôi đã cập nhật các câu hỏi với lỗi nó cho thấy ... một cái gì đó liên quan đến chuỗi hoặc bộ đệm ... Tôi không hiểu thông báo lỗi này. – otmezger
Nó không rõ ràng những gì bạn muốn đạt được, nhưng trong 'ax.bar (D, phạm vi (1, len (D) +1,1), 0,5)' đối số đầu tiên phải là một danh sách các số, trong trường hợp của bạn ' D.values () '. – adrianp