Customizing Text Font Properties

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to set font properties using keyword arguments in Matplotlib. We will explore different font families, styles, variants, weights, and sizes to customize the appearance of our text. We will use Matplotlib's fig.text() method to display the different font options.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48746{{"`Customizing Text Font Properties`"}} matplotlib/figures_axes -.-> lab-48746{{"`Customizing Text Font Properties`"}} python/for_loops -.-> lab-48746{{"`Customizing Text Font Properties`"}} python/lists -.-> lab-48746{{"`Customizing Text Font Properties`"}} python/tuples -.-> lab-48746{{"`Customizing Text Font Properties`"}} python/dictionaries -.-> lab-48746{{"`Customizing Text Font Properties`"}} python/importing_modules -.-> lab-48746{{"`Customizing Text Font Properties`"}} python/data_visualization -.-> lab-48746{{"`Customizing Text Font Properties`"}} python/build_in_functions -.-> lab-48746{{"`Customizing Text Font Properties`"}} end

Set up the environment

First, we need to import the necessary libraries and set up the environment by creating a new figure using plt.figure().

import matplotlib.pyplot as plt

fig = plt.figure()

Show font families

Next, we will display the different font families available in Matplotlib. We will use the fig.text() method to display each font family, with the family name as the text and the corresponding font family as a keyword argument.

alignment = {'horizontalalignment': 'center', 'verticalalignment': 'baseline'}
yp = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]

fig.text(0.1, 0.9, 'family', size='large', **alignment)
families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace']
for k, family in enumerate(families):
    fig.text(0.1, yp[k], family, family=family, **alignment)

Show font styles

Now, we will display the different font styles available in Matplotlib. We will use the fig.text() method to display each font style, with the style name as the text and the corresponding font style as a keyword argument.

fig.text(0.3, 0.9, 'style', **alignment)
styles = ['normal', 'italic', 'oblique']
for k, style in enumerate(styles):
    fig.text(0.3, yp[k], style, family='sans-serif', style=style, **alignment)

Show font variants

Next, we will display the different font variants available in Matplotlib. We will use the fig.text() method to display each font variant, with the variant name as the text and the corresponding font variant as a keyword argument.

fig.text(0.5, 0.9, 'variant', **alignment)
variants = ['normal', 'small-caps']
for k, variant in enumerate(variants):
    fig.text(0.5, yp[k], variant, family='serif', variant=variant, **alignment)

Show font weights

Now, we will display the different font weights available in Matplotlib. We will use the fig.text() method to display each font weight, with the weight name as the text and the corresponding font weight as a keyword argument.

fig.text(0.7, 0.9, 'weight', **alignment)
weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
for k, weight in enumerate(weights):
    fig.text(0.7, yp[k], weight, weight=weight, **alignment)

Show font sizes

Finally, we will display the different font sizes available in Matplotlib. We will use the fig.text() method to display each font size, with the size name as the text and the corresponding font size as a keyword argument.

fig.text(0.9, 0.9, 'size', **alignment)
sizes = [
    'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large']
for k, size in enumerate(sizes):
    fig.text(0.9, yp[k], size, size=size, **alignment)

Show bold italic

As a bonus, we can also display text with both bold and italic styles. We will use the fig.text() method to display the text with the appropriate style, weight, and size.

fig.text(0.3, 0.1, 'bold italic',
         style='italic', weight='bold', size='x-small', **alignment)
fig.text(0.3, 0.2, 'bold italic',
         style='italic', weight='bold', size='medium', **alignment)
fig.text(0.3, 0.3, 'bold italic',
         style='italic', weight='bold', size='x-large', **alignment)

Summary

In this lab, we learned how to set font properties using keyword arguments in Matplotlib. We explored different font families, styles, variants, weights, and sizes to customize the appearance of our text. We used the fig.text() method to display the different font options.

Other Python Tutorials you may like