Home > Net >  label argument in plt.fill_between not working
label argument in plt.fill_between not working

Time:06-20

I am working with line plots using:

x = np.arange(25,55,5)
y = np.random.randint(10000,50000,6)
y2 = np.random.randint(10000,60000,6)

Then the main plotting code is:

plt.style.use('fivethirtyeight')


plt.xlabel("some random ages")
plt.ylabel('some random salaries')
plt.title('whatever')
plt.legend(['first','second'])

plt.plot(x,y)
plt.plot(x,y2) 



plt.fill_between(x,y,color = 'blue', alpha = 0.25, where = (y>np.median(y)), interpolate =True, 
                 label = 'above median') 
#Colour blue for salaries more than the median.


plt.fill_between(x,y,color = 'red', alpha = 0.25, where = (y<=np.median(y)), interpolate =True, 
                 label = 'below median')
#Colour red for salaries less than the median.


plt.show()

The filling is working as intended, however the plot legends only have the entries specified in plt.legend(). The label argument, which seems to work in the tutorial video I am watching as well as every other site I have seen, does not add the intended labels to the legends. I had the same problem in stack plots, where the tutor, who is using pycharm, used labels to add legend to the satckplot, and I had to use plt.legend because the labels command seemed to do nothing. However here I don't know how to add the legends for the fill colours along with the legends for the main line plots.

I am using the latest version of jupyter notebook and latest python version on macOS Monterey 12.4.

CodePudding user response:

Either specify your labels in all places:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
x = np.arange(25,55,5)
y = np.random.randint(10000,50000,6)
y2 = np.random.randint(10000,60000,6)

plt.style.use('fivethirtyeight')

plt.plot(x,y, label='red')
plt.plot(x,y2, label='blue') 

#Colour blue for salaries more than the median.
plt.fill_between(x,y,color = 'blue', alpha = 0.25, where = (y>np.median(y)), interpolate =True, 
                 label = 'above median') 


#Colour red for salaries less than the median.
plt.fill_between(x,y,color = 'red', alpha = 0.25, where = (y<=np.median(y)), interpolate =True, 
                 label = 'below median')

plt.xlabel("some random ages")
plt.ylabel('some random salaries')
plt.title('whatever')
plt.legend()

plt.show()

or specify all labels in the legend parameters:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
x = np.arange(25,55,5)
y = np.random.randint(10000,50000,6)
y2 = np.random.randint(10000,60000,6)

plt.style.use('fivethirtyeight')

plt.plot(x,y)
plt.plot(x,y2) 

#Colour blue for salaries more than the median.
plt.fill_between(x,y,color = 'blue', alpha = 0.25, where = (y>np.median(y)), interpolate =True) 

#Colour red for salaries less than the median.
plt.fill_between(x,y,color = 'red', alpha = 0.25, where = (y<=np.median(y)), interpolate =True)

plt.xlabel("some random ages")
plt.ylabel('some random salaries')
plt.title('whatever')
plt.legend(['red', 'blue', 'above', 'below'])

plt.show()

enter image description here

  • Related