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

预先感谢您的帮助!(下面的代码)(链接到第一个数据)(链接到我要添加的数据

我正在尝试从第二个 CSV(上图)导入数据,并根据该 CSV 数据向该图添加第二行。执行此操作的最佳方法是什么?(下图)

图上的波浪线代表数据的范围。

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import warnings

warnings.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.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']


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

我拥有的:

https://img2.mukewang.com/651278f00001b0eb03940282.jpg

我想要拥有什么:

https://img1.mukewang.com/651278fb000165ee03930281.jpg


隔江千里
浏览 76回答 1
1回答

料青山看我应如是

查看新代码的内联符号删除是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 dataat = pd.read_csv('data/so_data/2020-09-29 64128817/allStationsDailyAirTemp1.csv')# set Date to a datetime formatat.Date = pd.to_datetime(at.Date)# extract day of yearat['doy'] = at.Date.dt.dayofyear# selet data from Minotat = at[at.Station == 'Minot']# groupby the day of year (doy) and aggregate min max and meanatg = 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 warningdf_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']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=bxatg['mean'].plot(ax=bx, label='Mean Air Temp')# add air temp fill between plot to the bx plotbx.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))# gridbx.grid()# set legend locationbx.legend(bbox_to_anchor=(1.05, 1), loc='upper left')# remove margin spacesplt.margins(0, 0)plt.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python