在Plotly中在线间隙添加注释。

huangapple go评论100阅读模式
英文:

Add annotation between line gap in plotly

问题

我有这样的图表:

我想知道是否有办法在这些线之间添加这个注释?从一个点到另一个点。如果可能的话,我很抱歉可能会重复。

这是我期望的输出:

这是我的当前注释代码:

有关如何做到这一点的任何建议?

英文:

I have a graph like this:
在Plotly中在线间隙添加注释。

Instead of the days being on top of the symbol, I was wondering if there is a way I can add this annotation between the lines? From one dot to another. I apologize if in case, this is a possible duplicate.

This is my expected output:
在Plotly中在线间隙添加注释。

This is my current code for the annotation:

    fig.add_annotation(
        go.layout.Annotation(
            x=row["order_date"],
            y=row["sales"],
            text=f"{row['time_diff']} days",
            showarrow=False,
            align='center',
            yanchor='auto',
            yshift=10,
            textangle=-0
        )

Any suggestions on how can I do this?

答案1

得分: 3

你可以创建一个新的数据框,按照order_date对原始df进行排序,然后计算相邻行之间的平均日期和平均销售额,以确定放置注释的位置。要获取文本,可以计算相邻订单之间的时间差。

你的注释数据框将会是这样的:

      time_diff  sales          order_date
    0    5 天    5.0 2019-08-27 12:00:00
    1   11 天    5.0 2019-09-04 12:00:00
    2    0 天    7.5 2019-09-10 00:00:00
    3    0 天   15.0 2019-09-10 00:00:00
    4    5 天   20.0 2019-09-12 12:00:00
    5    0 天   17.5 2019-09-15 00:00:00
    6  139 天   15.0 2019-11-23 12:00:00
    7       NaT    NaN                 NaT

以下是使用类似你的样本数据创建的图表代码:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

## 创建类似你的样本数据和图表
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')

## 计算相邻点之间的差异:
## 获取时间差异和平均销售额
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()

在Plotly中在线间隙添加注释。

英文:

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

在Plotly中在线间隙添加注释。

huangapple
  • 本文由 发表于 2023年4月20日 09:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定