您可以添加另一条迹线,该迹线是具有两个点的线段:
(start_time, max_fill_to_value), (end_time, max_fill_to_value)
,并使用参数 fill='tonexty'
,然后该参数将填充到包含时间序列数据的另一条迹线。例如:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
np.random.seed(42)
df = pd.DataFrame({
'x': pd.date_range(start='2023-10-01',end='2023-10-02',periods=48),
'y': 10+np.random.rand(48)
})
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df['x'],
y=df['y']
))
max_fill_to_value = 5
fig.add_trace(go.Scatter(
x=['2023-10-01','2023-10-02'],
y=[max_fill_to_value, max_fill_to_value],
fill='tonexty',
mode='lines',
line_color='#636EFA',
showlegend=False,
))
y_upper_range = max(df['y']) + (max(df['y'])-max_fill_to_value)/16
fig.update_layout(yaxis_range=[max_fill_to_value, y_upper_range])
fig.show()