How to customize Matolpotlib using rcParams

In the realm of data visualization, Matplotlib stands as a robust and flexible tool, and at its core lies a powerful customization feature known as rcParams (short for “runtime configuration parameters”). rcParams represent a set of configurations that control the behavioral and visual elements of Matplotlib, allowing us to unleash our creativity in our plots. These settings act as global guidelines for Matplotlib, and we can customize them as per our needs.

How to customize rcParams

To customize rcParams in Matplotlib, we have two options: tweak them during runtime or make lasting modifications to the matplotlibrc file.

Method 1: Changing rcParams at runtime

We can dynamically adjust rcParams in our Python script using the plt.rcParams object. For instance, to alter the font style for text in our plots, we can make the following simple runtime modification:

import matplotlib as plt
plt.rcParams['font.style'] = 'oblique'
Change font syle by altering rc parameters

This single line of code will set the font style for all text elements in our subsequent plots to oblique.

Method 2: Modifying the matplotlibrc file

If we want to avoid making changes each time, we can consider tweaking the matplotlibrc file. This file acts as a configuration repository, housing Matplotlib’s default settings. If this file is modified, the customizations propagate across all Matplotlib plots.

The matplotlibrc file’s location varies depending on the system, but we can find it by running the following Python command:

import matplotlib
print(matplotlib.matplotlib_fname())
Finding the location of the matplotlibrc file

Once we’ve located the file, we open it in a text editor and search for lines corresponding to the settings we wish to modify. For example, to tweak the default font style, we can seek out the font.style command and adjust its value. Then, we save the file, and voila! The customizations will adorn all our future figures.

Useful customization options

Because we now have access to the core settings of the Matplotlib library, let’s explore some useful customization options we can use for our plotting needs.

The axes category

The axes category in Matplotlib’s rcParams allows us to fine tune the appearance and behavior of axes in the plots. This includes control over axis limits, label positions, gridlines, spinesThe main lines defining the boundaries of the axis., and the style and size of axis elements like ticksThe small lines on a spine at specific intervals. and labels. Here’s how we can modify the axes category:

plt.rcParams['axes.grid'] = 'True'
plt.rcParams['axes.grid.axis'] = 'both'

The figure category

The figure category in Matplotlib’s rcParams allows us to adjust the figure’s dimensions, resolution, background color, margins, labels, and the behavior of constrained layout. It also encompasses options for managing the visibility of frames and legends within figures. Here’s how we can modify the figure category:

plt.rcParams['figure.facecolor'] = 'blue'
plt.rcParams['figure.figsize'] = 'both'

The font category

The font category in Matplotlib’s rcParams allows us to configure fonts used in plots. We can define font families, styles, sizes, and weights for various text components, including titles, labels, and mathematical text. The fine-tuning of fonts is essential to achieve the desired clarity and readability in our figures. Here’s how we can modify the font category:

plt.rcParams['font.style'] = 'oblique'
plt.rcParams['font.weight'] = '300'

The grid category

The grid category in Matplotlib’s rcParams allows us to gain control over the appearance of gridlines in figures. Parameters such as color, style, width, and transparency of gridlines are at our disposal. We might even have different grid styles for the two axes. Here’s how we can modify the grid category:

plt.rcParams['grid.color'] = 'red'
plt.rcParams['grid.linewidth'] = 1.2

The legend category

Legends are useful when we need to draw multiple plots on a single figure. The legend category extends the power of customization to legends within the Matplotlib plots. We can dictate the legend’s location, frame properties, labels, marker sizes, and spacing. Here’s how we can modify the legend category:

plt.rcParams['legend.loc'] = 'bottom-left'
plt.rcParams['legend.shadow'] = 'True'

The lines category

The lines category in Matplotlib’s rcParams allows us to configure the style and appearance of lines in Matplotlib plots. Settings for line color, width, style (dashed, dotted, solid, etc.), markers, and marker attributes are available as rcParams. Here’s how we can modify the lines category:

plt.rcParams['lines.linewidth'] = 2
plt.rcParams['lines.linestyle'] = ':'

The mathtext category

The mathtext category in Matplotlib’s rcParams allows us to set fonts for various math text components, like bold, italic, sans-serif, and monospace. These configurations allow us to align our mathematical text style with our overall styling convention used in the text other than the figures. Here’s how we can modify the mathtext category:

plt.rcParams['mathtext.bf'] = 'sans:bold'
plt.rcParams['mathtext.tt'] = 'monospace'

The patch category

The patch category in Matplotlib’s rcParams allows us to customize geometric shapes, such as rectangles or ellipses, added to our plots as patches. We can control attributes such as edge color, face color, line width, and antialiasing, allowing for the creation of visually appealing annotations and shapes within our plots. Here’s how we can modify the patch category:

plt.rcParams['patch.edgecolor'] = 'black'
plt.rcParams['patch.antialiased'] = 'True'

The savefig category

The savefig category in Matplotlib’s rcParams comes in handy when we need to save the Matplotlib figures as external files. Here, we can define settings for the saved figure. These settings include file format, resolution, orientation transparency, edge color, and background color for the saved file. Here’s how we can modify the savefig category:

plt.rcParams['savefig.transparent'] = 'True'
plt.rcParams['savefig.format'] = 'pdf'

Try it yourself

Here, we’ve provided a playground with a sample plot to try different customization options by changing the rcParams. You can explore different options from the rc file and make the perfect theme for yourself!

import matplotlib.pyplot as plt
# Modifying rc parameters.
plt.rcParams['font.style'] = 'oblique'
plt.rcParams['lines.linestyle'] = ':'
plt.rcParams['grid.color'] = 'orange'
plt.rcParams['grid.linewidth'] = 0.3
# Defining x and y values.
x_values = [i for i in range(20)]
y_values = [(j**2)/2 + 1 for j in x_values]
# Plotting the values.
plt.plot(x_values, y_values)
# Setting title, x label, and y label.
plt.xlabel("x values")
plt.ylabel("y values")
plt.title(r"$y = \frac{x^2}{2} + 1$")
# Turning on the grid.
plt.grid()

Conclusion

Becoming proficient in the art of customizing Matplotlib through rcParams is a valuable skill for crafting visually appealing and coherent data visualizations. Whether it’s adjusting font sizes, altering marker styles, or refining gridline appearances, rcParams empowers us to tailor Matplotlib to our needs. With the knowledge of modifying rcParams at runtime or within the rc file, our plots will convey data effectively and harmonize with our project’s design and style requirements.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved