Search code examples
pythonmatplotliblegend

How to add a title to a matplotlib legend


I have a figure with multiple legends and I would like to be able to label them. I would like to put a title/label Some label on the legend, as shown below. This is not a duplicate of this answer which only states how to place a legend on the figure.

I have checked the legend guide but can't find any info on how to do this.

enter image description here

Again, please don't close this question (as was this one) it's not a duplicate of the answer linked above. That answer does not give any information on adding a label/title to a legend.


Solution

  • As pointed out here, you can place a title with the title option in the legend command. For example:

    plt.plot([0,1], label='A')
    plt.legend(title='title')
    

    Or with axis instance:

    fig, ax = plt.subplots()
    ax.plot([0,1], label='A')
    ax.legend(title='title')
    

    Output:

    enter image description here