Tuesday, February 9, 2010

Area plots and filling in with matplotlib



A question on StackOverflow (here) prompted me to look into the function fill_between in matplotlib. One good use of this would be when we have some factor (say bacterial Phyla) that vary in proportions in different samples (say, sequences collected from different sites along the colon). I believe that R calls these "area plots." Another use we've seen before, to fill in the area below a curve.

[UPDATE: I forgot to normalize the values. This only came out right because the sum is < 1 for all y1, y2 values. You know what to do.]



Code:

import numpy as np
from pylab import *
np.random.seed(153)

N = 5
x = np.arange(0,N)
y1 = np.random.rand(N)
y2 = y1 + np.random.rand(N)
y3 = np.array([1]*N)
plot(x,y1)
plot(x,y2)

fill_between(x,y1,0,color='cyan')
fill_between(x,y2,y1,color='magenta')
fill_between(x,y3,y2,color='red')
show()

x = np.arange(0,N,0.01)
y4 = np.sqrt(x)
plot(x,y4,color='k',lw=2)
fill_between(x,y4,0,color='0.8')
show()