deprecated Warning - where parameter in mataplotlib library

Viewed 171
Ages = [19,20,21,22,23,24,25,26,27]

python = [23000,30000,40000,50000,60000,70000,80000,90000,100000]
Java  = [15000,20000,25000,35000,40000,50000,90000,100000,300000]


pl.plot(Ages,python,label="Python developer") 
pl.plot(Ages,Java,label="Java developer") 

pl.fill_between(Ages,python,Java,where= (python > Java), alpha=0.5, color="blue" , label="greater than")
pl.fill_between(Ages,python,Java,where= (python < Java), alpha=0.5, color="red" , label="less than")


pl.title("Fill area on line plots")   
pl.legend(loc="upper left")
pl.show()

I am getting deprecation warning & also i didnt get the red margin when python value less than Java value.. Below i have mentioned error & screenshot of output...

Error:

matplot.py:143: MatplotlibDeprecationWarning: The parameter where must have the same size as x in fill_between(). This will become an error in future versions of Matplotlib.
  pl.fill_between(Ages,python,Java,where= (python > Java), alpha=0.5, color="blue" , label="greater than")
matplot.py:144: MatplotlibDeprecationWarning: The parameter where must have the same size as x in fill_between(). This will become an error in future versions of Matplotlib.
  pl.fill_between(Ages,python,Java,where= (python < Java), alpha=0.5, color="red" , label="less than")

Output:

Output screenshot

1 Answers

This deprecation warning is mainly to pinpoint that the where parameter doesn't have the same number of arguments as the other arrays. Usually it hints at unwanted behavior. Before, such error was ignored.

The where parameter is expected to have one True/False value for each point on the plot.

In this case, the expression python < Java has just one single value, as both are regular Python lists. To obtain a list of individual boolean values, you either need to create that list more manually [p < j for p, j in zip(python, Java)]. Or change everything to numpy arrays, and let its vectorization and broadcasting take care of creating the array of boolean values.

Note that you need an extra parameter, interpolate=True to handle the line segments where the value for one point is True while the value for the other is False.

Here is the code with the lists changed to numpy arrays (python > Java then also returns an array) and interpolate=True set.

import matplotlib.pyplot as plt
import numpy as np

Ages = np.array([19, 20, 21, 22, 23, 24, 25, 26, 27])

python = np.array([23000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000])
Java = np.array([15000, 20000, 25000, 35000, 40000, 50000, 90000, 100000, 300000])

plt.plot(Ages, python, label="Python developer")
plt.plot(Ages, Java, label="Java developer")

plt.fill_between(Ages, python, Java, where=(python > Java), interpolate=True,
                 alpha=0.5, color="blue", label="greater than")
plt.fill_between(Ages, python, Java, where=(python < Java), interpolate=True,
                 alpha=0.5, color="red", label="less than")
plt.title("Fill area on line plots")
plt.legend(loc="upper left")
plt.show()

resulting plot

Related