以下代码片段中的函数
customAnnotations()
将使用
add_annotation()
为您想要注释的每个特定点生成此图形:
![enter image description here](https://istack.dev59.com/qQhZF.webp)
正如您指定的那样,这将产生一个标注了指定时间段开始和结束的图形。让我知道这对您有何帮助,我们可以根据您的具体需求进行调整。
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import datetime
pd.set_option('display.max_rows', None)
df = pd.DataFrame({'date': {3: '2020-08-03',
4: '2020-08-04',
5: '2020-08-05',
6: '2020-08-06',
7: '2020-08-07'},
'title_sales': {3: 2, 4: 4, 5: 3, 6: 6, 7: 2},
'regression_sales': {3: 4, 4: 6, 5: 5, 6: 6, 7: 4},
'causal_sales': {3: 3, 4: 5, 5: 4, 6: 5, 7: 3}})
df.set_index('date', inplace = True)
def customAnnotations(df, xStart, xEnd, yVal):
fig = go.Figure(data=go.Scatter(x=df.index, y=df[yVal].values, marker_color='black'))
per_start = df[df.index==xStart]
per_end = df[df.index==xEnd]
fig.add_annotation(dict(font=dict(color='rgba(0,0,200,0.8)',size=12),
x=per_start.index[0],
y=per_start[yVal].iloc[0],
showarrow=False,
text='Period start = ' + per_start.index[0] + ' ',
textangle=0,
xanchor='right',
xref="x",
yref="y"))
fig.add_annotation(dict(font=dict(color='rgba(0,0,200,0.8)',size=12),
x=per_end.index[0],
y=per_end[yVal].iloc[0],
showarrow=False,
text='Period end = ' + per_end.index[0] + ' ',
textangle=0,
xanchor='right',
xref="x",
yref="y"))
fig.show()
customAnnotations(df=df, xStart = '2020-08-04', xEnd = '2020-08-06', yVal='regression_sales')