1 回答
TA贡献1772条经验 获得超8个赞
查看新代码的内联符号
删除是
plt.style.use('ggplot')
因为很难看到fill_between
颜色不要
;
在 python 中使用将其他文件中的数据加载到单独的数据框中
根据需要清理并聚合新数据
将日期列设置为日期时间格式
提取一年中的某一天
groupby
一年中的某一天和总计mean
,min
, 和max
温度
将新数据绘制为
axes
与原始图相同的图,bx
。
df_all_stations = pd.read_csv('data/so_data/2020-09-29 64128817/all-deep-soil-temperatures.csv', index_col=1, parse_dates=True)
# load air temp data
at = pd.read_csv('data/so_data/2020-09-29 64128817/allStationsDailyAirTemp1.csv')
# set Date to a datetime format
at.Date = pd.to_datetime(at.Date)
# extract day of year
at['doy'] = at.Date.dt.dayofyear
# selet data from Minot
at = at[at.Station == 'Minot']
# groupby the day of year (doy) and aggregate min max and mean
atg = at.groupby('doy')['Temp'].agg([min, max, 'mean'])
selected_soil_station = 'Minot'
df_selected_station = df_all_stations[df_all_stations['Station'] == selected_soil_station].copy() # make a copy here, otherwise there will be warning
df_selected_station.fillna(method = 'ffill', inplace=True)
df_selected_station_D=df_selected_station.resample(rule='D').mean()
df_selected_station_D['Day'] = df_selected_station_D.index.dayofyear
mean=df_selected_station_D.groupby(by='Day').mean()
mean['Day']=mean.index
maxx=df_selected_station_D.groupby(by='Day').max()
minn=df_selected_station_D.groupby(by='Day').min()
mean['maxx20']=maxx['20 cm']
mean['minn20']=minn['20 cm']
bx = mean.plot(x='Day', y='20 cm', color='black', figsize=(9, 6), label='20 cm Soil Temp')
plt.fill_between(mean['Day'], mean['minn20'], mean['maxx20'], color='blue', alpha = 0.2, label='20 cm Soil Temp Range')
# add air temp plot to the bx plot with ax=bx
atg['mean'].plot(ax=bx, label='Mean Air Temp')
# add air temp fill between plot to the bx plot
bx.fill_between(atg.index, atg['min'], atg['max'], color='cyan', alpha = 0.2, label='Air Temp Range')
bx.set_xlabel("Day of the year")
bx.set_ylabel("Temperature in Celsius")
bx.set_title("Soil Temp, Air Temp, and Snow Depth for " + str(selected_soil_station))
# grid
bx.grid()
# set legend location
bx.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
# remove margin spaces
plt.margins(0, 0)
plt.show()
添加回答
举报