【发布时间】:2018-02-16 04:49:54
【问题描述】:
所以我试图从文件中读取一个列表,然后将其绘制为图表。
这是我的代码
with open('the list') as f:
listoflang = [tuple(map( str ,i.split())) for i in f]
print listoflang
import numpy as np
import matplotlib.pyplot as plt
top= listoflang
labels, ys = zip(*top)
xs = np.arange(len(labels))
width = 1
fig = plt.figure()
ax = fig.gca()
ax.bar(xs, ys, width, align='center')
ax.set_xticks(xs)
ax.set_xticklabels(labels)
ax.set_yticks(ys)
plt.savefig('graph.png')
listoflang 是这样的
[('mysql', 2), ('unix', 2), ('linux', 1), ('perl', 2), ('php', 4), ('java', 25 ), ('javascript(beginner)', 1), ('ruby', 2), ('html', 9), ('android', 1), ('css', 4), ('shell', 1), ('c/c++', 1), ('python', 29), ('javascript', 3), ('c#(beginner)', 1), ('jsp', 1), (' sql', 2), ('html&css', 1), ('c', 37), ('python(basics', 1), ('c++', 25), ('cpp', 5)]
但这会返回这个
> Traceback (most recent call last): File "graphmon.py", line 22, in
> <module>
> plt.savefig('graph.png') File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line
> 697, in savefig
> res = fig.savefig(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line
> 1573, in savefig
> self.canvas.print_figure(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py",
> line 2252, in print_figure
> **kwargs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py",
> line 545, in print_png
> FigureCanvasAgg.draw(self) File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py",
> line 464, in draw
> self.figure.draw(self.renderer) File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line
> 63, in draw_wrapper
> draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line
> 1144, in draw
> renderer, self, dsu, self.suppressComposite) File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line
> 139, in _draw_list_compositing_images
> a.draw(renderer) File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line
> 63, in draw_wrapper
> draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py",
> line 2426, in draw
> mimage._draw_list_compositing_images(renderer, self, dsu) File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line
> 139, in _draw_list_compositing_images
> a.draw(renderer) File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line
> 63, in draw_wrapper
> draw(artist, renderer, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/axis.py", line
> 1136, in draw
> ticks_to_draw = self._update_ticks(renderer) File "/usr/local/lib/python2.7/dist-packages/matplotlib/axis.py", line 969,
> in _update_ticks
> tick_tups = [t for t in self.iter_ticks()] File "/usr/local/lib/python2.7/dist-packages/matplotlib/axis.py", line 914,
> in iter_ticks
> self.major.formatter.set_locs(majorLocs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/ticker.py", line
> 644, in set_locs
> self._compute_offset() File "/usr/local/lib/python2.7/dist-packages/matplotlib/ticker.py", line
> 656, in _compute_offset
> locs = locs[(vmin <= locs) & (locs <= vmax)] IndexError: in the future, 0-d boolean arrays will be interpreted as a valid boolean
> index
我可能做错了什么? 提前致谢!!
【问题讨论】:
-
能否请您包括回溯?
-
如果我设置
top = [the values you posted for listoflang](在 python 3.6 上),您的代码运行良好。你用的是哪个版本的python? -
版本 Python 2.7.13 。有没有办法解决这个问题?
-
i.stack.imgur.com/AKzjA.png@roganjosh
-
我无法在 mpl 版本 2.0.2 的 Python 2.7.13 (Anaconda) 上复制它。我不知道它在抱怨什么 tbh... 似乎是它自己的源代码抛出错误
标签: python list numpy matplotlib tuples