colorbar settings with a object oriented approach (or mixed pyplot)

Folks, I was trying to use an object oriented approach to creating
colorbars, but I could manage to make it work. Currently I have close
to what I am trying to achieve, but the last bits are missing. The
outstanding issues are:

1) I only need one colorbar, how would I create a single colorbar on
the right that spanned across all axes? (ie. same height as the stack)
2) is there a way to place the colorbar in the bottom middle of my
panels (if I end up with more than one)?
3) How can I customize the tick labels of the colorbar?
4) Is this a 'pythonic' approach to begin with?

Here's my code, and a fake class that should provide the data needed
to use the example:

#!/usr/bin/env python

"""
Demo to help understand plotting tools
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

class Fun(object):
    """ Dummy class for testing
    """

    class body:
        pass

    def __init__(self):

        body = Fun.body
        body.time = np.arange(1000)
        body.alt = np.sin(body.time)
        body.A = 360*np.random.random_sample(1000)-180
        body.B = 180*np.random.random_sample(1000)-90
        body.C = 90*np.random.random_sample(1000)-45

def plot_angles(F):
    """ Plots the angles of body """

    from mpl_toolkits.axes_grid1.inset_locator import inset_axes

    # Set up plotting environment colors
    Nc = np.arange(-180,181,1)
    norm = mpl.colors.normalize(Nc.min(),Nc.max())

    fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)

    ax1.scatter(F.body.time,F.body.alt,c=F.body.A,norm=norm,label='A',edgecolor='none')

    ax2.scatter(F.body.time,F.body.alt,c=F.body.B,norm=norm,label='B',edgecolor='none')

    ax3.scatter(F.body.time,F.body.alt,c=F.body.C,norm=norm,label='C',edgecolor='none')

    # Fine-tune figure; make subplots close to each other and hide x ticks for
    # all but bottom plot.
    fig.subplots_adjust(hspace=0)
    plt.setp([a.get_xticklabels() for a in fig.axes[:-1]], visible=False)
    for a in fig.axes:
        ia = inset_axes(a,width="20%", height="5%", loc=4)
        a.set_xlim(F.body.time[0],F.body.time[-1])
        fig.add_subplot(a)
        plt.colorbar(a.collections[0],cax=ia,orientation='horizontal')
        ia.xaxis.set_ticks_position("top")

    plt.draw()

    return fig

def demo():

    F = Fun()
    plot_angles(F)

if __name__ == "__main__":
    fig = demo()
    plt.show()

1) I only need one colorbar, how would I create a single colorbar on
the right that spanned across all axes? (ie. same height as the stack)

There are a few options you can try. I guess the easiest way is
setting up the axes manually.

dy = 0.8/3.
ax1 = axes([0.1, 0.1, 0.8, dy])
ax2 = axes([0.1, 0.1+dy, 0.8, dy])
ax3 = axes([0.1, 0.1+2*dy, 0.8, dy])

sc = ax1.scatter([1,2],[3,4], c=[0,1], cmap="jet")
cax = axes([0.9, 0.1, 0.03, 0.8])
plt.colorbar(sc, cax=cax)

Also, you make take a look at the axes_grid1 toolkit.
(see e.g, http://matplotlib.sourceforge.net/examples/axes_grid/demo_axes_grid.html)

2) is there a way to place the colorbar in the bottom middle of my
panels (if I end up with more than one)?

Use loc=8.

3) How can I customize the tick labels of the colorbar?

You may use "set_ticks" and "set_ticklabels" method of the colorbar object.

       cb = plt.colorbar(a.collections[0],cax=ia,orientation='horizontal')
       cb.set_ticks([-100,0,100])
       cb.set_ticklabels(["-100", "0", "+100"])

Also, locators and formatter can be specified during colorbar creation
(take a look at the doc).

-JJ

···

On Thu, Nov 18, 2010 at 11:10 PM, John <washakie@...287...> wrote: