第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以陰影形式突出顯示時間序列數(shù)據(jù)中的類別列

以陰影形式突出顯示時間序列數(shù)據(jù)中的類別列

收到一只叮咚 2024-01-16 15:01:00
這里我有一個時間序列數(shù)據(jù),我正在嘗試使用plotly 進行繪制,如復(fù)制的那樣。我有一個分類列fill_cat,分別是二進制 1 或 0 。只要存在 1,就應(yīng)繪制垂直線或陰影以識別是否存在某個事件 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條經(jīng)驗 獲得超11個贊

答案是使用官方參考資料準(zhǔn)備的。在圖形上添加矩形可以通過 來處理add_vrect()。如果它變成手動開始和結(jié)束的間隔,您可以將其著色,但我采取的方法是對“fii_cat”提取的數(shù)據(jù)幀使用循環(huán)過程。(每天都是這樣)我想就是這個效果。未啟用填充顏色。


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

查看完整回答
反對 回復(fù) 2024-01-16
  • 1 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號