Matplotlib 与数据可视化--Plotting Weather Patterns

Coursera 课程 Applied Plotting, Charting & Data Representation in Python

Plotting Weather Patterns

数据来源: 本次使用的是密歇根安娜堡附近地区从2005年到2015年的天气数据,来源于The National Centers for Environmental Information (NCEI) [Daily Global Historical Climatology Network]。其中每一行为一个观测值。

数据变量:

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)

导出数据后可以看到是类似如下的表格:

可视化目标:我们旨在运用Matplotlib制作关于温度的折线图,在图中表示出一年中每一天的从2005年到2014年中的历史最高和最低温度(C)。此外我们还要对比2015年的数据,以观察2015年超出历史温度的天数。

数据处理:

在此数据集中,共有4017个观测值,其中包括'2012-02-29','2008-02-29' 这两个闰年的独有日期。考虑到我们的分析目的,可以去掉这两个观测值,因此仅有4015个观测值。

df_2015=df[df['Date']>='2015-01-01']

df_2005=df[df['Date']<'2015-01-01'] #分割数据

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() #找到日期对应的历年最低和最高气温

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()

处理后的数据如下:

最高温度数据:

最低温度数据:

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年中破纪录的数值与日期,其余的作为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); #进行填充

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)"); #生成散点图

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'); #设置坐标格式

ax.spines['top'].set_visible(False);

ax.spines['right'].set_visible(False);

plt.legend();

plt.legend(loc=4, frameon=False);

你可能感兴趣的:(Matplotlib 与数据可视化--Plotting Weather Patterns)