-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Cannot change the color of markers in a legend #8807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Why do you want to do this? |
Does that matter? To be clear, I almost never use |
The reason for this is a bit obscure. There are actually 2 artists (and in general N) and some reason only the first artist makes it into the To control exactly what is in the legend I suggest having a look at https://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists Doing it this way you will also be able to hold onto a reference to the handles and you can change them however you wish. |
Thanks. Indeed, I was thinking about this and came up with several ways to demonstrate the complicated behavior of markers in a legend. Surely you can at least imagine why someone might want to increase the handle linewidth, or analogously, the marker size. import matplotlib.pyplot as plt
plt.plot([0,1],[0,0],'r', label='LH[0]')
plt.plot([0,.4],[-.1,-.1],'go', label='LH[1]')
plt.plot([0,.4],[-.2,-.2],'k^', label='LH[2]')
plt.plot([0,.4],[-.3,-.3],'bs', label='LH[3]')
plt.plot([0,.4],[-.4,-.4],'D', mfc='c', label='LH[4]')
plt.scatter([0,.4],[-.5,-.5],c='m', label='LH[5]')
leg = plt.legend(loc='right', labelspacing=2)
LH = leg.legendHandles
LH[0].set_linewidth(8) # Works
LH[1].set_color('y') # No change
LH[2].set_ms(15) # No change
LH[3].set_marker('*') # Stars added to left and right!
LH[3].set_color('r') # Stars now change color
LH[4].set_color('w') # No change
LH[5].set_alpha(0.5) # Well-behaved scatter |
Right, the artist in If you want complete control of the legend, use the proxy artist path. Unfortunately, to expose more of the automatic legend internals would require a pretty big over-haul of the legend generation code. This is a very long standing issue (for example, see #2532 ). |
I encoutered this issue recently. ax_legend = fig.add_subplot(g[3, 0])
ax_legend.axis('off')
handles_markers = []
markers_labels = []
for marker_name, marker_style in markers_style.items():
pts = plt.scatter([0], [0], marker=marker_style, c='black', label=marker_name)
handles_markers.append(pts)
markers_labels.append(marker_name)
pts.remove()
ax_legend.legend(handles_markers, markers_labels, loc='center', ncol=len(markers_labels), handlelength=1.5, handletextpad=.1) where |
Bug report
Bug summary
Markers colors in a legend cannot be changed if the markers were added via
plt.plot
. Apparently, however, they can be if marker was added viaplt.scatter().
Code for reproduction
Actual outcome

Expected outcome
I expected the marker labeled "plot" in the legend, which was added via
plt.plot
, to be blue.Matplotlib version
Matplotlib, Python, Jupyter, and everything installed directly from the Anaconda installer downloaded directly from the Continuum website.
Also occurs on an older linux machine that runs matplotlib version 1.3.1 which was most likely installed via pip3.
The text was updated successfully, but these errors were encountered: