How do I set the figure title and axes labels font size in Matplotlib

0 votes

I am creating a figure in Matplotlib like this:

from matplotlib import pyplot as plt

fig = plt.figure()
plt.plot(data)
fig.suptitle('test title')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
fig.savefig('test.jpg')

I want to specify font sizes for the figure title and the axis labels. I need all three to be different font sizes, so setting a global font size (mpl.rcParams['font.size']=x) is not what I want. How do I set font sizes for the figure title and the axis labels individually?

Jan 4, 2021 in Python by Roshni
• 10,480 points
8,942 views

2 answers to this question.

0 votes

Functions dealing with text like label, title, etc. accept parameters same as matplotlib.text.Text. For the font size you can use size/fontsize:

from matplotlib import pyplot as plt    

fig = plt.figure()
plt.plot(data)
fig.suptitle('test title', fontsize=20)
plt.xlabel('xlabel', fontsize=18)
plt.ylabel('ylabel', fontsize=16)
fig.savefig('test.jpg')

For globally setting title and label sizes, mpl.rcParams contains axes.titlesize and axes.labelsize. (From the page):

axes.titlesize      : large   # fontsize of the axes title
axes.labelsize      : medium  # fontsize of the x any y labels

(As far as I can see, there is no way to set x and y label sizes separately.)

And I see that axes.titlesize does not affect suptitle. I guess, you need to set that manually.

answered Jan 4, 2021 by Gitika
• 65,770 points
0 votes

Setting the font size of the figure title and axis labels in a Matplotlib graph specifies the size of the text in the graph's title and axis labels.

When defining the labels of the graph using matplotlib.pyplot.xlabel(xlabel), matplotlib.pyplot.ylabel(ylabel), and matplotlib.pyplot.suptitle(t), add the parameter fontsize=int to each function call with int as the desired font size.

plt.figure()
x = [1,2]
y = [1,2]
plt.plot(x,y)
plt.xlabel('x-axis', fontsize=20)

specify font sizes

plt.ylabel('y-axis', fontsize=20)
plt.suptitle('Graph Title', fontsize=30)
answered Jan 4, 2021 by Nikita

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How do I run a set of code for all the requests in Flask?

Hi, You can use dedicated hooks(decorators) called before ...READ MORE

answered Jun 21, 2019 in Python by Shabnam
• 930 points
822 views
–1 vote
2 answers
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,356 views
0 votes
2 answers

How to change the font size on a matplotlib plot?

Functions dealing with text like label, title, etc. accept ...READ MORE

answered Jan 4, 2021 in Python by Carlos
4,322 views
+1 vote
1 answer

How to create plots using python matplotlib in IPython notebook?

I think you should try: I used %matplotlib inline in ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 58,020 points
1,603 views
+1 vote
1 answer

How to handle Real-Time Matplotlib Plotting

To draw a continuous set of random ...READ MORE

answered Sep 26, 2018 in Python by Priyaj
• 58,020 points
16,428 views
+1 vote
4 answers

How do i resolve the "unexpected indent" error in python?

Look for the whitespaces which are not ...READ MORE

answered Aug 2, 2019 in Python by Mohammad
• 3,230 points
137,531 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,770 points
2,400 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP