【问题标题】:set_3d_properties Attribute Error after updating conda to 4.9.2将 conda 更新到 4.9.2 后的 set_3d_properties 属性错误
【发布时间】:2021-02-01 16:05:20
【问题描述】:

我正在尝试使用 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 之前就可以工作,但我不愿意只是“回滚”版本而不了解它为什么首先被破坏。

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

【问题讨论】:

    标签: python numpy matplotlib conda


    【解决方案1】:

    因此,在查看了与 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([], [], []) 完美运行。

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

    【讨论】:

      【解决方案2】:

      conda 本身的版本极不可能是问题的根源,重要的是实际软件包的版本。

      无论如何,错误是函数期望数组但找到列表的特征。这可能是matplotlib 中的一个实际错误,但最简单的更改可能是让您进行转换,例如

          ...
          line.set_data(np.asarray(x), np.asarray(y))
          line.set_3d_properties(np.asarray(z))
          ...
      

      【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2019-08-23
      • 1970-01-01
      • 2019-03-01
      • 2018-03-31
      • 2017-11-30
      • 2018-08-17
      • 2019-06-26
      相关资源
      最近更新 更多