2016-05-10 105 views
3

使用熊貓我創建了一個時間序列的情節是這樣的:行添加到大熊貓繪製

import numpy as np 
import pandas as pd 

rng = pd.date_range('2016-01-01', periods=60, freq='D') 
ts = pd.Series(np.random.randn(len(rng)), index=rng) 

ax = ts.plot() 
ax.axhline(y=ts.mean(), xmin=-1, xmax=1, color='r', linestyle='--', lw=2) 

enter image description here

我想只使用數據的平均水平增加另一個水平線從二月開始。平均值只是ts.loc['2016-02'],但是如何在該級別添加橫跨整個數字的水平線,但僅限於2月份的日期?

回答

1

您可以使用xminxmax來控制圖表開始和結束的位置。但這是圖表的百分比。

import numpy as np 
import pandas as pd 

np.random.seed([3, 1415]) 
rng = pd.date_range('2016-01-01', periods=60, freq='D') 
ts = pd.Series(np.random.randn(len(rng)), index=rng) 
ts_feb = ts['2016-02'] 

# used to figure out where to start and stop 
ts_len = float(len(ts)) 
ts_len_feb = float(len(ts_feb)) 
ratio = ts_len_feb/ts_len 

ax = ts.plot() 
ax.axhline(y=ts.mean() * 5, xmin=0, xmax=1, color='r', linestyle='--', lw=2) 
ax.axhline(y=ts_feb.mean() * 5, xmin=(1. - ratio), xmax=1, color='g', linestyle=':', lw=2) 
1

或者您可以創建一個新的時間序列,其值是平均值,而索引僅涵蓋2月份。

ts_feb_mean = ts['2016-02'] * 0 + ts['2016-02'].mean() 

總之,它看起來像:

import numpy as np 
import pandas as pd 

rng = pd.date_range('2016-01-01', periods=60, freq='D') 
ts = pd.Series(np.random.randn(len(rng)), index=rng) 

# Feb mean 
ts_fm = ts['2016-02'] * 0 + ts['2016-02'].mean() 
ts_fm = ts_fm.reindex_like(ts) 

# Total mean 
ts_mn = ts * 0 + ts.mean() 

# better control over ax 
fig, ax = plt.subplots(1, 1) 
ts.plot(ax=ax) 
ts_mn.plot(ax=ax) 
ts_fm.plot(ax=ax)