Explain Codes LogoExplain Codes Logo

How to change the font size on a matplotlib plot

python
matplotlib
font-properties
responsive-design
Alex KataevbyAlex Kataev·Nov 3, 2024
TLDR

No time to lose? Let's adjust the matplotlib text sizes straight away! Globally fix it using rcParams, and BOOM, you're done:

plt.rcParams['font.size'] = 22 # All text will now be Ultra HD

Want to tweak further? fontsize has got your back:

plt.title('Title', fontsize=18) # The title, now available in IMAX 3D

Voila! All text sizes on your plot are under your control.

Set and forget - Global font control

The magic rc spell

Uniformity is king when it comes to styling your plots. Cast this mighty spell and command the kingdom of plot aesthetics:

import matplotlib.pyplot as plt plt.rc('font', size=16) # Text elements bow down to size 16 plt.rc('axes', titlesize=18) # Titles demand respect, so we go bigger plt.rc('axes', labelsize=14) # Axes labels, however, prefer to lay low

(Bet you didn't know: rc stands for "run commands." So, you're literally commanding your plots with this.)

Custom text properties

Need to dive into the details? FontProperties - your friendly, neighborhood customizer:

from matplotlib.font_manager import FontProperties prop_cycle = FontProperties(family='serif', size=12) plt.legend(prop=prop_cycle) # Legends will rise again, now in serif fonts!

Pro tip: Maintain a FontProperties object for ease of reusing customized plot properties.

Flex your style - Create responsive size variables

Got a dynamic project on your hands? matplotlib offers responsive sizing, akin to CSS's em and rem:

SMALL_SIZE = 8 # The "em" of Matplotlib plt.rc('font', size=SMALL_SIZE) # All text adjusts to that value

Take comfort, knowing font sizes now respond to your every whim!

Fonts compatibility & rc considerations

.otf fonts? Yeah, we got 'em! Remember though, if you're being very rc-happy, the changes stay for the entire runtime. So, reset defaults with plt.rcdefaults() before your creative spree goes haywire.

Advanced plotting – Choose your own adventure!

Unique style dictionaries

Become an art curator. Paint your plot with individual elements styled to perfection:

title_style = {'family': 'serif', 'color': 'darkred', 'weight': 'bold', 'size': 16} plt.title('Title that demands respect', **title_style) # Bold and beautiful! label_style = {'family': 'monospace', 'color': 'blue', 'size': 12} plt.xlabel('x-axis on a diet', **label_style) # Slim picking for the labels

Every element in its place. Each with their style – the harmony screams sophistication!

Harmony of size and style

Size isn't all, shape matters too! Blend size and style to make your point hit home:

plt.title('Juicy data', fontsize=20, fontweight='bold', color='navy')

This concoction not only changes the size but dresses the title for success.

Tick labels – The devils in the details

Tick labels may be inconspicuous, but they are aplenty! Control their décor with precision:

for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(10) label.set_color('magenta') # Because ticks are party animals! label.set_rotation(45) # They also love doing the twist!

Subtle changes that translate to massive readability improvements - now, that's a bargain!