2017-09-06 3 views
0

x- 데이터가 DateTimes 인 matplotlib 플롯을 만들었습니다.Matplotlib ax.fill_between not shown

오전 7 시부 터 오전 10 시까 지 전체 차트 영역과 같이 차트의 특정 영역을 음영 처리하고 싶습니다. 나는 아래에서 비슷한 것을 시도했다. 어떤 값의 오른쪽 전체를 음영 처리했다.

오류가 발생하지 않지만 차트에 음영이 표시되지 않습니다.

도와 주시겠습니까?

df = pd.read_csv(path) 
df['DateTime2'] = pd.to_datetime(df['DateTime']) 
df['Time'] = pd.to_datetime(df['DateTime']).dt.time 

sns.set_palette('muted') 

x = df['DateTime2']#.values 

fig, ax1 = plt.subplots(figsize=(12, 6)) 
fig.patch.set_facecolor('grey') 

ax1.plot(x, df[[col for col in df.columns if 'level' in col]]) 
ax1.set_xlabel('Time of day') 
ax1.set_ylabel('Dam level (%)') 
ax1.set_ylim([0, 100]) 

ax2 = ax1.twinx() 
ax2.plot(x, df[[col for col in df.columns if 'status' in col]]) 
ax2.set_ylabel('Pump status') 
ax2.yaxis.set_major_locator(MaxNLocator(integer=True)) 

ax1.xaxis.set_major_locator(mdates.HourLocator()) 
ax1.xaxis.set_minor_locator(mdates.MinuteLocator(byminute=30)) 
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) 
fig.autofmt_xdate(rotation=90) 
ax1.set_xlim((min(x),max(x))) 

y = df[[col for col in df.columns if 'level' in col]] 
x2 = pd.to_numeric(x.values) 
x2_mask = np.ma.masked_greater_equal(x2, 990002640000000000).mask 
ax1.fill_between(x2, 0, 100, where=x2_mask, facecolor='red') 

ax1.grid(which='major', alpha=0.5) 
ax1.grid(which='minor', alpha=0.25) 
ax2.grid(which='major', alpha=0.5) 
#ax1.minorticks_on() 

fig.tight_layout() 
plt.show() 
fig.savefig('output.png') 
plt.close('all') 

전류 출력 : enter image description here

예상 출력 : 같은 뭔가 : 승리의 라인 뒤에 빨간색 enter image description here 그러나이 페인트 기술!


스크립트 : https://hastebin.com/oyegaceneq.py

데이터 : https://hastebin.com/enabiguxan.csv

+0

당신이의 사진을 보여줄 수 예상 차트와 실제 차트 비교 팅? 업로드하면 링크가 이미지로 바뀝니다. –

+0

감사합니다. @ScottBoston – Mierzen

+0

을 편집합니다. 그래, 합리적인 z-order를 설정하거나 빨강 물건을 먼저 플로팅하십시오. –

답변

1

axvspan과 같은 것을 시도하십시오 (CSV로 저장) :

ax2.axvspan('2001-05-16 07:00:00','2001-05-16 10:00:00',alpha=.5,color='red') 
ax2.axvspan('2001-05-16 16:00:00','2001-05-16 20:00:00',alpha=.5,color='red') 

enter image description here

+0

정확히 그런 것! – Mierzen