Table of Contents
In order to change the font size of a legend in Matplotlib, you can use the “prop” parameter of the legend function. This parameter allows you to set the font size of the legend text. You can also set other properties such as font weight, font family, and font style. To do this, you must set the prop parameter when you create the legend object. Once you have set the properties, the legend object will be updated with the new font size.
You can easily add a plot to a Matplotlib plot by using the following code:
import matplotlib.pyplot as plt #add legend to plot plt.legend()
And you can easily change the font size of the text in the legend by using one of the following methods:
Method 1: Specify a Size in Numbers
You can specify font size by using a number:
plt.legend(fontsize=18)
Method 2: Specify a Size in Strings
You can also specify font size by using strings:
plt.legend(fontsize="small")
Options include:
- xx-small
- x-small
- small
- medium
- large
- x-large
- xx-large
The following examples show how to use each of these methods in practice:
Example 1: Specify Font Size Using a Number
The following example shows how to specify a legend font size using a number:
import matplotlib.pyplot as plt #create data plt.plot([2, 4, 6, 11], label="First Line") plt.plot([1, 4, 5, 9], label="Second Line") #add legend plt.legend(fontsize=18) #show plot plt.show()
Example 2: Specify Font Size Using a String
import matplotlib.pyplot as plt #create data plt.plot([2, 4, 6, 11], label="First Line") plt.plot([1, 4, 5, 9], label="Second Line") #add legend plt.legend(fontsize="small") #show plot plt.show()
How to Change Font Sizes on a Matplotlib Plot
How to Add Text to Matplotlib Plots