Text Baselines Comparison

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will compare the text baselines computed for mathtext and usetex using Matplotlib library. We will create a plot that contains two subplots, one with mathtext and the other with usetex. Each subplot will display four test strings with different styles and positions.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/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/PythonStandardLibraryGroup(["`Python Standard Library`"]) 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`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-49019{{"`Text Baselines Comparison`"}} matplotlib/figures_axes -.-> lab-49019{{"`Text Baselines Comparison`"}} matplotlib/matplotlib_config -.-> lab-49019{{"`Text Baselines Comparison`"}} python/variables_data_types -.-> lab-49019{{"`Text Baselines Comparison`"}} python/booleans -.-> lab-49019{{"`Text Baselines Comparison`"}} python/for_loops -.-> lab-49019{{"`Text Baselines Comparison`"}} python/list_comprehensions -.-> lab-49019{{"`Text Baselines Comparison`"}} python/lists -.-> lab-49019{{"`Text Baselines Comparison`"}} python/tuples -.-> lab-49019{{"`Text Baselines Comparison`"}} python/dictionaries -.-> lab-49019{{"`Text Baselines Comparison`"}} python/sets -.-> lab-49019{{"`Text Baselines Comparison`"}} python/importing_modules -.-> lab-49019{{"`Text Baselines Comparison`"}} python/data_collections -.-> lab-49019{{"`Text Baselines Comparison`"}} python/data_visualization -.-> lab-49019{{"`Text Baselines Comparison`"}} python/build_in_functions -.-> lab-49019{{"`Text Baselines Comparison`"}} end

Import the necessary libraries

We need to import the matplotlib.pyplot library to create the plot.

import matplotlib.pyplot as plt

Set the Matplotlib font

We need to set the font to be used for Matplotlib text. We will use the Computer Modern font and set it as the default font for Matplotlib.

plt.rcParams.update({"mathtext.fontset": "cm", "mathtext.rm": "serif"})

Create the subplots

We will create a figure that contains two subplots, one with mathtext and the other with usetex. We will use the subplots() method to create the subplots.

fig, axs = plt.subplots(1, 2, figsize=(2 * 3, 6.5))

Add test strings to the plot

We will add four test strings to each subplot, each with a different style and position. We will use the text() method to add the text to the subplots.

test_strings = ["lg", r"$\frac{1}{2}\pi$", r"$p^{3^A}$", r"$p_{3_2}$"]
for ax, usetex in zip(axs, [False, True]):
    ax.axvline(0, color="r")
    for i, s in enumerate(test_strings):
        ax.axhline(i, color="r")
        ax.text(0., 3 - i, s,
                usetex=usetex,
                verticalalignment="baseline",
                size=50,
                bbox=dict(pad=0, ec="k", fc="none"))

Set the plot limits and labels

We will set the plot limits and labels to match the desired output.

for ax in axs:
    ax.set(xlim=(-0.1, 1.1), ylim=(-.8, 3.9), xticks=[], yticks=[])
    ax.set_title(f"usetex={ax.usetex}\n")

Display the plot

We will display the plot using the show() method.

plt.show()

Summary

In this lab, we learned how to compare the text baselines computed for mathtext and usetex using Matplotlib. We created a plot that contained two subplots, one with mathtext and the other with usetex. We added four test strings to each subplot, each with a different style and position. Finally, we displayed the plot to compare the text baselines.

Other Python Tutorials you may like