3

Can you please tell me why fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$") does not work in the following code

import plotly.express as px
fig = px.bar(df, x=["Apples", "Oranges"], y=[10,20], color=["Here", "There"],
    labels=dict(x="Fruit", y="Amount", color="Place")
)

fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$", font_size=16)
                )

fig.show()

gives

enter image description here

hans
  • 323
  • 1
  • 14
  • 1
    What browser are you using. I noticed the same problem with Firefox, but in Chrome everything seem to work fine. BTW, notice that you didn't escape backslashes in your Python string. I suggest using `text=r'$\text{...} \sqrt{...}'` or escaping backslashes - `text='\\text{...} \\sqrt{...}'` – Andrew Slabko Apr 11 '21 at 21:01
  • Yes Firefox. Not an issue with Safari. Never tried Chrome (but see vestland's answer below). – hans Apr 12 '21 at 22:06

1 Answers1

1

To know for sure why it's not working on your end I would have to know:

  1. your plotly version, and
  2. how you're displaying your figure (JupyterLab?), and
  3. whether or not there's enough space for your title where you're outputting your figure.

Because it works fine on my end:

enter image description here

I'm running Plotly '4.14.3' in JupyterLab.

Same code as yours:

import plotly.express as px
fig = px.bar(df, x=["Apples", "Oranges"], y=[10,20], color=["Here", "There"],
    labels=dict(x="Fruit", y="Amount", color="Place")
)

fig.update_yaxes(title=dict(text="$\text{This is a test:} \sqrt{2^4}$", font_size=16)
                )

fig.show()

Edit 1: Google Chrome

It turns out that this issue might be related to the browser your running. I'm running Microsoft Edge at the moment. But here's the same code and figure using Chrome wiht no title:

enter image description here

vestland
  • 55,229
  • 37
  • 187
  • 305
  • Thanks a lot for your answer. (1) I just updated Plotly from `4.14.1` to `4.14.3`. Unfortunately, however, the issue remains. (2) I am using `Jupyter Notebook` with `Firefox` browser. (3) Any other title, e.g. `fig.update_yaxes(title=dict(text="This is a title", font_size=16))` displays just fine; however, if I use `$`, e.g. `fig.update_yaxes(title=dict(text="$This is a title$", font_size=16))` the title is gone? I have no idea why? – hans Mar 19 '21 at 21:50
  • 1
    @Hans That's *very* interesting. I'm running Edge actually. Hang on, I'll try other browsers... – vestland Mar 19 '21 at 21:54
  • 1
    @Hans What works fine on my end using JupyterLab and MS Edge does *not* work with Google Chrome. I'm not sure why, though... – vestland Mar 19 '21 at 21:56
  • I just ran the code in a `Jupyter Notebook` in `Safari`browser (I am on a rather old Mac here). Là voila - it works! This seems to a specific issue with `Firefox` (and perhaps also `Google Chrome`). I have no idea _why_? – hans Mar 19 '21 at 22:04
  • @hans Would you consider marking my suggestion as the accepted answer? – vestland Apr 12 '21 at 06:38