如何在 plotly subplots hoverlabel 中设置小数位?
How to set decimal places in plotly subplots hoverlabel?
如果有人能帮我解决这个问题,我会很高兴:
我使用 pandas 和 plotly express 创建了一个循环,它从用户选择的数据帧元组中创建了 n 个堆叠的子图。
源数据有10位小数,所以我设置
pd.set_option('precision',10)
数据框显示了足够的小数精度,散点图有效,但我无法让悬停标签显示所有 10 位小数。
我试着设置
fig.update_layout(hoverlabel_namelength=-1)
但它只更改了悬停标签中的 X 轴参考,而不是 Y 轴(包含数字)。
谁能帮帮我?
非常感谢您!
玛丽亚
这是我的源程序:
#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()
```
您可以在添加跟踪时使用 hovertemplate:
fig.add_trace(go.Scatter(x=fig_Xdata, y=fig_Ydata,
hovertemplate='%{y:.10f}', mode='lines', name=tracename), row=counter+1, col=1)
如果有人能帮我解决这个问题,我会很高兴: 我使用 pandas 和 plotly express 创建了一个循环,它从用户选择的数据帧元组中创建了 n 个堆叠的子图。
源数据有10位小数,所以我设置
pd.set_option('precision',10)
数据框显示了足够的小数精度,散点图有效,但我无法让悬停标签显示所有 10 位小数。 我试着设置
fig.update_layout(hoverlabel_namelength=-1)
但它只更改了悬停标签中的 X 轴参考,而不是 Y 轴(包含数字)。
谁能帮帮我?
非常感谢您! 玛丽亚
这是我的源程序:
#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()
```
您可以在添加跟踪时使用 hovertemplate:
fig.add_trace(go.Scatter(x=fig_Xdata, y=fig_Ydata,
hovertemplate='%{y:.10f}', mode='lines', name=tracename), row=counter+1, col=1)