python - Fill between y = 0 and positive values of y -


i'm practicing fill_between matplotlib function.

what want fill area between y = 0 , positive values of y , let area between y = 0 , negative values of y unfilled.

something like:

import matplotlib.pyplot plt  x = [1,2,3,4,5] y = [0,2,-3,4,-5]  ypos = [i in y if i>0]  plt.plot(x,y) plt.fill_between(x,0,ypos) plt.show() 

the error gives valueerror: argument dimensions incompatible. i've checked possible solutions couldn't working.

you use

where=np.array(y)>0 

to restrict filled region drawn , use

interpolate=true 

to have fill_between find points of intersection:


import numpy np import matplotlib.pyplot plt  x = np.array([1,2,3,4,5]) y = np.array([0,2,-3,4,-5]) plt.plot(x,y) plt.fill_between(x, 0, y, where=y>0, interpolate=true) plt.show() 

yields

enter image description here


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -