亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

python matplotlib中的時間序列奇怪的插值

python matplotlib中的時間序列奇怪的插值

呼喚遠方 2022-06-28 10:30:32
我是 matplotlib 的新手,并且正在處理周一到周五僅在9to間隔中具有值的數據。5當我嘗試繪制數據時,我注意到在下一個工作日之間17:00和9:00下一個工作日之間的缺失值之間存在插值,這是由于 matplotlib 突然包含了缺失的日期時間。我的目標是:1)在不連續的左側添加垂直虛線2)刪除插值以下是我正在使用的代碼片段。該代碼將生成兩個圖。import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom datetime import datetimex_var1= pd.date_range(datetime(2014, 1, 14, 9, 0),datetime(2014, 1, 21, 17, 0),                   freq="30min",                   tz= 'US/Pacific',                   closed= 'left'                   )x_var1 = x_var1[x_var1.dayofweek < 5]x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= False)]x_var1= x_var1[x_var1.hour != 12]np.random.seed(0)y_var2= np.random.normal(loc= 40, scale= 4.4, size= len(x_var1))fig, ax= plt.subplots(nrows= 2, ncols= 1)ax[0].plot(x_var1, y_var2)ax[0].set_title("This plot has extra interpolation that needs to be removed and needs vertical dotted lines for discontinuities")x_var2= np.arange(len(x_var1))ax[1].plot(x_var2, y_var2)ax[1].set_title("this plot only needs correct xaxis and added vertical dotted lines for discontinuities")plt.show()我的目標是使頂部圖看起來像底部圖,除了底部圖需要將其 x 軸更改為頂部圖的 x 軸并在不連續的左側添加垂直虛線。我對 matplotlib 的世界還很陌生,所以我不確定如何以 Python 的方式高效地完成這些事情。編輯:將示例數據集更改為更隨機的數據集,以更好地反映實際問題。12:00還包括午休時間,因此在和之間會丟失數據12:59
查看完整描述

2 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

好的,這就是你要找的嗎?


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


x_var1= pd.date_range(pd.to_datetime('2014-01-14 09:00:00'), pd.to_datetime('2014-01-21 17:00:00'),

                   freq="30min",

                   tz= 'US/Pacific',

                   closed= 'left'

                   )


x_var1 = x_var1[x_var1.dayofweek < 5]

x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= True)]

x_var1= x_var1[x_var1.hour != 12]


np.random.seed(0)

y_var2= np.random.normal(loc= 40, scale= 4.4, size= len(x_var1))


df = pd.DataFrame(index=x_var1, data=y_var2)


# mpl won't plot between nan values

df[(df.index.hour == 17) & (df.index.minute == 0)] = np.nan

df[(df.index.hour == 9) & (df.index.minute == 0)] = np.nan

fig, ax = plt.subplots()

df.plot(ax=ax, legend=False)

df[(df.index.hour == 9) & (df.index.minute == 0)] = 0


# we know the discontinuities occur at the end of the days

[plt.axvline(d, ls='--') for d, v in list(zip(df.index, df.iloc[:, 0])) if pd.isnull(v)]

http://img1.sycdn.imooc.com//62ba68020001221703790254.jpg

查看完整回答
反對 回復 2022-06-28
?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

這就是我將如何做到這一點。不確定我是否將垂直線放在正確的不連續處,但希望你能明白:


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


x_var1= pd.date_range(pd.to_datetime('2014-01-14 09:00:00'), pd.to_datetime('2014-01-21 17:00:00'),

                   freq="30min",

                   tz= 'US/Pacific',

                   closed= 'left'

                   )

x_var1 = x_var1[x_var1.dayofweek < 5]

x_var1= x_var1[x_var1.indexer_between_time('9:00','17:00', include_end= True)]


df = pd.DataFrame(index=x_var1, data=[np.nan]*len(x_var1))

df.iloc[0, 0] = 0

df.iloc[-1, 0] = 100

# to get line with "slope of 1"

df = df.interpolate(method='time')


# mpl won't plot between nan values

df[(df.index.hour == 17) & (df.index.minute == 0)] = np.nan

df[(df.index.hour == 9) & (df.index.minute == 0)] = np.nan

fig, ax = plt.subplots()

df.plot(ax=ax, legend=False)

df[(df.index.hour == 9) & (df.index.minute == 0)] = 0


# we know the discontinuities occur at the end of the days

[plt.axvline(d, ls='--') for d, v in list(zip(df.index, df.iloc[:, 0])) if pd.isnull(v)]

輸出:

http://img1.sycdn.imooc.com//62ba681000012ea503790257.jpg

查看完整回答
反對 回復 2022-06-28
  • 2 回答
  • 0 關注
  • 318 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號