PyPlot图例:'Poly3DCollection'对象没有属性'_edgecolors2d'。

20

直到取消注释 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' object has no attribute '_edgecolors2d'

我原以为可能是因为我在2D图中玩弄了plt.legend()的framealphaframeon参数,但我重新启动了运行时(我正在使用Google Colab Jupyter笔记本),清除了所有变量,问题仍然存在。

这个错误的可能原因是什么?

5个回答

25

您好,我发现这是一个漏洞,库的开发人员仍在努力解决它。 我在git上找到了以下关于这个问题的讨论线程:git

他们给出的建议是获得绘图。

surf = ax.plot_surface(X, Y, Z, label='h=0')
surf._facecolors2d=surf._facecolors3d
surf._edgecolors2d=surf._edgecolors3d

如果matplotlib版本是3.3.3,请尝试以下方法

surf._facecolors2d = surf._facecolor3d
surf._edgecolors2d = surf._edgecolor3d

9

更新@AmilaMGunawardana的回答

matplotlib 3.3.3版本开始,_facecolors3d_edgecolors3d已经不存在。所以,改为:

surf._facecolors2d = surf._facecolors3d
surf._edgecolors2d = surf._edgecolors3d

如果出现类似的AttributeError错误,请尝试以下方法:

surf._facecolors2d = surf._facecolor3d
surf._edgecolors2d = surf._edgecolor3d

由于声望不高,我必须将此作为答案而不是评论。


0
ax._facecolors2d = ax._facecolor

在Matplotlib 3.3.4中对我有效。


0
这对我有效:尝试将面和边从2D替换为3D。
plt.figure()
surf = ssr2D.T.plot.surface(label="ssr")
surf._facecolors2d  = surf._facecolor3d
surf._edgecolors2d  = surf._edgecolor3d
plt.legend()
plt.show()

0

我正在使用matplotlib 3.5,这个方法对我有效:

surf._facecolors2d = surf._facecolor3d
surf._edgecolors2d = surf._edgecolor3d 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接