How to get the axis ticks of a scatter plot to be shown in Latex using Python? --- [Python, Latex Representation]

My question in short can be seen in the figure. The numbers which are shown in the yellow box - how do I get them into Latex representation?

Long story or description:

You can see that, the x-axis (horizontal axis) is already displayed in Latex. Also, the \beta as the x-title, the vertical axis titles (x,y,z) and the labels are displayed using Latex.

Labels in Latex: Just have a look on the argument name in the following code

fig.append_trace(
    go.Scatter(
                x= mPVs,
                y= dat[:,2],
                name =r"$\text{z}$",
                    ),
    
        row=3, col=1)

Axes Titles : By the way, I do not like the pep8 style - I prefer start_Next_And_Next -style. I consider this type of coding to be less strenuous on the eyes.

def ticks_Modi(fig, ticks):
    
    # get the ticks to Latex representation
    ticks_Latex = [rf"${i:.3f}$" for i in ticks]
    
    # generate empty dicts
    tick_Coll = {
    "yaxis1": {},
    "yaxis2": {},
    "yaxis3": {},
    }
    
    # horizontal axis -> get as latex
    tick_Sett= {
    "tickmode": 'array',
    "tickvals": np.round(ticks, decimals = 3),
    "ticktext":  ticks_Latex
    }
    
    # vertical axes --> Latex, but HOW?
    tick_Sett_Y = {
    # "tickmode": 'array',
    # "tickvals": np.round(ticks, decimals = 3),
        
    # Plotly uses D3 -> also d3 formats doesent work here
    "tickformat": r"$"+".0%"+r"$"
    }
    
    tick_Coll["xaxis1"] = tick_Sett
    tick_Coll["yaxis1"] = tick_Sett_Y
    
    # get the axis title to be in latex
    tick_Coll["yaxis1"]["title"] = r"$\text{x}$"
    
    tick_Coll["xaxis2"] = tick_Sett
    tick_Coll["yaxis2"]["title"] = r"$\text{y}$"
    
    tick_Coll["xaxis3"] = tick_Sett
    tick_Coll["xaxis3"]["title"]= r"$\beta$"
    tick_Coll["yaxis3"]["title"] = r"$\text{z}$"
    
    # apply changes
    fig.update_layout(tick_Coll)
    
    return fig

What I also tried:

  1. Try to access the original vertical axes numbers and then use โ€œticktextโ€. However, unfortunately I was not able to get the original vertical axes ticks โ†’ see also: tickformat in Plotly Python

  2. Ploty is build on d3 and if we apply โ€œtickformatโ€ then the formatting abilities of d3 are invoked. Examples for that are given in:

  3. Plotlyโ€™s Documentation

  4. d3.format link

  5. d3 formatting GitHub

Still, up to now I do not know a solution. The reason, why I need Latex is that I want to embed these plots in my thesis.

Thanks to everyone, who is willing to spend his time on my issue.

Hello, finally I found a solution. I want to thank @RenaudLN. The post in Format ticks : add 'km' at the end reminded me of the possibility to use prefixes and suffixes. The following code solved my issue

 tick_Sett_Y = {
            "tickprefix": r"$" ,
            "ticksuffix": r"$",
        }

tick_Coll["yaxis1"] = tick_Sett_Y.copy()

# apply changes
fig.update_layout(tick_Coll)
1 Like

To add to this topic a new development for those that would like to use Latex.

With the latest Plotly.js v2.10.0, you now have support for Mathjax. Here are a coupe of examples for Latex with Plotly.