In this python tutorial, you will learn how to change the font size of all the elements on a matplotlib plot. matplotlib is a python library for the visualization of different types of data. Matplotlib Library is used in Machine learning and Data science-related projects.
Method No 1: Change the Font size of the Matplotlib plot using the plt.rc() function
The easiest way to change the font size of a matplotlib is to use the matplotlib.rc() function. You just have to create properties of a font in a JSON object and then pass it to the matplotlib.rc() function. The default font size is 10 for the font so you can change it to whatever size you would like.
Check out the following example.
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
x = np.arange(0.0, 50.0, 2.0)
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0
s = np.random.rand(*x.shape) * 800 + 500
plt.scatter(x, y, s, c="g", alpha=0.5, marker=r'$\clubsuit$',
label="Luck")
plt.xlabel("Leprechauns")
plt.ylabel("Gold")
plt.legend(loc='upper left')
# changing the complete style of the font
font = {'family' : 'DejaVu Sans',
'weight' : 'bold',
'size' : 27}
plt.rc('font', **font)
plt.show()
Output
This sets the font of all items to the font specified by the kwargs object, font.
Method No 2: Change Font size of Matplotlib plot using the plt.rcParams.update() function
This is another alternative way, which can help you change only the font size of the plot and leave others as it is. You can check out the following example to understand.
Example
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
x = np.arange(0.0, 50.0, 2.0)
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0
s = np.random.rand(*x.shape) * 800 + 500
plt.scatter(x, y, s, c="g", alpha=0.5, marker=r'$\clubsuit$',
label="Luck")
plt.xlabel("Leprechauns")
plt.ylabel("Gold")
plt.legend(loc='upper left')
# changing the size of the font
plt.rcParams.update({'font.size': 25})
plt.show()
Output
The output is the same as shown in the above picture.
Method No 3: Change the font size of each Element on the plot in Matplotlib
You can give diffent font size to diffent elem3ent on a matplotlib plot in python. matplotlib library gives you more control to change the size of each element individually.
import matplotlib.pyplot as plt
import numpy as np
# config for font size
SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
# Fixing random state for reproducibility
np.random.seed(19680801)
x = np.arange(0.0, 50.0, 2.0)
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0
s = np.random.rand(*x.shape) * 800 + 500
plt.scatter(x, y, s, c="g", alpha=0.5, marker=r'$\clubsuit$',
label="Luck")
plt.xlabel("Leprechauns")
plt.ylabel("Gold")
plt.legend(loc='upper left')
# changing the size of the font
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
plt.show()
Output
Method No 4: Change the font size of the plot that is already created
You can also change the font size when the plot is already created. This way you just update the font-size property of the matplotlib plot. To update the font size of a plot that is already created, use the set_fontsize() function.
Check the following example
import matplotlib.pyplot as plt
ax = plt.subplot(111, xlabel='x', ylabel='y', title='title')
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(20)
Output
Related Articles:
Have a Chat With Me!
I hope you liked this Python Tutorial💗. Now, Don't hesitate to leave a message at haxratali0@gamil.com. I respond to every mail I receive. I will be more than happy to chat with you.