Mat
Mat

Reputation: 33

How to set decimal places in plotly subplots hoverlabel?

I would be very happy if someone could help me with this: I created a loop using pandas and plotly express which creates n stacked subplots from a tuple of dataframes selected by the user.

The source data has 10 decimal places, so I set

    pd.set_option('precision',10)

The dataframes show adequate decimal precision, the scatter plots work, but I cannot get the hover label to show all 10 decimal places. I tried to set

    fig.update_layout(hoverlabel_namelength=-1)

but it only changes the X-Axis reference in the hoverlabel, not the Y-Axis (containing the numbers).

Can anyone help me?

Thank you very much in advance!! Maria

Here is my source program:

#import libraries

import tkinter as tk
import tkinter.filedialog
from pathlib import Path

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
pd.set_option('precision',10)

#select files into tuple 'datafiles' via tkinter

root = tkinter.Tk()
pathdir='/***/DSM_Exports/'

datafiles = tkinter.filedialog.askopenfilenames(parent=root,title='Choose a file', initialdir=pathdir)
datafiles = root.tk.splitlist(datafiles)

#prepare subplots template n rows, 1 column

fig = make_subplots(rows=len(datafiles), cols=1, shared_xaxes=True, vertical_spacing=0.01)

# set up loop to create subplot

for counter in range (0, len(datafiles)):  #Set up loop with length of datafiles tuple
    print(counter, datafiles[counter])

    # import file
    table=pd.read_csv(datafiles[counter], sep="\t", header=None)  

    pd.set_option('expand_frame_repr', False)

    # extract DSM cumulative dose column

    numrows = table.shape[0]+1
    print('Number of rows', numrows)

    DSMcml= table[[1,2,3]] #extract colulmns start time, end time and cumul dose
                       #double paranthesis!
    DSMcml= DSMcml.iloc[1:numrows] #cut column name

    DSMcml[2]= pd.to_datetime(DSMcml[2]) #convert to datetime endtime

    DSMcml[3]=DSMcml[3].str.replace(',','.') #change dot to comma in [3]
    DSMcml[3]=DSMcml[3].astype(float, errors = 'raise') #change [3] to float

    DSMcml= DSMcml[DSMcml[3]>=0].dropna() #>>remove lines with values <0

    fig_Xdata= DSMcml[2] #extract end times for X-axis
    fig_Ydata= DSMcml[3].round(10) #extract cumul dose for Y-axis
    
    tracename=Path(datafiles[counter]).stem

    fig.add_trace(
        go.Scatter(x=fig_Xdata, y=fig_Ydata, mode='lines', name=tracename),
        row=counter+1, col=1)
   
    fig.update_layout(title_text=datafiles[counter], hovermode='x unified', hoverlabel_namelength=-1)
    fig.update_xaxes(showspikes=True, spikecolor='green', spikesnap='cursor', spikemode='across', spikedash='solid')

counterstring=str(counter+1)   #set x-axis indicator for shared spike-line
fig.update_traces(xaxis='x'+counterstring) # set shared spike-line

fig.show()
```

Upvotes: 3

Views: 3334

Answers (1)

Derek O
Derek O

Reputation: 19610

You can use a hovertemplate when you add your traces:

fig.add_trace(go.Scatter(x=fig_Xdata, y=fig_Ydata, 
hovertemplate='%{y:.10f}', mode='lines', name=tracename), row=counter+1, col=1)

Upvotes: 4

Related Questions