mailwal

Using LaTeX in Matplotlb

Chris Webb from CodeDrome
image: Pixabay

LaTeX is a typesetting system used primarily for mathematical and scientific books, articles and papers. Probably its best known and most common use case is for creating mathematical notation, for example:

LaTeX can be used within Matplotlib for titles and labels and in this article I will show a few examples of how this is done. LaTeX is a complex subject so I can only scratch the surface of what is possible. This particular Matplotlib feature seems to be little known so the main purpose of this article is to let you know it exists and to get you started.

These are the LaTeX sample notations I will use in this article. In LaTeX anything prefixed with a backslash is a command which tells LaTeX to render a specific output or process any arguments in a certain way. Anything else is rendered as-is.

Exponents as Superscript

This is created with a caret ^, for example x^2 renders as

If the exponent has more than one digit you will need to enclose it in curly brackets, eg. x^{22} renders as

Greek Letters

All lowercase and some uppercase Greek letters are created with a backslash and their name in the Latin alphabet, for example \Theta and \theta give us

Fractions

These are created with the \frac command followed by the numerator and denominator in separate pairs of curly brackets, for example \pi \approx \frac{22}{7} renders as

Function Domains

If you are graphing mathematical functions you might like to show the domain or valid set of independent values. These typically consist of the “is an element of the set” symbol and a blackboard letter, eg. x \in \mathbb{R} renders as:

This is a list of the widely used blackboard numbers. (Each is a subset of the succeeding set).

  • \mathbb{N} - natural numbers (1, 2, 3...)

  • \mathbb{Z} - integers

  • \mathbb{Q} - rational numbers

  • \mathbb{R} - real numbers

  • \mathbb{C} - complex numbers

Trigonometric Functions

There is a comprehensive selection of these which consist of the backslash followed by their name, for example \sin x for

LaTeX renders anything not otherwise formatted as italic and also ignores spaces so if you type something like sin x you will get this which is not good!

I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.

The Code

This project consists of a single file called latexmpl.py which you can find in the GitHub repository.

NumPy and Matplotlib

The code uses the NumPy and Matplotlib libraries which are imported with their customary aliases. If you have not used these before my introductory articles might be useful.

The main function just has three function calls which you can comment/uncomment as needed.

squares

This function creates a set of numbers and their squares and then goes on to plot them. The reference articles linked above describe how this works.

The main purpose though is to show LaTeX being used to annotate the plot. Firstly on line 22 the label for the curve itself is set to $y=x^2$. In Matplotlib LaTeX is enclosed in $ and as mentioned above the ^ makes the next character superscript. On lines 27 and 28 the x-axis label uses the function domain notation and the y-axis is the same as the label. Finally on line 30 the graph title uses a combination of the above. Note that in Python the backslash within a string is the escape character so itself has to be escaped with another backslash.

Run the program with this terminal command.

python3 latexmpl.py

This is the result.

sines

This function follows the same pattern as the first but plots the sines of a range of angles, the familiar sine wave. Again the curve and axis labels and title use LaTeX, this time using \sin and \Theta.

Uncomment sines() in main and run the program again.

squareroots

As you may know a number to the power of ½ is the square root of that number. This code calculates and plots the square roots of a range of integers and, as you probably guessed, includes the appropriate LaTeX. This time we use \frac{1}{2}.

Run the squareroots function which will give you this.

I hope this article will encourage you to try a bit of LaTeX in your own Matplotlib plots. If you need to use any notation I haven’t covered here please let me know in the comments and I’ll try to help.


For updates and random ramblings please follow me on Bluesky.

No AI was used in creating the code, text or images in this article.

No AI was used in the creation of the code, text or images in this article.

Please feel free to let me know if you have any comments or suggestions on this article.

© 2026 Chris Webb
548 Market Street PMB 72296, San Francisco, CA 94104