Polar Plot: TypeError: 'numpy.float64' object cannot be interpreted as an index
Polar Plot: TypeError: 'numpy.float64' object cannot be interpreted as an index
我怀疑对此的答案是“不要使用 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
。
- 将
r
和 theta
转换为标准元组并从脚本中删除 numpy
。
我不能尝试的东西
- 从 Matplotlib
show()
视图中保存无花果 window(必须使用 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
中,最后一个表达式会产生各种结果或错误:
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
我怀疑对此的答案是“不要使用 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
。 - 将
r
和theta
转换为标准元组并从脚本中删除numpy
。
我不能尝试的东西
- 从 Matplotlib
show()
视图中保存无花果 window(必须使用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
中,最后一个表达式会产生各种结果或错误:
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