Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the font size on a matplotlib plot

How does one change the font size for all elements (ticks, labels, title) on a matplotlib plot?

I know how to change the tick label sizes, this is done with:

import matplotlib  matplotlib.rc('xtick', labelsize=20)  matplotlib.rc('ytick', labelsize=20)  

But how does one change the rest?

like image 461
Herman Schaaf Avatar asked Oct 10 '10 10:10

Herman Schaaf


People also ask

How do I change the font size on a plot in Matplotlib?

Plot x data points using plot() method. To change the font size of the scale in matplotlib, we can use labelsize in the ticks_params()method. To display the figure, use show() method.

How do I change font size in Python graph?

Change Font Size Globally We'll want to set the font_size parameter to a new size. We can get to this parameter via rcParams['font. size'] . You have to set these before the plot() function call since if you try to apply them afterwards, no change will be made.

What is Matplotlib default font size?

Note: The default font size for all elements is 10.


2 Answers

From the matplotlib documentation,

font = {'family' : 'normal',         'weight' : 'bold',         'size'   : 22}  matplotlib.rc('font', **font) 

This sets the font of all items to the font specified by the kwargs object, font.

Alternatively, you could also use the rcParams update method as suggested in this answer:

matplotlib.rcParams.update({'font.size': 22}) 

or

import matplotlib.pyplot as plt plt.rcParams.update({'font.size': 22}) 

You can find a full list of available properties on the Customizing matplotlib page.

like image 189
Herman Schaaf Avatar answered Sep 21 '22 20:09

Herman Schaaf


If you are a control freak like me, you may want to explicitly set all your font sizes:

import matplotlib.pyplot as plt  SMALL_SIZE = 8 MEDIUM_SIZE = 10 BIGGER_SIZE = 12  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 

Note that you can also set the sizes calling the rc method on matplotlib:

import matplotlib  SMALL_SIZE = 8 matplotlib.rc('font', size=SMALL_SIZE) matplotlib.rc('axes', titlesize=SMALL_SIZE)  # and so on ... 
like image 25
Pedro M Duarte Avatar answered Sep 18 '22 20:09

Pedro M Duarte