Configuring the font family#

You can explicitly set which font family is picked up, either by specifying family names of fonts installed on user's system, or generic-families (e.g., 'serif', 'sans-serif', 'monospace', 'fantasy' or 'cursive'), or a combination of both. (see Text properties and layout)

In the example below, we are overriding the default sans-serif generic family to include a specific (Tahoma) font. (Note that the best way to achieve this would simply be to prepend 'Tahoma' in '')

The default family is set with the rcparam, e.g.

rcParams[''] = 'sans-serif'

and for the you set a list of font styles to try to find in order:

rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans',
                               'Lucida Grande', 'Verdana']

The defaults are OS dependent and can be viewed with:

import matplotlib.pyplot as plt

DejaVu Sans
DejaVu Sans Mono

Choose default sans-serif font

def print_text(text):
    fig, ax = plt.subplots(figsize=(6, 1), facecolor="#eefade")
    ax.text(0.5, 0.5, text, ha='center', va='center', size=40)

plt.rcParams[""] = "sans-serif"
print_text("Hello World! 01")
font family rc

Choose sans-serif font and specify to it to "Nimbus Sans"

plt.rcParams[""] = "sans-serif"
plt.rcParams["font.sans-serif"] = ["Nimbus Sans"]
print_text("Hello World! 02")
font family rc

Choose default monospace font

plt.rcParams[""] = "monospace"
print_text("Hello World! 03")
font family rc

Choose monospace font and specify to it to "FreeMono"

plt.rcParams[""] = "monospace"
plt.rcParams["font.monospace"] = ["FreeMono"]
print_text("Hello World! 04")
font family rc

Gallery generated by Sphinx-Gallery