Tutorial Example

Understand Matplotlib Fontdict: A Beginner Guide – Matplotlib Tutorial

Matplotlib fontdict can allow us to set the font style of text in a plot, how to use it to set font style? In this tutorial, we will introduce some tips on how to set values of fontdict for matplotlib beginners.

How to set fontdict

At beginning, we will introduce how to use fontdict in matplotlib application. Here is an example:

fontdict = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'size': 16,
        }
plt.title('Damped exponential decay', fontdict=font)

From code above, we can find fontdict is a python dictionary, it contains some key and value elements on font style.

Value of fontdict

To set fontdict correctly, we should know what key and value we should use. Here are some keys and values are often used in python script.

Key Value
family such as serif, fantasy, Tahoma, monospace,’Times New Roman’ et al. You should notice: you can set font-family or font in this key.
color darkred, red, black
weight ‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’
size 10, 12
style ‘normal’, ‘italic’, ‘oblique’

Then you can use these keys and values to set the font style of text in matplotlib. For example:

font = {'family': 'Arial',
        'color':  'darkred',
        'weight': 'normal',
        'size': 14,
        }
plt.title('Tutorial Example on Matplotlib', fontdict=font)

The effect likes:

You will find the font style of title is changed by us.