极坐标图:TypeError:“numpy.float64”对象不能被解释为索引

Posted

技术标签:

【中文标题】极坐标图:TypeError:“numpy.float64”对象不能被解释为索引【英文标题】:Polar Plot: TypeError: 'numpy.float64' object cannot be interpreted as an index 【发布时间】:2021-03-03 05:58:44 【问题描述】:

我怀疑这个问题的答案是“不要使用 Matplotlib 极坐标图”。

我正在尝试生成极坐标图similar to this example plot 并将图表写入文件。

"""
Demo of a line plot on a polar axis.
"""
import numpy as np
import matplotlib.pyplot as plt


r = np.arange(0, 3.0, 0.01)
theta = 2 * np.pi * r

ax = plt.subplot(111, polar=True)
ax.plot(theta, r, color='r', linewidth=3)
ax.set_rmax(2.0)
ax.grid(True)

ax.set_title("A line plot on a polar axis", va='bottom')
plt.savefig("/path/to/file/plot.png")

当使用show() 时,图形将被绘制,并且在shell 中出现回溯。使用savefig() 时,回溯会导致savefig() 操作在未完成的情况下退出——因此不会写入文件。这似乎是matplotlib 内部的错误,仅将脚本转换为删除numpy 是不够的,因为savefig() 正在使用numpy 进行极坐标绘图。 此示例在早期版本的 Mac OS 上运行良好(可能是早期版本的 numpy )。

我已经尝试过什么

捕获TypeError。 将rtheta 转换为标准元组并从脚本中删除numpy

我不能尝试的事情

从 Matplotlib show() 视图窗口保存无花果(必须使用 savefig())。 升级 Python 升级 Matplotlib 降级 numpy Mac OS 降级

我运气不好?

Mac OS 10.15.7 Python 2.7 Matplotlib 1.3.1 numpy 1.16.6

更新: 上面的脚本在不同的机器上运行良好,这个版本的 numpy:Mac OS 10.15.7 Python 2.7 Matplotlib 1.3.1 numpy 1.8.0rc1

更新 2: 这是所有冗长的追溯。

Traceback (most recent call last):
  File "untitled text 7", line 20, in <module>
    plt.savefig('/Path/To/File/plot.png')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backend_bases.py", line 1962, in print_png
    return agg.print_png(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backends/backend_agg.py", line 505, in print_png
    FigureCanvasAgg.draw(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axis.py", line 1096, in draw
    tick.draw(renderer)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axis.py", line 234, in draw
    self.gridline.draw(renderer)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/lines.py", line 550, in draw
    tpath, affine = transf_path.get_transformed_path_and_affine()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 2562, in get_transformed_path_and_affine
    self._revalidate()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 2539, in _revalidate
    self._transform.transform_path_non_affine(self._path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 2230, in transform_path_non_affine
    self._a.transform_path(path))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 2227, in transform_path_non_affine
    return self._a.transform_path_non_affine(path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 2230, in transform_path_non_affine
    self._a.transform_path(path))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/projections/polar.py", line 72, in transform_path_non_affine
    ipath = path.interpolated(path._interpolation_steps)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/path.py", line 553, in interpolated
    vertices = simple_linear_interpolation(self.vertices, steps)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/cbook.py", line 1711, in simple_linear_interpolation
    result = np.zeros(new_shape, a.dtype)
TypeError: 'numpy.float64' object cannot be interpreted as an index

【问题讨论】:

numpy 的当前版本是 1.19,但正在放弃对 Py.2.7 的支持。 Matplotlib 是 3.3.2。所以你使用的是旧版本。 numpy 已收紧其索引规则。浮点数(例如 2.3)作为索引没有意义,但旧版本允许您使用 2.0 而不是 2。如果错误是由 matplotlib 中的草率引起的,唯一的解决方法可能是获得正确的版本组合。 我们通常要求提供完整的回溯,以便更好地了解问题所在。但这可能隐藏在 matplotlib 调用中,因此可能很难破译。 @hpaulj - 谢谢。不幸的是,这是一个项目要分布在广泛的机器上,这些机器运行的版本与描述的一样旧(甚至更旧)。将其复制到回溯中。它很长,但为了完整起见,我将继续添加它。 【参考方案1】:

在新的numpy 中,最后一个表达式会产生各种结果或错误:

In [108]: np.zeros(10)
Out[108]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [109]: np.zeros(10.)
Traceback (most recent call last):
  File "<ipython-input-109-6c3d535444cc>", line 1, in <module>
    np.zeros(10.)
TypeError: 'float' object cannot be interpreted as an integer

In [111]: np.zeros(np.array(10.))
Traceback (most recent call last):
  File "<ipython-input-111-4250f3d71116>", line 1, in <module>
    np.zeros(np.array(10.))
TypeError: only integer scalar arrays can be converted to a scalar index

In [112]: np.zeros(np.array([10.]))
Traceback (most recent call last):
  File "<ipython-input-112-e10bfdd4bb4f>", line 1, in <module>
    np.zeros(np.array([10.]))
TypeError: 'numpy.float64' object cannot be interpreted as an integer

【讨论】:

感谢调查。经过大量研究,似乎唯一的制胜法宝就是不玩。看来我得想出一个替代解决方案了。

以上是关于极坐标图:TypeError:“numpy.float64”对象不能被解释为索引的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:函数构建代码之外的操作正在传递一个图张量

TypeError:float() 参数必须是字符串或数字,而不是与散点图相关的“datetime.time”

创建图像的缩略图失败,并出现TypeError:'int'对象不可下标

MATLAB极坐标绘图 ( Polar Chart 极坐标图 | MATLAB 绘图问题 1 )

excel散点图纵坐标范围更改

在 matplotlib 中将一些值绘制到极坐标图