How to add math to Matplotlib plot

Sometimes, you may need to add LaTeX mathematical expressions to a Matplotlib plot. For example, adding Greek symbols, superscript, subscript or formulas to the axis labels, plot title or tick labels.

Doing that is very easy in Matplotlib. Just indicate the text to be a raw string and enclose the mathematical expression in dollar signs. For example:

import matplotlib.pyplot as mplot
mplot.xlabel(r"$x_1 > x_3$")

Tried with: Python 2.7.6 and Ubuntu 14.04


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.