Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Making a subscript and superscript on the same character

Hi,

Does anyone know a good way to make both a subscript and superscript on the same character using Plotly (Python)? I’d like to make something like this: snip

I used 'V<sub>O</sub><sup>**</sup>'and instead the superscript appears after the subscript, as opposed to directly above it: plotly

Thanks,
Becky

You could try like <sup>V<sub>0</sub>**</sup>. See if this gives you desired result.

@reot4424
You can use LaTeX strings instead of html tags to define annotations, plot title, axis title or even text in scatter plots, but for unknow reasons, not in Bar trace:

import plotly.graph_objects as go
text =["$V_0^{**}$", "$V_1^{**}$", "$\\text{Here is: }V_2^{**}$", "$V_3^{**}$"]
fig1 = go.Figure(go.Scatter(x=[2,3,4,5], y=[ 2,3,1, 2.6], text=text, 
                           textposition =["bottom center", "top center", "bottom center", "top center"],
                           mode="lines+text"))

fig1.update_layout(width=700, height=450, yaxis_range=[0.75, 3.25],
                   xaxis_title="$\\text{My LaTeX title: } H_n^q$")
fig1.add_annotation(x=4, y=2,
                    text="$V_O^{**}$",
                    font_size=16,
                    showarrow=False)

With the same text list as above, the LaTeX strings are not rendered correctly for a Bar trace:

fig2 = go.Figure(go.Bar(x=[2,3,4,5], y=[ 2,3,1, 2.6], text=text,  textposition="outside"))
fig2.update_layout(width=700, height=450, yaxis_range=[0, 3.25])

This is a plotly.js bug. I’m I right @archmoj ?

1 Like

@empet Thanks for the response, this was very helpful. I was able to get the LaTeX labels working with my scatterplot.

The labels worked on my bar plot as well, but tick labels are a bit too close to the x-axis. At first glance, I didn’t see any parameters to increase this distance but I’ll keep looking around:

Otherwise, everything else looks good.

@reot4424

Could you please paste here all your layout settings, because I tried to set xaxis ticklabels as in your example and they are perfectly visible:

text =["$V_O^{X}$", "$V_O^{Y'}$", "$V_2^{**}$", "$V_3^{**}$"]
fig = go.Figure(go.Bar(x=text, y=[ 2,3,1, 2.6], width=0.9))
fig.update_layout( width=500, height=450)

Bar-xLaTeX

Also, what Plotly version are you running?

I am running the latest version of Plotly (4.14.3)

I think the issue was that I had to set the LaTeX font to “Huge” since it wasn’t responding to changes in the font size:

    label_dict={'O_vac_pos_2': "$\Huge{V_O^{\cdot\cdot}}$",
                'O_vac_pos_1': "$\Huge{V_O^{\cdot}}$",
                'O_vac': "$\Huge{V_O^{X}}$",
                'O_vac_neg_1': "$\Huge{V_O^{'}}$",
                'O_vac_neg_2': "$\Huge{V_O^{''}}$",
                }

Removing Huge fixed the spacing issue, but the text was too small.

It may be something in my figure settings since your code works as expected and changing the annotation size works fine. I’d send all my settings but it’s in a class meant for multiple figures so it’s pretty complicated to look at.

I was able to get mine working by increasing the font size; This increased the space between the label and axis and didn’t increase the font size:

self.fig.update_xaxes(tickfont=dict(size=35))

Now my figure looks as expected:

Thanks again for your help.

Is it possible to control the distance/positioning of the labels V0, V2 etc on the x-axis, e.g. how do I move them further down?

@windrose

You addressed this question elsewhere and I answered it there:https://community.plotly.com/t/changing-the-xaxis-title-label-position/13562/7

1 Like