【问题标题】:Highlighting the category column in time series data in plotly as shades以阴影突出显示时间序列数据中的类别列
【发布时间】:2020-10-28 08:48:53
【问题描述】:

这里我有一个时间序列数据,我正在尝试使用 plotly 进行绘制。我有一个分类列fill_cat,分别是二进制 1 或 0。在曾经存在 1 的地方,应绘制一条垂直线或阴影以识别存在某个事件 1。

 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
nperiods = 200
np.random.seed(123)
df = pd.DataFrame(np.random.randint(-10, 12, size=(nperiods, 4)),
                  columns=list('ABCD'))
datelist = pd.date_range(datetime.datetime(2020, 1, 1).strftime('%Y-%m-%d'),periods=nperiods).tolist()
df['dates'] = datelist 
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
df.iloc[0] = 0
df = df.cumsum().reset_index()
df['fill_cat'] = [0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
       1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0] 
df.head()

fig = px.line(df, x='dates', y=df.columns[1:])
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,255,0.1)')
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,255,0.1)')
fig.show()

【问题讨论】:

    标签: python visualization plotly-python timeserieschart


    【解决方案1】:

    答案是使用官方参考准备的。在图形上添加矩形可以由add_vrect() 处理。如果它变成一个手动开始和结束的间隔,你可以给它上色,但我采取的方法是对'fii_cat'提取的数据帧使用循环过程。 (每天都这样)我想这就是效果。填充颜​​色未启用。

    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
    nperiods = 200
    np.random.seed(123)
    df = pd.DataFrame(np.random.randint(-10, 12, size=(nperiods, 4)),
                      columns=list('ABCD'))
    datelist = pd.date_range(datetime.datetime(2020, 1, 1).strftime('%Y-%m-%d'),periods=nperiods).tolist()
    df['dates'] = datelist 
    df = df.set_index(['dates'])
    df.index = pd.to_datetime(df.index)
    df.iloc[0] = 0
    df = df.cumsum().reset_index()
    df['fill_cat'] = [0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
           1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
           0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0] 
    df.head()
    
    
    fig = px.line(df, x='dates', y=df.columns[1:])
    fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,255,0.1)')
    fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,255,0.1)')
    
    fill_data = df[df['fill_cat'] == 1]
    for idx,row in fill_data.iterrows():
        d = str(row.dates.strftime('%Y-%m-%d'))
        fig.add_vrect(x0=str(row.dates.strftime('%Y-%m-%d')),
                      x1=str(row.dates.strftime('%Y-%m-%d')),
                      fillcolor='yellow',
                      opacity=0.5,
                      line_width=2)
    
    fig.show()
    

    【讨论】:

    • AttributeError: 'Figure' object has no attribute 'add_vrect' 收到此错误
    • 更新到所有创建图表的代码。您现在可以查看了。
    猜你喜欢
    • 2011-12-09
    • 1970-01-01
    • 2022-01-21
    • 2018-09-18
    • 2010-12-27
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多