Controlling Font Sizes in Matplotlib Plots

Matplotlib offers extensive customization options for creating publication-quality plots. A common requirement is to control the font sizes of various plot elements – the title, axis labels, and tick labels. This tutorial explains how to effectively adjust these font sizes to achieve the desired visual appearance.

Setting Font Sizes Individually

The most direct approach is to specify the fontsize argument when creating or modifying plot elements. This gives you granular control over each element’s appearance.

Here’s how you can set font sizes for the figure title (using suptitle), x-axis label, and y-axis label:

import matplotlib.pyplot as plt

fig = plt.figure()
plt.plot([1, 2, 3, 4], [5, 6, 7, 8])  # Example data

fig.suptitle('My Plot Title', fontsize=20)
plt.xlabel('X-axis Label', fontsize=18)
plt.ylabel('Y-axis Label', fontsize=16)

fig.savefig('my_plot.png')

In this example, fontsize is set to different values for each element, allowing for a customized look. The fontsize argument accepts either an absolute size in points (e.g., 12) or a relative size string (e.g., 'large', 'x-large').

Using rcParams for Global Control

While individual settings offer precise control, it’s often more efficient to define default font sizes globally using matplotlib.rcParams. This approach ensures consistency across multiple plots and simplifies customization.

You can update rcParams directly:

import matplotlib.pyplot as plt

plt.rcParams['axes.labelsize'] = 16
plt.rcParams['axes.titlesize'] = 18
plt.rcParams['figure.titlesize'] = 20 #For suptitle

fig = plt.figure()
plt.plot([1, 2, 3, 4], [5, 6, 7, 8])

fig.suptitle('My Plot Title')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')

fig.savefig('my_plot_global.png')

Here, we set default font sizes for axis labels, axis titles, and the figure title (used by suptitle). Any subsequent plots will inherit these settings unless explicitly overridden.

Alternatively, you can create a dictionary containing the desired rcParams and update them using plt.rcParams.update():

import matplotlib.pyplot as plt

params = {'axes.labelsize': 14,
          'axes.titlesize': 16,
          'figure.titlesize': 18}

plt.rcParams.update(params)

# Create plot here...

This approach is more organized and allows you to easily switch between different sets of plot customizations.

Customizing Tick Label Sizes

You can also control the size of tick labels on the axes:

import matplotlib.pyplot as plt

plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12

# Plot code...

Important Considerations:

  • Consistency: Maintain consistent font sizes throughout your plots and publications for a professional appearance.
  • Readability: Choose font sizes that are easily readable and appropriate for the intended audience and output medium (e.g., screen vs. print).
  • Overriding: Remember that you can always override global rcParams settings by explicitly specifying the fontsize argument when creating or modifying plot elements.

By mastering these techniques, you can create visually appealing and informative Matplotlib plots with precisely controlled font sizes.

Leave a Reply

Your email address will not be published. Required fields are marked *