为了账号安全,请及时绑定邮箱和手机立即绑定

以阴影形式突出显示时间序列数据中的类别列

以阴影形式突出显示时间序列数据中的类别列

收到一只叮咚 2024-01-16 15:01:00
这里我有一个时间序列数据,我正在尝试使用plotly 进行绘制,如复制的那样。我有一个分类列fill_cat,分别是二进制 1 或 0 。只要存在 1,就应绘制垂直线或阴影以识别是否存在某个事件 1。 import numpy as npimport pandas as pdimport plotly.graph_objects as goimport plotly.express as pximport datetimepd.set_option('display.max_rows', None)# data samplenperiods = 200np.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] = 0df = 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()
查看完整描述

1 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

答案是使用官方参考资料准备的。在图形上添加矩形可以通过 来处理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()

https://img1.sycdn.imooc.com/65a629e80001b02611070446.jpg

查看完整回答
反对 回复 2024-01-16
  • 1 回答
  • 0 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信