【发布时间】:2020-09-01 05:06:28
【问题描述】:
我正在尝试使用 fill_between 填充记录高温和记录低温之间的区域,下面是我的代码,但是当我尝试使用 d、Max_Plot2005_2014_array、Min_Plot2005_2014_array 填充该区域时出现错误。我是 python 新手,所以我不知道我可能做错了什么。 感谢您的建议!
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import calendar
from datetime import datetime
ax = plt.gca()
pd.set_option('display.max_rows', None)
df= pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv')
df.rename(columns={'Data_Value':'Temp_Celcius'}, inplace=True)
df.rename(columns={'Date':'Year'}, inplace=True)
df['Temp_Celcius']=0.1*df['Temp_Celcius']
df['Year']=pd.to_datetime(df['Year'], format='%Y-%m-%d')
year2005_2014_day29mask= (df['Year'].map(lambda x: x.year) != 2015) & (df['Year'].map(lambda x: x.day) != 29)
year2015_mask=df['Year'].map(lambda x: x.year) == 2015
Plot2005_2014=df[year2005_2014_day29mask]
Plot2005_2014=Plot2005_2014.drop_duplicates(subset=(['Year', 'Temp_Celcius']), keep='first')
Plot2015=df[year2015_mask]
Plot2015=Plot2015.drop_duplicates(subset=(['Year', 'Temp_Celcius']), keep='first')
Max_Plot2005_2014=Plot2005_2014.groupby(['Element','Year']).max()
Min_Plot2005_2014=Plot2005_2014.groupby(['Element','Year']).min()
Max_Plot2005_2014.reset_index().plot(figsize=(15,10), title='High and low temperatures over the period 2005-2014', x ='Year', y='Temp_Celcius', kind = 'line', color='red', ax=ax)
Min_Plot2005_2014.reset_index().plot(figsize=(15,10), title='High and low temperatures over the period 2005-2014', x ='Year', y='Temp_Celcius', kind = 'line', color='blue', ax=ax)
ax.legend(["Temp_Max", "Temp_Min"])
d= Plot2005_2014['Year'].values
Max_Plot2005_2014_array=Max_Plot2005_2014['Temp_Celcius'].values
Min_Plot2005_2014_array=Min_Plot2005_2014['Temp_Celcius'].values
plt.gca().fill_between(d,Max_Plot2005_2014_array,Min_Plot2005_2014_array, facecolor='blue',alpha=0.25)
plt.show()
【问题讨论】:
标签: python pandas numpy dataframe matplotlib