UPDATE: I've got something to work (see below). I would appreciate comments
on whether it is 'pythonic' and the most efficient. Also, what would I have
to do to have a more OO approach? I would prefer to not use 'pyplot' in
general.
#!/usr/bin/env python 

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt

def plot_fill_between(array=None):
    """ plot with fill_between for a cumsum vector """

    # Set up plotting environment
    fig = plt.figure()
    ax = fig.gca()
    nx = 40
    ny = 20
    if array is None:
        X = range(nx)
        array = np.random.rand(nx,ny)
    
    array = np.cumsum(array,axis=1)
        
    Nc = np.array([float(i)/ny for i in range(ny)])
    norm = mpl.colors.normalize(Nc.min(),Nc.max())
    jet = plt.cm.get_cmap('jet')
    facecolors=[]
    for i in range(ny-1):
        if i == 0:
            facecolors.append(jet(norm(Nc[i])))
            plt.fill_between(X,np.zeros(len(array[:,i])),array[:,i],
                             color=facecolors[-1],label='%s' % i)
        facecolors.append(jet(norm(Nc[i+1])))
        plt.fill_between(X,array[:,i],array[:,i+1],
                         color=facecolors[-1],label='%s' % i)
    fig.autofmt_xdate()

    
    plt.title('Fill Between Demo')
    ## ListedColormap
    pos = ax.get_position()
    l, b, w, h = getattr(pos, 'bounds', pos)

    ax2 = fig.add_axes([l,.09,w,0.03])
    cmap = mpl.colors.ListedColormap(facecolors)
    bounds = range(len(facecolors)+1)
    cb2 = mpl.colorbar.ColorbarBase(ax2,
                                    cmap=cmap,
                                    boundaries=bounds,
                                    ticks=bounds, # optional
                                    spacing='proportional',
                                    orientation='horizontal')
    cb2.set_label('(units)',size='x-small')


    return fig

if __name__ == "__main__":
    fig = plot_fill_between()
    plt.show()
-- 
View this message in context: 
http://www.nabble.com/help-with-fill_between-and-a-colorbar-tp25882693p25887843.html
Sent from the matplotlib - users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to