Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 700
vasek1 Avatar asked Sep 16 '12 05:09

vasek1


People also ask

How do I change the axis font size in Matplotlib?

To change the font size of the scale in Matplotlib, we can use labelsize in the tick_params() method.


2 Answers

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.

like image 158
Avaris Avatar answered Sep 28 '22 12:09

Avaris


You can also do this globally via a rcParams dictionary:

import matplotlib.pylab as pylab params = {'legend.fontsize': 'x-large',           'figure.figsize': (15, 5),          'axes.labelsize': 'x-large',          'axes.titlesize':'x-large',          'xtick.labelsize':'x-large',          'ytick.labelsize':'x-large'} pylab.rcParams.update(params) 
like image 21
tsando Avatar answered Sep 28 '22 11:09

tsando