1

I used agg to find mean and min values grouped by age. Then tried to plot area filled line graph.

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

My mean values output :

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

I'm getting the following error

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

How to solve this?

Heisenberg
  • 13
  • 4

1 Answers1

0

As plt.fill_between() documentation reports, this function need at least two parameters:

xarray (length N)
The x coordinates of the nodes defining the curves.

y1array (length N) or scalar
The y coordinates of the nodes defining the first curve.

The area you want to fill is defined by two functions f1 and f2, you need to pass to plt.fill_between():

  1. the array of the x coordinates of the points of f1
  2. the array of the y coordinates of the points of f1
  3. (optionally) the array of the y coordinates of the points of f2. If you do not specify f2, plt.fill_between() will automatically use the x axis in its place.

In your code,

plt.fill_between(mean_values,alpha = 0.25)

you are specifying mean_values only, which matplotlib interprets how xarray, so it requires you for y1, as the error reports.
Check these answers as a reference:

Zephyr
  • 11,891
  • 53
  • 45
  • 80