How to Change the Font Size in Matplotlib Plots | Towards Data Science

Usually, you may have to tweak the default font size of figures created with matplotlib in order to ensure that all the elements are visualised properly and are easy to read. In today’s short guide we will discuss how to change the font size in plots generated using matplotlib library. Specifically, we will explore how to
change the font size globally (which means that it will then be applicable to all plots generated)
change the size for individual components such as axes, figure title, tick labels, etc.
and how to change the font size for a specific plot that has already been generated
First, let’s create an example plot using some dummy data that we’ll use it as a reference throughout this article in order to demonstrate a few concepts.
import matplotlib.pyplot as pltplt.plot([1, 3, 5, 7], color='red', label='1st Line')plt.plot([2, 4, 6, 8], color='blue', label='2nd Line')plt.suptitle('Example Figure')plt.xlabel('This is x-axis')plt.ylabel('This is y-axis')plt.legend()plt.show()
And the output Figure looks like the one shown below:
If you want to change the font size of all plots created as well as all components shown in each individual plot including titles, legend, axes-labels and so on, then you need to update the corresponding parameter in rcParams which is a dictionary containing numerous customisable properties.
import matplotlib.pyplot as plt# Option 1plt.rcParams['font.size'] = 18# Option 2plt.rcParams.update({'font.size': 18})
The output figure after updating font.size parameter is shown below:
As mentioned, this will have a global effect and will impact pretty much every component shown in the figure which in most of the cases this may not be what you actually want.
Now sometimes, you may want some components to have different font sizing than other components. For instance, you may need to adjust the font size of the figure title and legend differently. In this case, you have to specify the font size for each individual component by modifying the corresponding parameters as shown below.
import matplotlib.pyplot as plt# Set the default text font sizeplt.rc('font', size=16)# Set the axes title font sizeplt.rc('axes', titlesize=16)# Set the axes labels font sizeplt.rc('axes', labelsize=16)# Set the font size for x tick labelsplt.rc('xtick', labelsize=16)# Set the font size for y tick labelsplt.rc('ytick', labelsize=16)# Set the legend font sizeplt.rc('legend', fontsize=18)# Set the font size of the figure titleplt.rc('figure', titlesize=20)
The solution introduced earlier will be applicable to every figure generated. If you want to tweak the font size for an individual plot that has already been created then you may have to specify the font size for each individual component. Assuming that you have created a subplot then the following should do the trick:
import matplotlib.pyplot as pltax1 = plt.subplot(    111,     xlabel='This is x-axis',     ylabel='This is y-axis',     title='Example subplot')ax1.title.set_fontsize(18)ax1.xaxis.label.set_fontsize(18)ax1.yaxis.label.set_fontsize(18)map(lambda p: p.set_fontsize(18), ax1.get_xticklabels())map(lambda p: p.set_fontsize(18), ax1.get_yticklabels())plt.show()
In today’s short guide we discussed about a few ways for changing the font size in Python plots generated with matplotlib library. We explored how you can change the font size for each component and plot created as well as how to change the font size just for a specific figure.
Finally, we also discussed how one can tweak the font size of specific components. This is usually useful when say you want the figure title and legend have different font sizes.
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
gmyrianthous.medium.com
You may also like