Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm trying to add a legend to a 3D plot
import matplotlib.pyplot as plt
import matplotlib.lines as lines
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams["figure.figsize"] = (12, 10)
fig = plt.figure(1)
plt.clf()
ax = Axes3D(fig,
rect = [0, 0, .95, 1],
elev = 48,
azim = 134,
plt.cla()
ax.scatter(df_labeled['frequency'], df_labeled['recency'], df_labeled['monetary'],
c = df_labeled['label'],
s = 200,
alpha = 0.5,
edgecolor = 'darkgrey',
label = df_labeled['label'].unique()
colors = ['blue', 'red', 'green']
ax.set_xlabel('Frequency',
fontsize = 16)
ax.set_ylabel('Recency',
fontsize = 16)
ax.set_zlabel('Monetary',
fontsize = 16)
ax.legend()
plt.show()
And I ended up getting this:
The legend is incorrectly displaying one color and in a list rather than 3 separate legends for each label. What am I doing wrong?
You can do that by plotting each color separately, then legend will have an entry for each plot you made. Here is a minimal example with 2 plots:
import matplotlib.pyplot as plt
from numpy.random import rand
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
n = 100
ax.scatter(rand(n), rand(n), rand(n), label='Plot 1')
ax.scatter(rand(n), rand(n), rand(n), label='Plot 2')
plt.legend()
plt.show()
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.