2016-11-28 2 views
1

axvspan 내가 가진팬더 줄거리는

   n 
dates   
2016-01-24 0 
2016-02-26 98 
2016-03-04 68 
2016-03-26 45 
2016-05-03 89 
2016-05-09 83 
2016-05-11 58 
2016-07-29 53 
2016-09-18 79 
2016-10-20 57 

내가 플롯 월과 3 월의 axvspan을하고 싶은 생성이

from datetime import date 
import random 

start_date = date.today().replace(day=1, month=1).toordinal() 
end_date = date.today().toordinal() 

df=pd.DataFrame.from_dict({'dates':[ date.fromordinal(random.randint(start_date, end_date)) for i in xrange(10) ] 
         ,'n':np.random.randint(0,100,10)}) 
df.index=df['dates']; del df['dates'] 
df.sort_index(inplace=True) 

같은 dataframe.

내 플로팅 코드는 간단하다 axvspan 내 날짜 내가 그 2개월 선택하고 사용하는 방법을 무작위이기 때문에 여기에

ax=df.plot() 
+2

는 axvspan (pd.datetime (2016, 2, 1), pd.datetime은 (2016,4,1)이) 당신이 원하는 무엇을 제공합니까? –

+0

그게 작동합니다. 어떤 이유로 나는 ax.axvspan (pd.datetime (2016, 2, 1), pd.datetime (2016,4,1)) 루프를 사용하여 아무 것도 볼 수 없습니다. 그렇지 않으면 작동합니다. – NinjaGaiden

답변

1

은 예입니다. 날짜가 수 년에 걸쳐 적용되는 경우 개선되어야합니다. 원칙이 정확히 같으므로 this question의 답을 확인할 수도 있습니다.

ax = df.plot() 
# Getting the boundaries for dates in feb and march 
period = df[(df.index.month > 1) & (df.index.month < 3)].index 
# Highlighting 
ax.axvspan(min(period) - MonthBegin(), max(period) + MonthEnd(), 
      facecolor='g', edgecolor='none', alpha=.2) 

enter image description here