Stix Fonts Demo

PythonPythonBeginner
Practice Now

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

Introduction

This is a step-by-step tutorial on how to use Matplotlib to plot mathematical equations and text using different fonts.

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/sets("`Sets`") 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-48961{{"`Stix Fonts Demo`"}} matplotlib/figures_axes -.-> lab-48961{{"`Stix Fonts Demo`"}} python/for_loops -.-> lab-48961{{"`Stix Fonts Demo`"}} python/lists -.-> lab-48961{{"`Stix Fonts Demo`"}} python/tuples -.-> lab-48961{{"`Stix Fonts Demo`"}} python/sets -.-> lab-48961{{"`Stix Fonts Demo`"}} python/importing_modules -.-> lab-48961{{"`Stix Fonts Demo`"}} python/data_visualization -.-> lab-48961{{"`Stix Fonts Demo`"}} python/build_in_functions -.-> lab-48961{{"`Stix Fonts Demo`"}} end

Install Matplotlib

To start, you need to have Matplotlib installed in your environment. You can do this by running the following command in your terminal or command prompt:

pip install matplotlib

Import Matplotlib and Define Text

In this step, we import Matplotlib and define the text that we will be plotting using different fonts.

import matplotlib.pyplot as plt

circle123 = "\N{CIRCLED DIGIT ONE}\N{CIRCLED DIGIT TWO}\N{CIRCLED DIGIT THREE}"

tests = [
    r'$%s\;\mathrm{%s}\;\mathbf{%s}$' % ((circle123,) * 3),
    r'$\mathsf{Sans \Omega}\;\mathrm{\mathsf{Sans \Omega}}\;'
    r'\mathbf{\mathsf{Sans \Omega}}$',
    r'$\mathtt{Monospace}$',
    r'$\mathcal{CALLIGRAPHIC}$',
    r'$\mathbb{Blackboard\;\pi}$',
    r'$\mathrm{\mathbb{Blackboard\;\pi}}$',
    r'$\mathbf{\mathbb{Blackboard\;\pi}}$',
    r'$\mathfrak{Fraktur}\;\mathbf{\mathfrak{Fraktur}}$',
    r'$\mathscr{Script}$',
]

Plot the Text

Now that we have defined the text, we can plot it using Matplotlib. In this step, we create a figure and add the text to it using the fig.text() method.

fig = plt.figure(figsize=(8, len(tests) + 2))
for i, s in enumerate(tests[::-1]):
    fig.text(0, (i + .5) / len(tests), s, fontsize=32)

plt.show()

Analyze the Output

After running the code, we should see the plotted text using different fonts. The output should look like this:

output

Summary

In this tutorial, we learned how to plot mathematical equations and text using different fonts in Matplotlib. We covered the steps to install Matplotlib, import it into our code, define the text, and plot it using fig.text().

Other Python Tutorials you may like