【问题标题】:IndexError: in the future, 0-d boolean arrays will be interpreted as a valid boolean indexIndexError:将来,0-d 布尔数组将被解释为有效的布尔索引
【发布时间】: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 。有没有办法解决这个问题?
  • 我无法在 mpl 版本 2.0.2 的 Python 2.7.13 (Anaconda) 上复制它。我不知道它在抱怨什么 tbh... 似乎是它自己的源代码抛出错误

标签: python list numpy matplotlib tuples


【解决方案1】:

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

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

其中 mask 的维度与 ret 的第一个维度互补,并且 mask 和 zeroes 是具有相同维度的二进制数组(内部只有 0 和 1)。

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

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

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

当警告过滤器被配置为生成错误时,我得到更频繁 问题。当过滤器设置为“警告”时,错误消息也会发生变化。

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

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

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

【讨论】:

    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 2017-12-25
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    相关资源
    最近更新 更多