Change Font Size in Matplotlib
Matplotlib library mainly used to create 2-dimensional graphs and plots. It has a module named pyplot which makes things easy for plotting. To change the font size in Matplotlib, the two methods given below can be used with appropriate parameters:
Method 1: matplotlib.rcParams.update()
rcParams is an instance of matplotlib library for handling default matplotlib values hence to change default the font size we just have to pass value to the key font.size
Approach:
- Import module
- Create data
- Set rcParams.update() with value to the font.size key.
- Normally plot the data
- Display plot
Example:
Python3
import matplotlib.pyplot as plt # setting font sizeto 30plt.rcParams.update({'font.size': 30}) x = [1, 2, 3, 4, 5, 6]y = [0, 2, 4, 6, 8, 10] # plotting a plotplt.plot(x, y) # setting title nameplt.title("Title") # setting x axis labelplt.xlabel("x axis") # setting y axis labelplt.ylabel("y axis") plt.show() |
Output:
Method 2: matplotlib.rc(group, **kwargs):
This function sets the current rc params. The group is the grouping for the rc like line, font, axes etc and kwargs is a dictionary attribute name and value pairs.
Approach:
- Import module
- Create data
- Create a font dictionary to pass value of the font
- Supply this font value to rc()
- Normally plot the data
- Display data
Example:
Python3
import matplotlib.pyplot as plt # creating a dictionaryfont = {'size': 10} # using rc functionplt.rc('font', **font) x = [1, 2, 3, 4, 5, 6]y = [0, 2, 4, 6, 8, 10] # plotting a plotplt.plot(x, y) # setting title nameplt.title("Title") # setting x axis labelplt.xlabel("x axis") # setting y axis labelplt.ylabel("y axis") plt.show() |
Output:


