简体   繁体   中英

Matplotlib fill between functionality

I saw the use of fill_between in in one the sentdex tutorial on matplotlib. The code was like

import matplotlib.pyplot as plt

axis=plt.subplot2grid((1,1),(0,0)) #a and b are lists of equal sizes
axis.plot(a,b)
axis.fill_between(a,b,34,where=(b>34),facecolor='r',alpha=.5)

Now his code ran fine and what he got in output was colored face on the portion where values in b were larger than 34. But the same gives error for me :

TypeError: '>' not supported between instances of 'list' and 'int'

I cant find how to use the where functionality

So, I'm following same tutorial and I had same problem. You need to import numpy library and then convert your list to numpy array and then everything will work perfectly.

import matplotlib.pyplot as plt
import numpy

b = numpy.array(b)

axis=plt.subplot2grid((1,1),(0,0)) #a and b are lists of equal sizes
axis.plot(a,b)
axis.fill_between(a,b,34,where=(b>34),facecolor='r',alpha=.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM