【问题标题】:Argument dimensions are incompatible on matplotlib line chartmatplotlib 折线图上的参数尺寸不兼容
【发布时间】:2019-01-02 16:53:28
【问题描述】:

尝试对两个折线图之间的区域进行着色但出现以下错误

'参数维度不兼容'

我曾尝试查看类似的问题,但似乎无法找到适合我的解决方案。

df['Date'] = pd.DatetimeIndex(df['Date']).year
df = df[df['Date'] != 2015]
df_min = df[df['Element'] =='TMIN'].groupby('Date').aggregate({'Data_Value':np.average})
df_max = df[df['Element'] == 'TMAX'].groupby('Date').aggregate({'Data_Value':np.average})
n = np.array(len(df_min))
x = np.linspace(0,n,n,endpoint=True)
plt.plot(df_max, '-o',df_min,'-o')
plt.fill_between(x,df_min,df_max, color='grey',alpha='0.5')
plt.show()

我所指的数据库链接:Database

【问题讨论】:

  • 你检查xdf_mindf_max的形状是否相同?
  • @cheersmate df_min & df_max = (10,1), x 是 (10,)

标签: python matplotlib


【解决方案1】:

您的代码中有两个问题:

  • 您的 x 基本上是一个长度为 0 的整数,而您需要传递与您要填充的 x ​​比例范围(在您的情况下为年)相对应的值数组。
  • 您的df_mindf_max 是DataFrame,而您应该传递的是对应于df_mindf_max 的y 值。这些可以使用df_min.valuesdf_max.values 访问。但是,这是一个或多个数组。然后你需要flatten 它来获取一维数组才能使用fill_between

以下是一种解决方案:

x = df_max.index.values

plt.plot(df_max, '-o',df_min,'-o')
plt.fill_between(x,df_min.values.flatten(),df_max.values.flatten(), color='grey',alpha='0.5')

【讨论】:

    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多