2017-05-26 56 views
2

我是一個新手,並試圖在我的圖表上設置兩條線,分別爲.fill_between()。我試過了我能想到的一切,以確保底層數據是正確的,但每次我繪製這個圖時,.fill_between()在x軸上出現的一個值比它應該是短的。 這裏是我的代碼:Matplotlib .fill_between()抵消-1 -1

df_combo = pd.read_csv('MySampleData.csv') 
max_hist_temps = df_combo['hist_max'] 
min_hist_temps = df_combo['hist_min'] 
plt.figure() 
plt.plot(max_hist_temps, '-.r' 
     , min_hist_temps, '-.b' 
     ) 
plt.gca().fill_between(range(len(max_hist_temps)), 
     min_hist_temps, max_hist_temps, 
     facecolor='blue', 
     alpha = 0.25) 
ax = plt.gca() 
ax.axis([0,364,-45,45]) 
plt.show() 

下面是一些樣本數據:

Day_of_Year hist_min hist_max 
1    -16 15.6 
2    -26.7 13.9 
3    -26.7 13.3 
4    -26.1 10.6 
5    -15 12.8 
6    -26.6 18.9 
7    -30.6 21.7 
8    -29.4 19.4 
9    -27.8 17.8 

感謝你的幫助, 我

回答

4

的問題是,你沒有指定正確的參數x fill_between。您將通過從0開始的range(len(...)),而您的Day_of_Year從1開始,因此是1偏移量。

[加,我覺得你的例子是不完整的,你應該說你設置Day_of_Year爲您的數據的索引。]

解決您的問題,傳遞的max_hist_temp指數在x PARAM fill_between功能:

plt.gca().fill_between(max_hist_temps.index, # CHANGED HERE 
     min_hist_temps, max_hist_temps, 
     facecolor='blue', 
     alpha = 0.25) 
1

您可能需要使用數據框作爲實際x值的Day_of_Year列,而不是一些其他指標。

import io 
import pandas as pd 
import matplotlib.pyplot as plt 

u = u"""Day_of_Year hist_min hist_max 
1    -16 15.6 
2    -26.7 13.9 
3    -26.7 13.3 
4    -26.1 10.6 
5    -15 12.8 
6    -26.6 18.9 
7    -30.6 21.7 
8    -29.4 19.4 
9    -27.8 17.8""" 



df_combo = pd.read_csv(io.StringIO(u), delim_whitespace=True) 
max_hist_temps = df_combo['hist_max'] 
min_hist_temps = df_combo['hist_min'] 
plt.figure() 
plt.plot(df_combo['Day_of_Year'], max_hist_temps, '-.r', 
     df_combo['Day_of_Year'], min_hist_temps, '-.b' 
     ) 
plt.gca().fill_between(df_combo['Day_of_Year'], 
     min_hist_temps, max_hist_temps, 
     facecolor='blue', 
     alpha = 0.25) 
ax = plt.gca() 
ax.axis([0,10,-45,45]) 
plt.show() 

enter image description here