Javed Was
Javed Was

Reputation: 86

How to get the axis ticks of Plotly's scatter plot to be shown in Latex using Python? --- [Python, Plotly, 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? Question can be seen here

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:
  1. Plotly's Documentation
  2. d3.format link
  3. 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.

Upvotes: 0

Views: 273

Answers (1)

Javed Was
Javed Was

Reputation: 86

finally I found a solution. I want to thank @RenaudLN. The post in Plotly Question 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)

Upvotes: 1

Related Questions