【问题标题】:Use of fill_between with pandadataframe, need help i get the following error ValueError: Argument dimensions are incompatible将fill_between与pandadataframe一起使用,需要帮助我得到以下错误ValueError:参数尺寸不兼容
【发布时间】: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


    【解决方案1】:

    引起怀疑的一个细节是:

    • 作为 x 轴,您使用 whole DataFrame 中的 Year 列,
    • 但源到y轴你使用分组的结果,所以数 元素的数量小于 x 轴的来源。

    还引起怀疑的是,您的分组位于 两个 列上。

    所以,总结一下,检查 x 轴上有多少元素 并在 y 轴(要绘制的曲线)的两个来源中。

    同时指出哪一行导致了上述异常。 我想它比提到的fill_between更早发生。

    最后一句话: 您展示了一段代码如何生成 DataFrame, 但如果没有源数据,我们仍然无法复制您的计算。 你最好提供Max_Plot2005_2014的内容, Min_Plot2005_2014d (实际上,较小的样本,但导致相同 例外)。

    【讨论】:

    • 谢谢,根据您的反馈,我能够解决问题
    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多