I'm making a legend for a scatter plot using a proxy artists (http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist) and attempting to make circle-shaped markers.
This is my code:
legend([Circle((0,0), fc='g')], ["Green Circle"])
But when I plot it, the legend doesn't have a circle and instead displays a rectangle...
How do I get the legend to have circle markers?
If all you are looking for is a circular marker in the legend (as opposed to strictly using proxy artists), then I suggest trying something like:
line1 = Line2D([], [], color="white", marker='o', markerfacecolor="red")
line2 = Line2D([], [], color="white", marker='o', markerfacecolor="green")
line3 = Line2D([], [], color="white", marker='o', markersize=5, markerfacecolor="slategray")
line4 = Line2D([], [], color="white", marker='o', markersize=10, markerfacecolor="slategray")
plt.legend((line1, line2, line3, line4), ('Thing 1', 'Thing 2', 'Thing 3', 'Thing 4'), numpoints=1, loc=1)
This shows you circles of different color and sizes, where the 2D line that is drawn is white (hence color="white"
). If you don't want the circles filled in, for example, set markeredgecolor="green"
and markerfacecolor="white"
.
Further tweaks
color="white"
will still show the line. linewidth=0
will completely hide the line.handletextpad
keyword argument for plt.legend
to reduce the space between the circle and the label. The value can also be negative, e.g. handletextpad=-0.3
.seaborn
palettes, you can set the individual marker face colors by indexing the palette. E.g. markerfacecolor=sns.color_palette("cubehelix", 3)[0]
@punkkat When adding patches (which Circle is) to be added to a legend, it is not the patch itself which is added, but a new Rectangle patch is created with exactly the same properties (color, hatching, alpha etc.) of the given patch. Hence, no matter the incoming shape, the result will be a square patch in the legend.
The simple solution, as given by @cosmosis, is to pass line instances to legend, which can support arbitrary markers, and since there is no limitation on the shape of a marker, there is no limitation on the shapes that you can pass to be put on the legend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With