What you can do is create a new dataframe for your annotations by ordering your original df
by the order_date
, then take the average datetime and average sales between consecutive rows to determine where to place the annotations. To obtain the text, you can take the difference in time between consecutive orders.
Your dataframe for annotations would look something like this:
time_diff sales order_date
0 5 days 5.0 2019-08-27 12:00:00
1 11 days 5.0 2019-09-04 12:00:00
2 0 days 7.5 2019-09-10 00:00:00
3 0 days 15.0 2019-09-10 00:00:00
4 5 days 20.0 2019-09-12 12:00:00
5 0 days 17.5 2019-09-15 00:00:00
6 139 days 15.0 2019-11-23 12:00:00
7 NaT NaN NaT
And here is a figure I made using some sample data similar to yours:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
## create sample data and figure similar to yours
df = pd.DataFrame({
'order_date': ['2019-08-25 00:00:00','2019-08-30','2019-09-10','2019-09-10','2019-09-10','2019-09-15','2019-09-15','2020-02-01'],
'sales': [5,5,5,10,20,20,15,15],
})
df['order_date'] = pd.to_datetime(df['order_date'])
fig = px.line(df, x='order_date', y='sales', markers=True, )
fig.update_traces(marker_color='blue', line_color='darkgrey')
## between consecutive points:
## get the difference in time, and the average sales
df_diff = pd.DataFrame({
'time_diff': df['order_date'].diff(),
'sales': (df['sales'] + df['sales'].shift(-1)) / 2,
})
df_diff['order_date'] = df['order_date'] + (df_diff['time_diff'] / 2).shift(-1)
df_diff['time_diff'] = df_diff['time_diff'].shift(-1)
y_axis_range = 1.25*(df.sales.max() - df.sales.min())
fig.add_trace(
go.Scatter(
x=df_diff["order_date"],
y=df_diff["sales"] + 0.01*y_axis_range,
text=df_diff["time_diff"].astype(str),
mode='text',
showlegend=False,
)
)
fig.show()
