- Plotly uses MathJax for LaTeX math symbols, but default LaTeX line breaks (
\\) need a special way to work. - Standard LaTeX writing might not work in Plotly. This is because Python text strings and MathJax read the text more than once.
- The best way to write math on multiple lines in Plotly is to use raw Python strings and the
alignsection. - Mixing markdown text styles with LaTeX in Plotly often causes mistakes in how things look or puts things in the wrong place.
- Using
$$...$$oralignto show math as a block is good for putting math clearly on axis labels, notes, and titles.
Introduction
Working with Plotly’s math mode makes it possible to use math symbols and formulas like LaTeX. These can look good enough for papers and go on plot notes and axis labels, not just be plain text. But people often have trouble with formatting. This happens a lot when trying to add breaks inside math. This article looks closely at how Plotly's math mode works. We see why LaTeX line breaks don't work the way you think they should. And then, we show how to get line breaks to work right. We'll show you with code examples and good ways to do things.
What is Math Mode in Plotly?
Plotly lets you use math symbols and formulas like LaTeX. It uses MathJax to do this behind the scenes. This lets you put math symbols and formulas on the plot. You can add them to:
- Plot titles
- Axis labels
- Pop-up notes
- Notes on the plot
When you use math mode, Plotly tells the MathJax tool to read whatever is inside $...$ or $$...$$ as LaTeX.
What LaTeX Commands Work in Plotly
Within Plotly's math mode, most standard LaTeX math writing works. This includes things like:
- Numbers slightly above or below others:
x^2,a_i - Parts of a whole:
\frac{a}{b} - Adding many numbers or finding the area under a curve:
\sum,\int - Greek letters:
\alpha,\lambda, and others - Tables of numbers and math on many lines:
\begin{bmatrix} ... \end{bmatrix},alignsection
Pretty much anything MathJax can read in a LaTeX file will work here too. But you need to use the right way to write it and handle special characters.
Why Standard LaTeX Line Breaks Fail in Plotly
It's a common problem for many people trying to add line breaks with the usual LaTeX \\ inside Plotly figures.
Why it Doesn't Work
Plotly looks at the math text more than once before MathJax gets it. These steps include:
- Python text reader – Reads special characters like
\\. This doesn't happen if you use a raw string. - Plotly drawing tool – Draws the graph layout and turns the text into HTML.
- MathJax reader – The last step that reads the LaTeX.
If you don't write it the right way, your line breaks might vanish or just show up as \\. This can cause problems with how the graph looks or even break it.
What often goes wrong:
- Not using raw strings (
r"..."). This causes\to be eaten by Python. - Trying to break lines inside
$...$instead of$$...$$. - Mixing regular text and LaTeX in a way that isn't safe.
Plotly’s Drawing Tool: MathJax Behind the Scenes
Plotly uses MathJax to deal with LaTeX. MathJax is a good tool made with JavaScript. It makes math look sharp in browsers.
How MathJax Reads What You Give It
'$...$'– Inline math'$$...$$'– Display block math\begin{align}...\end{align}– Multi-line math that lines up\\– Line breaks inside math blocks
The main point is, for MathJax to make a line break, it has to actually get \\. This means we need to use Python so \\ stays the same through all the steps.
Raw Strings Help
Python's raw strings (r"...") stop one step where the text is read. This lets us send \\ straight to MathJax. Python won't change it. Example:
r"\\"
This will really be sent as \\. MathJax will read it the right way as a line break.
How to Add Line Breaks That Actually Work
Here are two good ways to get line breaks that work in Plotly math mode.
Method 1: Use \\ and a Raw String
You do this by putting the math inside a raw string and writing \\ where you want a break.
import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(
title=r"$$E = mc^2 \\ F = ma$$"
)
fig.show()
Why it Works:
r"makes sure Python doesn't mess with the backslashes early on.\\gets to MathJax just fine. This makes the line breaks work.
Method 2: Use align for Cleaner Math
This works best for longer or harder math that needs lines lined up or many lines. It looks like how you'd set things up in a LaTeX file.
fig = go.Figure()
fig.add_annotation(
x=2, y=3,
text=r"$$\begin{align} E &= mc^2 \\ F &= ma \end{align}$$",
showarrow=False
)
fig.show()
Good points:
- Lines up equal signs so it's easy to read.
- Deals with many equations well.
- Gives you a better way to show steps in math.
Real-World Code Look
Let's go deeper with these methods and put them together in a graph you might make for a science report.
import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(
title={
"text": r"$$\begin{align} y &= mx + b \\ z &= ax^2 + bx + c \end{align}$$",
"x": 0.5
},
annotations=[
go.layout.Annotation(
x=2, y=2,
text=r"$$\begin{align} a^2 + b^2 &= c^2 \\ \text{Hypotenuse Rule} \end{align}$$",
showarrow=True,
arrowhead=2
)
]
)
fig.show()
This code makes two separate blocks of LaTeX. One is in the title, and the other is a note. See how both equations use the align section to keep the math tidy on different lines.
Things That Can Go Wrong and What You Can't Do
Plotly math mode can do a lot, but it's not perfect. Here are some things that often go wrong and what you can't do:
- âť— If you don't use raw strings, the LaTeX won't work right (
\goes away or acts weird). - ⚠️ You can't use
\\for line breaks in inline math ($...$). - 📉 Axis labels might look different than annotations. Check how it looks in each spot.
- đź”— HTML stuff (like
<br>) doesn't work inside math mode. - 🧬 MathJax sometimes doesn't fully support hard-to-write LaTeX sections when used on the web.
Display vs Inline Math Mode in Plotly
It's really important to know when to use block math ($$...$$) and when to use inline math ($...$).
Inline Mode ($...$)
Use for simple math inside text:
text=r"$E = mc^2$"
Good for legends, part of axis titles, or short notes.
Display Mode ($$...$$ or align)
Use for key formulas or math that needs many lines:
text=r"$$\begin{align} y &= mx + b \\ z &= ax^2 + bx + c \end{align}$$"
Good for:
- Plot titles
- Full math formulas
- Reports and slides
Finding Problems with Math Mode
When things don't work, it's usually because of common mistakes.
What to Check:
- âś… Are you using raw strings like
r"..."? - âś… Would your LaTeX work somewhere like MathJax or Overleaf?
- âś… Are you only using
\\inside$$...$$blocks? - âś… Did you make sure not to mix HTML or markdown with the math text?
- âś… Look at the browser's console (if you're using Plotly on the web) for MathJax problems.
Making Multi-line Equations Look Nice
You can't change the look of math in Plotly as much as in a full LaTeX file.
What you can change:
- How text lines up: use settings like
xanchor,x, andyanchor. - Size of the letters: through layout or annotation settings.
fig.add_annotation(
...,
font=dict(size=18)
)
What you can't change:
- Space between lines.
- Font used for the math.
- How different parts inside the math look.
MathJax handles these inside, and Plotly usually doesn't let you change them with its tools.
Where to Use This: Plot Titles in Science Reports
Think about making a chart for a physics paper that shows how a particle's energy goes down over time. A Plotly title using multi-line LaTeX can make your charts stand out:
fig.update_layout(title=r"$$\begin{align} f(t) &= A e^{-\lambda t} \\ \text{where } \lambda &= \text{decay constant} \end{align}$$")
This makes things clear right away. Anyone looking at the chart gets the main formula and what it means just from the chart itself. This means you don't need long descriptions.
Good Ways to Work with Plotly LaTeX
To make sure your math is easy to read, neat, and works right, do these things:
- âś… Always use raw strings when putting LaTeX in text.
- âś… Don't mix LaTeX math mode with markdown (like
**,_). - âś… Check how the formatting looks using a MathJax test tool before putting it in Plotly.
- âś… Use
alignfor any math with many parts or equal signs. - âś… Break up long formulas into easier parts and put them as separate notes.
- âś… It's better to use display math when using math mode in titles or parts of the graph.
Why Writing it Right Matters
Good pictures often decide if people understand an analysis well or not. A chart with clear, multi-line math formulas can:
- Show how you got the answer step by step.
- Make formulas perfectly clear.
- Make tech reports look more trustworthy.
- Help teaching with charts that are easier to understand.
- Give more meaning in talks for work or school.
Basically, when you learn how to make Plotly show LaTeX right and handle line breaks, your charts get a lot better.
More Tools and Help
Here are some tools to help you do better:
- ✅ MathJax Official Documentation – Check this for all the LaTeX you can use.
- ✅ Plotly’s LaTeX Docs – Rules for using LaTeX in Plotly.
- ✅ Live MathJax Tester – Test your LaTeX text before using it.
- ✅ Python String Escaping Guide – Learn how to stop problems with special characters in Python text.
Getting Good at Plotly Math Mode and Line Breaks
Today, where we use lots of data and share technical ideas, writing math the right way isn't just good, it's a must. Using plotly math mode can seem tricky because of how text is read and shown. But once you see how Python, Plotly, and MathJax work together, it's a strong tool.
If you're writing simple math or showing full steps in notes or titles, the ways we talked about here will make your charts show math well and look good. Use Plotly line breaks on purpose. Think of it as part of making the chart look right, not just a fix. What happens? You get clearer ideas, better talks, and reports that are done better.
Sources:
MathJax Consortium. (n.d.). MathJax: Beautiful math in all browsers. Retrieved from https://www.mathjax.org
Plotly Technologies Inc. (n.d.). LaTeX and Math formatting in Plotly. Retrieved from https://plotly.com/python/latex/
Stack Overflow users. (2023, Aug 1). How to have a line break in the math mode text in Plotly. Retrieved from https://stackoverflow.com/questions/79690330/how-to-have-a-line-break-in-the-math-mode-text-in-plotly