How to add units in Mathematical style (like Latex) to a Plotly graph without eroding the rest of the label?

Viewed 209

I want to make a graph that has something on the y-axis, and something on the x-axis where the x axis is given in metres squared. I've tried using Latex dollar signs to put it in 'equation mode' but this gets rid of the text before it.

Code snippet:

figure = go.Figure(
        layout=go.Layout(
            xaxis_title="Area ($m^2$)",
            yaxis_title="Value ($m/s$)"
        ))

When I run the above the 'Area' part of the x-axis title doesn't appear, only the m^2 units. How do I simultaneously get the title and the units in mathematical style to appear?

1 Answers

This only works if the whole label is surrounded by $ signs, thus the following works.

figure = go.Figure(
        layout=go.Layout(
            xaxis_title=r"$\text{Area }(m^2)$",
            yaxis_title=r"$\text{Value }(m/s)$"
        ))

Notice that we write r"$\text{..., or otherwise we need to escape the backslash, e.g. "$\\text{...".

Related