以下代码片段工作正常,直到我取消注释 plt.legend()
行:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-1, 1)
y = np.linspace(-1, 1)
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X**2 * Y)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, label='h=0')
ax.plot(np.zeros_like(y), y, np.zeros_like(y), label='singular points')
# plt.legend()
plt.show()
我收到以下错误:
“Poly3DCollection”对象没有属性“_edgecolors2d”
我认为原因可能是我在二维图中使用了 plt.legend() 的 framealpha
和 frameon
参数,但我重新启动了运行时(我正在使用 Google Colab Jupyter Notebook),清除所有变量,问题仍然存在。
什么可能导致此错误?
最佳答案
嗨,我发现这是一个错误,库开发人员仍在尝试解决它。 我在 git 中找到了有关该问题的以下线程
他们给出的建议是绘制图
surf = ax.plot_surface(X, Y, Z, label='h=0')
surf._facecolors2d=surf._facecolors3d
surf._edgecolors2d=surf._edgecolors3d
如果 matplotlib 版本是 matplotlib 3.3.3 请尝试以下
surf._facecolors2d = surf._facecolor3d
surf._edgecolors2d = surf._edgecolor3d
关于python - PyPlot图例: 'Poly3DCollection' object has no attribute '_edgecolors2d' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54994600/