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?
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.
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.
Note: The default font size for all elements is 10.
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.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With