How Do I Write A Latex Formula In The Legend Of A Plot Using Matplotlib

Matplotlib is a powerful library in Python for creating visualizations, including various types of plots. One common need in data visualization is adding legends to your plots to explain the data. Often, these legends require the inclusion of LaTeX-formatted mathematical expressions. In this article, we will explore how to write LaTeX formulas in the legend of a plot using Matplotlib inside a Jupyter Notebook or Python script.


Before we dive into the process of adding LaTeX formulas to your Matplotlib legends, make sure you have the following prerequisites in place:

1. Python and Matplotlib Installed

Ensure that you have Python installed on your system. You can download it from the official Python website ( Additionally, you need to have Matplotlib installed. You can install Matplotlib using pip:

pip install matplotlib

2. Jupyter Notebook (Optional)

If you are using Jupyter Notebook, you can install it with:

pip install notebook

Adding LaTeX Formulas to Matplotlib Legends

Matplotlib provides an easy way to include LaTeX-formatted text in various elements of your plot, including the legend. To achieve this, follow these steps:

Step 1: Import Necessary Libraries

In your Python script or Jupyter Notebook, start by importing the required libraries:

import matplotlib.pyplot as plt

Step 2: Create a Plot

Next, create a plot using Matplotlib. For the purpose of this demonstration, let’s create a simple plot with some data points:

# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

# Create a plot
plt.plot(x, y, label='$y=x^2$')

In this example, we’re plotting the function (y = x^2) and using LaTeX formatting to display the equation in the legend.

Step 3: Add a Legend

To add a legend to your plot, use the plt.legend() function. Additionally, specify the location where you want the legend to appear, such as ‘upper right’, ‘lower left’, or ‘center’.

# Add a legend
plt.legend(loc='upper right')

Step 4: Display the Plot

Finally, display the plot using

# Display the plot

Now, when you run your script or Jupyter Notebook cell, you’ll see a plot with a legend in the upper-right corner, displaying the LaTeX-formatted formula (y = x^2).

Advanced LaTeX Formatting

Matplotlib also allows you to customize the appearance of LaTeX-formatted text in your legends. You can use LaTeX commands to control font size, style, and color. For example, to make the formula in the legend red and italicized:

# Customize the legend text
plt.plot(x, y, label='$\color{red}{\mathit{y=x^2}}$')

In this example, we use LaTeX commands \color{red} and \mathit{} to change the color to red and make the text italic.

Frequently Asked Questions

How do I include a LaTeX formula in the legend of a Matplotlib plot?

To include a LaTeX formula in the legend of a Matplotlib plot, you can set the label parameter of the plot to a LaTeX formatted string. For example:

   import matplotlib.pyplot as plt

   x = [1, 2, 3, 4, 5]
   y = [x**2 for x in x]

   plt.plot(x, y, label=r'$y = x^2$')

The r in r'$y = x^2$' denotes a raw string, allowing LaTeX formatting.

Can I use LaTeX commands like \frac or \sqrt in the legend?

Yes, you can use LaTeX commands like \frac and \sqrt in the legend. Here’s an example:

   import matplotlib.pyplot as plt

   x = [1, 2, 3, 4, 5]
   y = [x**2 for x in x]

   plt.plot(x, y, label=r'$y = \sqrt{x}$')

Matplotlib will render the LaTeX commands correctly in the legend.

How do I change the font size of LaTeX-formatted text in the legend?

You can change the font size of LaTeX-formatted text in the legend by using the fontsize parameter when calling plt.legend(). For example:

   plt.plot(x, y, label=r'$y = x^2$')
   plt.legend(fontsize=12)  # Set the font size to 12

What if my LaTeX formula contains special characters like underscores or caret (^)?

When using special characters like underscores or caret (^) in your LaTeX formula within the legend, you should enclose the LaTeX string in curly braces {}. For example:

   plt.plot(x, y, label=r'${y = x^2}$')

This prevents issues with these characters being treated as subscript or superscript.

How do I align the LaTeX text in the legend?

You can align LaTeX text in the legend using LaTeX’s alignment commands within the legend label. For example, to center-align the text:

   plt.plot(x, y, label=r'$\centering y = x^2$')

You can use \centering, \flushleft, or \flushright to control text alignment within the legend.

In this article, we’ve explored how to write LaTeX formulas in the legend of a plot using Matplotlib. This skill is valuable for creating professional and informative data visualizations, especially when dealing with mathematical or scientific data. With Matplotlib’s support for LaTeX formatting, you can easily integrate complex mathematical expressions into your plots and enhance the clarity of your visualizations. Start incorporating LaTeX-formatted legends into your Matplotlib plots today to create more compelling and informative data visualizations.

You may also like to know about:






Leave a Reply

Your email address will not be published. Required fields are marked *