On Tue, Jan 17, 2012 at 12:37 PM, Michael Rawlins <rawlin...@yahoo.com>wrote:

>
>
>   ------------------------------
> *From:* Benjamin Root <ben.r...@ou.edu>
> *To:* Michael Rawlins <rawlin...@yahoo.com>
> *Cc:* "matplotlib-users@lists.sourceforge.net" <
> matplotlib-users@lists.sourceforge.net>
> *Sent:* Tuesday, January 17, 2012 10:36 AM
> *Subject:* Re: [Matplotlib-users] placing colorbar when using subplot
> command
>
>
> On Tue, Jan 17, 2012 at 9:30 AM, Michael Rawlins <rawlin...@yahoo.com>wrote:
>
>
> I'm relatively new to matplotlib. Trying to place a colorbar in a figure.
> The code below, placed in a file and executed with python, draws 4 maps
> using basemap. I've been unable to get a colorbar to show up anywhere on
> the figure. Ideally I would like the option of placing a colorbar across
> the bottom, spanning across both bottom map panels.  Also would need the
> option of placing a colorbar either to the right of or below each map.
> Uncommenting the two lines under "Here make a colorbar" cause an error.
> I've used those commands when creating just one map using the figure
> command.
>
> TIA,
> Mike
>
>
>
> Mike,
>
> Try using the axes_grid1 toolkit to produce your axes objects and to
> allocate enough room for colorbars.
>
> http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html
>
> Cheers!
> Ben Root
>
> P.S. - a little history, there used to be an axes_grid toolkit, but has
> since been superseded by axes_grid1.
>
>
> Clicking on source code on that page produces an error. For several of the
> graphic on that page, dropping the code into a file and running also
> produces various errors. Being new to this software, having a specific
> example that I can run and then incorporate into my code would be a big
> help.
>
> Mike
>
>
>
Mike,

I do apologize for that.  We will have to get that fixed on the website
(not sure why it is happening).  I have attached an example file for you to
try.  Also, which version of matplotlib are you running?  Without the error
message you are getting, it would be hard to tell you what is wrong (most
likely it is a version issue).

Ben Root
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid

def get_demo_image():
    import numpy as np
    from matplotlib.cbook import get_sample_data
    f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
    z = np.load(f)
    # z is a numpy array of 15x15
    return z, (-3,4,-4,3)

def demo_simple_grid(fig):
    """
    A grid of 2x2 images with 0.05 inch pad between images and only
    the lower-left axes is labeled.
    """
    grid = AxesGrid(fig, 131, # similar to subplot(131)
                    nrows_ncols = (2, 2),
                    axes_pad = 0.05,
                    label_mode = "1",
                    )

    Z, extent = get_demo_image()
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, interpolation="nearest")

    # This only affects axes in first column and second row as share_all = False.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])


def demo_grid_with_single_cbar(fig):
    """
    A grid of 2x2 images with a single colorbar
    """
    grid = AxesGrid(fig, 132, # similar to subplot(132)
                    nrows_ncols = (2, 2),
                    axes_pad = 0.0,
                    share_all=True,
                    label_mode = "L",
                    cbar_location = "top",
                    cbar_mode="single",
                    )

    Z, extent = get_demo_image()
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
    #plt.colorbar(im, cax = grid.cbar_axes[0])
    grid.cbar_axes[0].colorbar(im)

    for cax in grid.cbar_axes:
        cax.toggle_label(False)
        
    # This affects all axes as share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])


def demo_grid_with_each_cbar(fig):
    """
    A grid of 2x2 images. Each image has its own colorbar.
    """

    grid = AxesGrid(F, 133, # similar to subplot(122)
                    nrows_ncols = (2, 2),
                    axes_pad = 0.1,
                    label_mode = "1",
                    share_all = True,
                    cbar_location="top",
                    cbar_mode="each",
                    cbar_size="7%",
                    cbar_pad="2%",
                    )
    Z, extent = get_demo_image()
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
        grid.cbar_axes[i].colorbar(im)

    for cax in grid.cbar_axes:
        cax.toggle_label(False)

    # This affects all axes because we set share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])



if 1:
    F = plt.figure(1, (5.5, 2.5))

    F.subplots_adjust(left=0.05, right=0.98)

    demo_simple_grid(F)
    demo_grid_with_single_cbar(F)
    demo_grid_with_each_cbar(F)

    plt.draw()
    plt.show()

------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to