IndexError:将来,0-d布尔数组将被解释为有效的布尔索引

时间:2017-09-07 12:34:15

标签: python list numpy matplotlib tuples

所以我试图从文件中读取一个列表,然后将其绘制成图形。

这是我的代码

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&# 39;,2),(' php',4),(' java',25),(' javascript(初学者)',1),( ' ruby​​',2),(' html',9),(' android',1),(' css',4) ,(' shell',1),(' c / c ++',1),(' python',29),(' javascript',3),(' c#(初学者)',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

我可能做错了什么? 在此先感谢!!

1 个答案:

答案 0 :(得分:-1)

我在Python 3.5.2和numpy 1.12.1上使用numpy数组和掩码的构造有类似的问题。

构造错误显示的位置: ret[ix,(mask==zeroes)] = 0

其中mask具有与ret的第一维互补的尺寸,而mask和0是具有相同尺寸的二进制数组(仅零和一些)。

警告过滤器配置为生成错误时

with warnings.catch_warnings():
warnings.simplefilter("error")  # "ignore" if not solved quickly

ret [ix,(mask == zeroes)] = 0

当警告过滤器配置为生成错误时,我会更频繁地使用 问题。当过滤器设置为&#34;警告&#34;时,错误消息也会改变。

with warnings.catch_warnings():
warnings.simplefilter("error")  # "ignore" if not solved quickly

ret[ix,(mask==zeroes)] = 0

我不确定正确的解决方法是什么。