为了账号安全,请及时绑定邮箱和手机立即绑定

如何从多个文件绘制最小-最大填充之间图

如何从多个文件绘制最小-最大填充之间图

隔江千里 2023-09-26 14:22:09
预先感谢您的帮助!(下面的代码)(链接到第一个数据)(链接到我要添加的数据)我正在尝试从第二个 CSV(上图)导入数据,并根据该 CSV 数据向该图添加第二行。执行此操作的最佳方法是什么?(下图)图上的波浪线代表数据的范围。import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport warningswarnings.filterwarnings('ignore')raw_data = pd.read_csv('all-deep-soil-temperatures.csv', index_col=1, parse_dates=True)df_all_stations = raw_data.copy()selected_soil_station = 'Minot'df_selected_station = df_all_stations[df_all_stations['Station'] == selected_soil_station]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.dayofyearmean=df_selected_station_D.groupby(by='Day').mean()mean['Day']=mean.indexmaxx=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']plt.style.use('ggplot')bx = mean.plot(x='Day', y='20 cm',color='black')plt.fill_between(mean['Day'],mean['minn20'],mean['maxx20'],color='blue',alpha = 0.2);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))我拥有的:我想要拥有什么:
查看完整描述

1 回答

?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

  • 查看新代码的内联符号

  • 删除是plt.style.use('ggplot')因为很难看到fill_between颜色

  • 不要;在 python 中使用

  • 将其他文件中的数据加载到单独的数据框中

  • 根据需要清理并聚合新数据

    • 将日期列设置为日期时间格式

    • 提取一年中的某一天

    • groupby一年中的某一天和总计meanmin, 和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()

https://img3.sycdn.imooc.com/6512790b0001b02806530341.jpg

查看完整回答
反对 回复 2023-09-26
  • 1 回答
  • 0 关注
  • 51 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信