Usetex Font Effects

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will walk you through how to apply font effects to your Matplotlib plots using the usetex mode. We will use a sample script that demonstrates font effects specified in the pdftex.map file. By the end of this tutorial, you will be able to create professional-looking plots with custom font effects.

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/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") 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-49020{{"`Usetex Font Effects`"}} matplotlib/figures_axes -.-> lab-49020{{"`Usetex Font Effects`"}} python/booleans -.-> lab-49020{{"`Usetex Font Effects`"}} python/for_loops -.-> lab-49020{{"`Usetex Font Effects`"}} python/lists -.-> lab-49020{{"`Usetex Font Effects`"}} python/tuples -.-> lab-49020{{"`Usetex Font Effects`"}} python/sets -.-> lab-49020{{"`Usetex Font Effects`"}} python/function_definition -.-> lab-49020{{"`Usetex Font Effects`"}} python/importing_modules -.-> lab-49020{{"`Usetex Font Effects`"}} python/data_visualization -.-> lab-49020{{"`Usetex Font Effects`"}} python/build_in_functions -.-> lab-49020{{"`Usetex Font Effects`"}} end

Install Matplotlib

Before we begin, we need to make sure that Matplotlib is installed. You can install it using pip:

pip install matplotlib

Import the Required Libraries

In this step, we will import the required libraries for this tutorial. We will be using the Matplotlib library to create our plot.

import matplotlib.pyplot as plt

Define the Font Function

In this step, we will define a function that sets the font. This function takes a font name as an argument and returns a string that sets the font to the specified name.

def setfont(font):
    return rf'\font\a {font} at 14pt\a '

Create the Plot

In this step, we will create the plot. We will use the fig.text() method to add text to the plot. We will iterate over a list of fonts and corresponding text, using the zip() function to match them up. We will set the usetex parameter to True to enable the usetex mode.

fig = plt.figure()
for y, font, text in zip(
    range(5),
    ['ptmr8r', 'ptmri8r', 'ptmro8r', 'ptmr8rn', 'ptmrr8re'],
    [f'Nimbus Roman No9 L {x}'
     for x in ['', 'Italics (real italics for comparison)',
               '(slanted)', '(condensed)', '(extended)']],
):
    fig.text(.1, 1 - (y + 1) / 6, setfont(font) + text, usetex=True)

fig.suptitle('Usetex font effects')
plt.show()

Interpret the Results

The script creates a plot that demonstrates font effects specified in the pdftex.map file. It shows how you can use different fonts and font styles to create custom text on your plot.

Summary

In this tutorial, we learned how to use the usetex mode in Matplotlib to apply custom font effects to our plots. We defined a function to set the font, and then used the fig.text() method to add text to our plot. We also iterated over a list of fonts and corresponding text to demonstrate different font effects. By following these steps, you can create professional-looking plots with custom fonts in Matplotlib.

Other Python Tutorials you may like