Area charts

Tom Denniston <tom.denniston@...660...> writes:

Is there any easy way to make an area chart like the one attached in
matplotlib without manually manipulating polygons?

Here's a snippet that makes a plot like that (but I haven't figured
out how to create the legend). To test it in ipython -pylab, type
something like

figure()
plot_cumulative(gca(), arange(20), rand(20,20))
show()

Perhaps a more refined version of this would be a useful addition to
matplotlib?

import Numeric as N
import matplotlib

# Colors from http://geography.uoregon.edu/datagraphics/color_scales.htm
colors = [
[1.000, 0.500, 0.000],
[1.000, 0.750, 0.500],
[1.000, 1.000, 0.200],
[1.000, 1.000, 0.600],
[0.200, 1.000, 0.000],
[0.700, 1.000, 0.550],
[0.100, 0.700, 1.000],
[0.650, 0.930, 1.000],
[0.400, 0.300, 1.000],
[0.800, 0.750, 1.000],
[0.900, 0.100, 0.200],
[1.000, 0.600, 0.750],
]

def plot_cumulative(ax, x, array):
    x, array = N.asarray(x), N.asarray(array)
    # Change e.g. array=[[1,2,3],[4,5,6]] to cs=[[0,0,0],[1,2,3],[5,7,9]]]
    cs = N.concatenate((N.zeros((1,len(x))), N.cumsum(array)))
    # Change e.g. x=[1,2,3] to xx=[1,2,3,3,2,1]
    xx = N.concatenate((x, x[::-1]))
    for i in range(0, N.size(cs,0)-1):
        # Define y coordinates of polygon as cs[i,:] and cs[i+1,:]
        yy = N.concatenate((cs[i,:], cs[i+1,::-1]))
        c = colors[i % len(colors)]
        ax.fill(xx, yy, facecolor=c)

···

--
Jouni