【问题标题】:ERROR :" fill_between() missing 1 required positional argument: 'y1' " while using fill_between in area filled line plot错误:“fill_between() 缺少 1 个必需的位置参数:'y1'”在区域填充线图中使用 fill_between
【发布时间】:2020-07-12 14:50:17
【问题描述】:

我使用 agg 来查找按年龄分组的平均值和最小值。然后尝试绘制区域填充折线图。

grpby = df6.groupby('age')
mean_values = grpby.agg({'hoursperweek':np.mean})
min_values = grpby.agg({'hoursperweek':np.min})


from matplotlib import pyplot as plt
plt.plot(mean_values,label = 'mean')
plt.plot(min_values,label = 'min',linestyle = '--')
plt.fill_between(mean_values,alpha = 0.25)
plt.legend()
plt.title('Mean and min')
plt.xlabel('ages')
plt.ylabel('hrs per week')
plt.show()

我的平均值输出:

     hoursperweek
age              
31      42.877252
32      42.878019
33      42.965714
34      42.937923
35      43.908676
36      43.257238
37      43.706294
...
...

我收到以下错误

    plt.fill_between(mean_values,alpha = 0.25)
TypeError: fill_between() missing 1 required positional argument: 'y1'

如何解决?

【问题讨论】:

    标签: python pandas dataframe matplotlib


    【解决方案1】:

    正如plt.fill_between()documentation报告的那样,这个函数需要至少两个参数:

    xarray(长度 N)
    定义曲线的节点的 x 坐标。

    y1array(长度 N)或标量
    定义第一条曲线的节点的 y 坐标。

    要填充的区域由f1f2两个函数定义,需要传递给plt.fill_between()

    1. f1点的x坐标数组
    2. f1的点的y坐标数组
    3. (可选)f2 的点的 y 坐标数组。如果不指定f2plt.fill_between() 将自动使用 x 轴代替。

    在您的代码中,

    plt.fill_between(mean_values,alpha = 0.25)

    您仅指定mean_values,matplotlib 解释xarray 的方式,因此它需要您提供y1,因为错误报告。
    检查这些答案作为参考:

    【讨论】:

    • 谢谢!我将数据框分成两个列表(x 和 y 轴)并尝试,它工作!
    猜你喜欢
    • 2023-03-03
    • 2019-10-30
    • 2022-01-14
    • 2020-10-12
    • 2020-08-13
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多