Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Change Font Size in Matplotlib

  • Last Updated : 03 Jan, 2021

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() 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 30
plt.rcParams.update({'font.size': 30})
  
x = [1, 2, 3, 4, 5, 6]
y = [0, 2, 4, 6, 8, 10]
  
# plotting a plot
plt.plot(x, y)
  
# setting title name
plt.title("Title")
  
# setting x axis label
plt.xlabel("x axis")
  
# setting y axis label
plt.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 dictionary
font = {'size': 10}
  
# using rc function
plt.rc('font', **font)
  
x = [1, 2, 3, 4, 5, 6]
y = [0, 2, 4, 6, 8, 10]
  
# plotting a plot
plt.plot(x, y)
  
# setting title name
plt.title("Title")
  
# setting x axis label
plt.xlabel("x axis")
  
# setting y axis label
plt.ylabel("y axis")
  
plt.show()

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!