如何在绘图子绘图悬停标签中设置小数位数



如果有人能帮我,我会非常高兴:我使用pandas和plotly express创建了一个循环,它从用户选择的数据帧元组中创建了n个堆叠的子画面。

源数据有10位小数,所以我设置了

pd.set_option('precision',10)

数据帧显示出足够的小数精度,散点图可以工作,但我无法让悬停标签显示所有10位小数。我试着设置

fig.update_layout(hoverlabel_namelength=-1)

但它只更改悬停标签中的X轴参考,而不更改Y轴(包含数字(。

有人能帮我吗?

提前非常感谢!!Maria

这是我的源程序:

#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()
```

添加跟踪时可以使用悬停模板:

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

最新更新