Completely custom legend in Matplotlib, Python

前端 未结 2 677
悲&欢浪女
悲&欢浪女 2021-01-02 17:18

I am using Matplotlib to basically draw a \'picture\', not for plotting data.

In the \'picture\' I use plt.annotate to label certain parts of the pictur

2条回答
  •  情歌与酒
    2021-01-02 17:26

    In most cases you probably also want to illustrate elements on the graphic with color in your custom legend. In this case I would simply use matplotlib's own functions, than you also do not need to write your own complex function.

    import matplotlib 
    
    red_line = matplotlib.lines.Line2D([], [], color='red',markersize=100, label='Blue line')
    
    
    blue_line = matplotlib.lines.Line2D([], [], color='blue', markersize=100, label='Green line')
    purple_line = matplotlib.lines.Line2D([], [], color='purple', markersize=100, label='Green line')
    
    handles = [blue_line,red_line, purple_line]
    labels = [h.get_label() for h in handles] 
    
    ax.legend(handles=handles, labels=labels)  
    plt.show()
    

提交回复
热议问题