matplotlib: make legend keys square

前端 未结 3 708
一整个雨季
一整个雨季 2021-01-18 17:31

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

3条回答
  •  旧时难觅i
    2021-01-18 18:02

    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'))
    

提交回复
热议问题