python - Plotly:如何在 plotly 线图中的特定点添加标记(python/pandas)

标签 python pandas matplotlib plotly

我有一个 df(如下),我想编写一个函数,该函数将返回一个 plotly 折线图,该折线图在数据集中的指定点处带有数据标签(该图跟踪促销的成功,所以我想标记促销开始和结束的时间 - 但这与数据集本身的开始和结束不同。

index   date    x_sales y_sales
0   2019-10-24  0   27
1   2019-10-25  0   30
2   2019-10-26  0   34
3   2019-10-27  0   36
4   2019-10-28  0   29

plotly 文档不清楚如何添加注释,我能找到的唯一相关的 StackOverflow 问题是关于 R 的。

理想情况下,我想要一张像这里这样的图表:

sales graph with x_sales and y_sales

但在其中一行上有一个小标签用于开始日期和一个用于结束日期

我当前的代码是最基本的:

fig = px.line(df_g, x="date", y=df_g.columns)

感谢您的帮助。

最佳答案

下面代码片段中的函数 customAnnotations() 将使用 add_annotation() 为您要注释的每个特定点生成此图:

enter image description here

正如您指定的那样,这会生成一个数字,其中对指定时间段的开始和结束进行了注释。让我知道这对您来说效果如何,我们可以根据您的具体需求进行调整。

# imports
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)

# data sample
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):
    # xStart = '2020-08-04'
    # xEnd = '2020-08-06'
    # xVal='date'
    # yVal='regression_sales'
    
    
    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],
                                        #x = xStart
                                        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],
                                        #x = xStart
                                        y=per_end[yVal].iloc[0],
                                        showarrow=False,
                                        text='Period end = ' + per_end.index[0] + '  ',
                                        #ax = -10,
                                        textangle=0,
                                        xanchor='right',
                                        xref="x",
                                        yref="y"))

    fig.show()
    
customAnnotations(df=df, xStart = '2020-08-04', xEnd = '2020-08-06', yVal='regression_sales')

关于python - Plotly:如何在 plotly 线图中的特定点添加标记(python/pandas),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64241461/

相关文章:

python - Python 中一张图中的停留图

python - Matplotlib - 提取 3D 多边形图的 2D 轮廓

python - 使用 matplotlib 在一个循环内的两个独立图形窗口中的子图

python - 如何在 Python 中格式化 JSON 文本?

python - Pygame Logo 没有改变

python - 遍历目录路径

python - 如何在列中的每个不同值上拆分 DataFrame?

python - 弹性 map 减少误差

python - 将秒数添加到 python 日期时间,不包括周末

Python:pandas.to_csv 添加不需要的引号