Markers of legend legends matplotlib

I am making a legend about scatter using proxy executors (http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist) and trying to make round markers.

This is my code:

legend([Circle((0,0), fc='g')], ["Green Circle"]) 

But when I draw it, the legend does not have a circle and instead displays a rectangle ...

How do I get a legend with round markers?

+4
source share
2 answers

If all you need is a circular marker in the legend (as opposed to the strict use of proxy artists), then I suggest trying something like:

 line1 = Line2D(range(1), range(1), color="white", marker='o', markerfacecolor="red") line2 = Line2D(range(1), range(1), color="white", marker='o',markerfacecolor="green") line3 = Line2D(range(1), range(1), color="white", marker='o',markersize=5, markerfacecolor="slategray") line4 = Line2D(range(1), range(1), 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 circles of different colors and sizes, where the 2D line drawn is white (hence color="white" ). If you do not want the circles to fill, for example, set markeredgecolor="green" and markerfacecolor="white" .

Other settings

  • If you don’t have a white background or the line intersects with the grid in the background, color="white" will still show the line. linewidth=0 will completely hide the line.
  • Use the handletextpad keyword argument for plt.legend to reduce the space between the circle and the label. The value can also be negative, for example. handletextpad=-0.3 .
  • If you use seaborn palettes, you can set individual marker colors by specifying a palette. For instance. markerfacecolor=sns.color_palette("cubehelix", 3)[0]
+7
source

@punkkat. When patches are added (which is surrounded), the patch itself is not added to the legend, but a new Rectangle patch is created with exactly the same properties (color, hatching, alpha, etc.) of this patch. Therefore, regardless of the input form, the result will be a square patch in the legend.

A simple solution given by @cosmosis is to pass the string instances to the legend, which can support arbitrary markers, and since there are no restrictions on the shape of the marker, there are no restrictions on the forms that you can go to the legend.

+2
source

All Articles