Table of Contents
Adding a title to a Matplotlib legend is a simple process that can be done by using the “title” function. This function allows the user to specify a title for the legend, which can help to provide context and clarify the information being presented in the plot. By using this function, the title will be displayed above the legend and can be customized with various font styles, sizes, and colors. This feature is useful for enhancing the overall appearance and readability of the plot, making it easier for the audience to understand the data being visualized. Overall, adding a title to a Matplotlib legend is a useful tool for creating professional and informative plots.
Add a Title to Matplotlib Legend (With Examples)
By default, legends in Matplotlib plots do not include a title.
However, you can use the following basic syntax to add a title to a legend:
plt.legend(title='this is my title')
The following example shows how to use this syntax in practice.
Example 1: Add Title to Matplotlib Legend
The following code shows how to create a Matplotlib plot with multiple lines and a legend:
import pandas as pd import matplotlib.pyplotas plt #create data df = pd.DataFrame({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8]}) #add lines to plot plt.plot(df['points'], label='Points') plt.plot(df['assists'], label='Assists') #add legend plt.legend()

Notice that the legend doesn’t have a title.
To add one, we can simply use the title argument within the plt.legend() function:
#add title to legend plt.legend(title='Metric')

To modify the font size of the legend title, use the title_fontsize argument:
Note: The default font size is 10.
#add title to legend with increased font size plt.legend(title='Metric', title_fontsize=25)

Notice that the font size of the legend is much larger now.
#add title to legend with increased title and label font size plt.legend(title='Metric', title_fontsize=25, fontsize=15)

Notice that the labels in the legend are much larger now.
Additional Resources
Cite this article
stats writer (2024). How can I add a title to a Matplotlib legend?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-title-to-a-matplotlib-legend/
stats writer. "How can I add a title to a Matplotlib legend?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-title-to-a-matplotlib-legend/.
stats writer. "How can I add a title to a Matplotlib legend?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-title-to-a-matplotlib-legend/.
stats writer (2024) 'How can I add a title to a Matplotlib legend?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-title-to-a-matplotlib-legend/.
[1] stats writer, "How can I add a title to a Matplotlib legend?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add a title to a Matplotlib legend?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
