Title for matplotlib legend
Add the title
parameter to the this line:
legend = plt.legend(handles=[one, two, three], title="title", loc=4, fontsize='small', fancybox=True)
See also the official docs for the legend
constructor.
Just to add to the accepted answer that this also works with an Axes object.
fig, ax = plt.subplots()ax.plot([0, 1, 2], [0, 1, 4], label='some_label') # Or however the Axes was created.ax.legend(title='This is My Legend Title')
In case you have an already created legend, you can modify its title with set_title()
. For the first answer:
legend = plt.legend(handles=[one, two, three], loc=4, fontsize='small', fancybox=True)legend.set_title("title") # plt.gca().get_legend().set_title() if you didn't store the# legend in an object or you're loading a saved figure.
For the second answer based on Axes:
fig, ax = plt.subplots()ax.plot([0, 1, 2], [0, 1, 4], label='some_label') # Or however the Axes was created.ax.legend()ax.get_legend().set_title("title")