Coursera 課程 Applied Plotting, Charting & Data Representation in Python
Plotting Weather Patterns
數(shù)據(jù)來源: 本次使用的是密歇根安娜堡附近地區(qū)從2005年到2015年的天氣數(shù)據(jù),來源于The National Centers for Environmental Information (NCEI) [Daily Global Historical Climatology Network]。其中每一行為一個觀測值。
數(shù)據(jù)變量:
id : station identification code
date : date in YYYY-MM-DD format (e.g. 2012-01-24 = January 24, 2012)
element : indicator of element type
TMAX : Maximum temperature (tenths of degrees C)
TMIN : Minimum temperature (tenths of degrees C)
value : data value for element (tenths? of degrees C)
導(dǎo)出數(shù)據(jù)后可以看到是類似如下的表格:

可視化目標(biāo):我們旨在運(yùn)用Matplotlib制作關(guān)于溫度的折線圖,在圖中表示出一年中每一天的從2005年到2014年中的歷史最高和最低溫度(C)。此外我們還要對比2015年的數(shù)據(jù),以觀察2015年超出歷史溫度的天數(shù)。
數(shù)據(jù)處理:
在此數(shù)據(jù)集中,共有4017個觀測值,其中包括'2012-02-29','2008-02-29' 這兩個閏年的獨(dú)有日期。考慮到我們的分析目的,可以去掉這兩個觀測值,因此僅有4015個觀測值。
df_2015=df[df['Date']>='2015-01-01']
df_2005=df[df['Date']<'2015-01-01'] #分割數(shù)據(jù)
df_2015['Month-Date']=df_2015.apply(lambda row: "-".join(row["Date"].split("-")[1:]), axis=1)
df_2005['Month-Date']=df_2005.apply(lambda row: '-'.join(row["Date"].split("-")[1:]), axis=1) #修改日期格式
dfmax=df_2005[df_2005['Element']=='TMAX'].groupby('Month-Date').max()
dfmin=df_2005[df_2005['Element']=='TMIN'].groupby('Month-Date').min()
dfmax_2015=df_2015[df_2015['Element']=='TMAX'].groupby('Month-Date').max()
dfmin_2015=df_2015[df_2015['Element']=='TMIN'].groupby('Month-Date').min() #找到日期對應(yīng)的歷年最低和最高氣溫
new_df_min['Data_Value'],new_df_min['Data_Value1']=new_df_min['Data_Value']/10,new_df_min['Data_Value1']/10
new_df_max['Data_Value'],new_df_max['Data_Value1']=new_df_max['Data_Value']/10,new_df_max['Data_Value1']/10
dfmax_2015.columns=['ID1','Date1','Element1','Data_Value1']
new_df_max=dfmax.join(dfmax_2015)
dfmin_2015.columns=['ID1','Date1','Element1','Data_Value1']
new_df_min=dfmin.join(dfmin_2015)
new_df_max.head()
處理后的數(shù)據(jù)如下:
最高溫度數(shù)據(jù):

最低溫度數(shù)據(jù):

new_df_min["Data_Value1"] = new_df_min.apply(lambda row: row["Data_Value1"] if (row["Data_Value1"] < row["Data_Value"]) else np.NaN , axis=1)
new_df_max["Data_Value1"] = new_df_max.apply(lambda row: row["Data_Value1"] if (row["Data_Value1"] > row["Data_Value"]) else np.NaN , axis=1) #找出2015年中破紀(jì)錄的數(shù)值與日期,其余的作為nan值處理
Matplotlib使用:
fig, ax = plt.subplots();
fmt = mdates.DateFormatter('%m-%d');
ax.xaxis.set_major_formatter(fmt);
ax.plot(new_df_max['Month-Date'],new_df_max["Data_Value"],label="High",alpha=0.25);
ax.plot(new_df_min['Month-Date'],new_df_min["Data_Value"], label="Low",alpha=0.25);? #生成折線圖
ax.fill_between(new_df_min['Month-Date'].values, new_df_min["Data_Value"].values,new_df_max["Data_Value"].values, facecolor='grey',alpha=0.25); #進(jìn)行填充
ax.scatter(new_df_max['Month-Date'].values, new_df_max["Data_Value1"].values, s=10, c="blue", alpha=0.8, label="High (2015)");
ax.scatter(new_df_min['Month-Date'].values, new_df_min["Data_Value1"].values, s=10, c="red", alpha=0.8, label="Low (2015)"); #生成散點(diǎn)圖
plt.xlabel('Date');
plt.ylabel('Temperature,Degrees');
plt.title('Daliy Maximum and Minimum Temperature, Ann Arbor, Michigan, United States');
ax.tick_params(top='off', bottom='on', left='off', right='off', labelleft='on', labelbottom='on'); #設(shè)置坐標(biāo)格式
ax.spines['top'].set_visible(False);
ax.spines['right'].set_visible(False);
plt.legend();
plt.legend(loc=4, frameon=False);
