首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Plotly:如何在绘制折线图中的特定点添加标记(python / pandas)

Plotly:如何在绘制折线图中的特定点添加标记(python / pandas)
EN

Stack Overflow用户
提问于 2020-10-07 17:48:15
回答 1查看 2K关注 0票数 2

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

代码语言:javascript
复制
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。

理想情况下,我想要一个如下所示的图表:

但是其中一条线上有一个start date的小标签和一个end date的标签。

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

代码语言:javascript
复制
fig = px.line(df_g, x="date", y=df_g.columns)

谢谢你的帮助。

EN

Stack Overflow用户

回答已采纳

发布于 2020-10-07 18:38:13

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

正如您所指定的,这将生成一个图,其中注释了指定期间的开始和结束。让我知道这是如何为您工作的,我们可以根据您的特定需求进行调整。

代码语言:javascript
复制
# 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')
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64241461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档