help with fill_between and a colorbar

Hello, I have the following function as an example. You can see I started to play with one of the demos to put a colorbar on it. I’m a little confused how to make this work, as on my figure I have several ‘collections’. The other examples I have seen deal with one collection. Maybe someone could show how to make a color bar here??

Thanks!

#!/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()

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’)

for i in range(ny-1):

cmap = jet(norm(Nc[i]))

if i == 0:

plt.fill_between(X,np.zeros(len(array[:,i])),array[:,i],color=cmap,label=’%s’ % i)

plt.fill_between(X,array[:,i],array[:,i+1],color=cmap,label=’%s’ % i)

plt.title(‘Fill Between Demo’)

ListedColormap

#ax2 = fig.add_axes([0.15, 0.05, 0.75, 0.05])

#cmap = mpl.colors.ListedColormap(range(H.numageclasses))

#cmap.set_over(‘0.25’)

#cmap.set_under(‘0.75’)

If a ListedColormap is used, the length of the bounds array must be

one greater than the length of the color list. The bounds must be

monotonically increasing.

#bounds = range(H.numageclasses+1)

#norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

##cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,

norm=norm,

# to use ‘extend’, you must

# specify two extra boundaries:

boundaries=[0]+bounds+[13],

extend=‘both’,

ticks=bounds, # optional

spacing=‘proportional’,

orientation=‘horizontal’)

##cb2.set_label(‘Units’)

return fig

if name == “main”:

fig = plot_fill_between()

plt.show()

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.