Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyPlot legend: 'Poly3DCollection' object has no attribute '_edgecolors2d'

The following code snippet works fine until I uncomment the plt.legend() line:

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()

I get the following error: 'Poly3DCollection' object has no attribute '_edgecolors2d'

I thought the cause may have been that I had played around with the framealpha and frameon parameters of plt.legend() in 2d plots, but I restarted the runtime (I'm working in a Google Colab Jupyter Notebook), clearing all variables, and the problem persisted.

What might be causing this error?

like image 583
Jacob Stern Avatar asked Mar 05 '19 02:03

Jacob Stern


2 Answers

Hi I found that is a bug still the library developers are trying to figure out it. I have found the following thread about the issue in git

Their suggestion they have given is to get the plotting

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

If the matplotlib version is matplotlib 3.3.3 try below

surf._facecolors2d = surf._facecolor3d
surf._edgecolors2d = surf._edgecolor3d
like image 118
AmilaMGunawardana Avatar answered Oct 23 '22 06:10

AmilaMGunawardana


Update to @AmilaMGunawardana's answer

As of matplotlib 3.3.3, _facecolors3d and _edgecolors3d do not exist. So, instead of this:

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

that would lead to a similar AttributeError, try this:

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

I had to make this an answer, instead of a comment, due to low rep.

like image 9
P_0 Avatar answered Oct 23 '22 05:10

P_0