I am working with matplotlib and would like to change the keys in my legends to be squares instead of rectangles when I make, for example, bar plots. Is there a way to speci
Modifying handlelength
globally affects the width of other markers in the legend. That solution will therefore be incompatible with, e.g. a combination of point and line patches. Instead, you can just add a square point marker to the legend using Line2D
. You just have to set its associated line to zero-width:
rect1 = mlines.Line2D([], [], marker="s", markersize=30, linewidth=0, color="r")
rect2 = mlines.Line2D([], [], marker="s", markersize=30, linewidth=0, color="y")
ax.legend((rect1, rect2), ('Men', 'Women'))