首页 > 解决方案 > 将 conda 更新到 4.9.2 后的 set_3d_properties 属性错误

问题描述

我正在尝试使用 matplotlibs 保存功能保存动画,但遇到了这样的错误并不断抛出 Numpy 错误。我去更新 conda 和附加库到版本 4.9.2,所有附加库都是最新版本。之后动画不再运行,并且我不断收到以下错误消息:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 224, in process
func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\animation.py", line 1259, in _on_resize
self._init_draw()
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\animation.py", line 1706, in _init_draw
self._drawn_artists = self._init_func()
File "c:\Users\matth\Google Drive\University\Year 3\Individual Project\Source Code\path-planning\genetic_algorithm_3d\genetic_algorithm_3D.py", line 111, in init
line.set_3d_properties([])
File "C:\ProgramData\Anaconda3\lib\site-packages\mpl_toolkits\mplot3d\art3d.py", line 143, in set_3d_properties
zs = np.broadcast_to(zs, xs.shape)

AttributeError: 'list' object has no attribute 'shape'

导致上述错误的代码的简化版本如下:

import matplotlib.pyplot as plt
import matplotlib.animation as ani
import mpl_toolkits.mplot3d.axes3d as p3

def init():
    line.set_data([], [])    
    line.set_3d_properties([])
    return line,


def animate(i, line, points):
    x = []
    y = []
    z = []
    for point in points[i]:
        x.append(point[0])
        y.append(point[1])
        z.append(point[2])

    line.set_data(x, y)
    line.set_3d_properties(z)
    return line,


start = [0, 1, 2]
goal = [10, 9, 6]
points = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

fig = plt.figure()
ax = p3.Axes3D(fig)

ax.plot([start[0]], [start[1]], [start[2]], 'gs')
ax.plot([goal[0]], [goal[1]],[goal[2]], 'ms')
line, = ax.plot([], [], [], lw=2, color="red")

# PLEASE NOTE that I do not intend for this simplified version of code to run correctly, just for the error to no longer exist
# hopefully this will at least help with understanding the issue
animation = ani.FuncAnimation(fig, animate, 50, init_func=init, fargs=(line, points), interval=500,   blit=True)

plt.show()

就像我提到的,这在我更新 conda 之前是有效的,但我不愿意只是“回滚”版本而不理解它为什么首先被破坏。

对此的任何帮助将不胜感激!

标签: pythonnumpymatplotlibconda

解决方案


因此,在查看了与 matplotlib 3.3.3 版相关的文档后https://matplotlib.org/3.3.3/api/_as_gen/mpl_toolkits.mplot3d.art3d.Line3D.html?highlight=set_3d_properties#mpl_toolkits.mplot3d.art3d.Line3D。 get_data_3d,正如 Matt Thompson 强调的那样,我注意到该方法set_data_3d基本上取代了我以前使用的方法,因此将 3 个空数组作为输入参数传递给方法,set_data_3d([], [], []),效果很好。

希望这可以帮助任何偶然发现这个线程的人!


推荐阅读