How to place an image colorbar next to the image when there are several subplots ?

I apologize for asking such a trivial question, but I’ve spent a long time trying to fix this:

I have a large 2D array that displays as an image, with a colorbar on the side.

I also display 2 curves on top of the image. i.e. in same axes.

The following code does it:

fig,ax = plt.subplots()

cax = ax.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\

interpolation=‘nearest’)

ax.set_title(“Dummy title”)

Add colorbar

cbar = fig.colorbar(cax)

ax.set_ylabel(‘mV’)

ax.plot(emtrate, emrate, ‘.r’) #curve 1

ax.plot(tt, rate*50 - 25.0, ‘-k’, linewidth=3) #curve 2

plt.show()

···

========

IN FACT,

I want the curves in separate axes, below the image while the colorbar remains immediately to the right

of the image.

I’ve tried many minor variations, for way over an hour…

I’ve looked at demos, read about colorbar in several different parts of matplotlib docs…

Can someone help?? … Either the colorbar is next to the last plot, or else I

get an error.

Here is code that I’ve tried: It puts the colorbar in the wrong place, and in addition the image size

is very small while the ax2 and ax3 curve plots are much wider.

I want the image and the second 2 plots the same width.

fig, (ax1, ax2,ax3) = plt.subplots(3,1)

cax = ax1.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\

interpolation=‘nearest’)

ax1.set_title(“Dummy title”)

Add colorbar

cbar = fig.colorbar(cax) # this places the colorbar next to the third subplot

plt.colorbar(cax) # so does this

ax1.set_ylabel(‘mV’)

ax2.plot(emtrate, emrate, ‘.r’)

ax3.plot(tt, rate*50 - 25.0, ‘-k’, linewidth=3)

plt.show()

Thanks for any help,

Peter

Save our in-boxes! http://emailcharter.org

cax is not the Axes containing the image, but the image itself. It seems
that you want to "steal" some space to the first subplot so:

        fig, (ax1, ax2,ax3) = plt.subplots(3,1)
        im = ax1.imshow(bighistT, extent=myextent, cmap=cm.coolwarm,
            aspect=myaspect, interpolation='nearest')
        ax1.set_title("Dummy title")
        cbar = fig.colorbar(im, ax=ax1)

or you can also create a new Axes between ax1 and ax2, and tell
colorbar() to put the Colorbar into (with the cax keyword argument)

See
http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar

···

Le jeudi 19 février 2015 à 23:10 -0800, Peter Rowat a écrit :

I apologize for asking such a trivial question, but I’ve spent a long time trying to fix this:

I have a large 2D array that displays as an image, with a colorbar on the side.
I also display 2 curves on top of the image. i.e. in same axes.
The following code does it:

    fig,ax = plt.subplots()
    cax = ax.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\
        interpolation='nearest')
    ax.set_title("Dummy title")
    # Add colorbar
    cbar = fig.colorbar(cax)
    ax.set_ylabel('mV')

    ax.plot(emtrate, emrate, '.r') #curve 1
    ax.plot(tt, rate*50 - 25.0, '-k', linewidth=3) #curve 2

    plt.show()

IN FACT,
I want the curves in separate axes, below the image while the colorbar remains immediately to the right
of the image.

I've tried many minor variations, for way over an hour..
I've looked at demos, read about colorbar in several different parts of matplotlib docs...

Can someone help?? .... Either the colorbar is next to the last plot, or else I
get an error.

Here is code that I've tried: It puts the colorbar in the wrong place, and in addition the image size
is very small while the ax2 and ax3 curve plots are much wider.
I want the image and the second 2 plots the same width.

    fig, (ax1, ax2,ax3) = plt.subplots(3,1)
    cax = ax1.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\
        interpolation='nearest')
    ax1.set_title("Dummy title")
    # Add colorbar
# cbar = fig.colorbar(cax) # this places the colorbar next to the third subplot
    plt.colorbar(cax) # so does this

--
Fabrice

Peter,

I think that you want
cax = ax1.imshow(…)
cbar = fig.colorbar(cax,ax=ax1) # Where the ax keyword tells the colorbar which axes to steal space from. [1]

If you want the colorbar to be to the right of the first axes, and have the second and third axes line up with the first, then you need to create your own axes instance [2] {cbarax = fig.add_axes([…]) } for the colorbar and give that instance to the cax keyword of fig.colorbar (without an ax keyword). [1]

Also, I wouldn’t refer to the object returned by imshow as cax, as that is confusing when looking at the possible arguments of fig.colorbar. I would call the image object im or something similar.

-Sterling

[1] http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar
[2] http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.add_axes

···

On Feb 19, 2015, at 11:10PM, Peter Rowat <peter@...4612...> wrote:

I apologize for asking such a trivial question, but I’ve spent a long time trying to fix this:

I have a large 2D array that displays as an image, with a colorbar on the side.
I also display 2 curves on top of the image. i.e. in same axes.
The following code does it:

    fig,ax = plt.subplots()
    cax = ax.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\
        interpolation='nearest')
    ax.set_title("Dummy title")
    # Add colorbar
    cbar = fig.colorbar(cax)
    ax.set_ylabel('mV')

    ax.plot(emtrate, emrate, '.r') #curve 1
    ax.plot(tt, rate*50 - 25.0, '-k', linewidth=3) #curve 2

    plt.show()

IN FACT,
I want the curves in separate axes, below the image while the colorbar remains immediately to the right
of the image.

I've tried many minor variations, for way over an hour..
I've looked at demos, read about colorbar in several different parts of matplotlib docs...

Can someone help?? .... Either the colorbar is next to the last plot, or else I
get an error.

Here is code that I've tried: It puts the colorbar in the wrong place, and in addition the image size
is very small while the ax2 and ax3 curve plots are much wider.
I want the image and the second 2 plots the same width.

    fig, (ax1, ax2,ax3) = plt.subplots(3,1)
    cax = ax1.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\
        interpolation='nearest')
    ax1.set_title("Dummy title")
    # Add colorbar
# cbar = fig.colorbar(cax) # this places the colorbar next to the third subplot
    plt.colorbar(cax) # so does this
    ax1.set_ylabel('mV')
    ax2.plot(emtrate, emrate, '.r')
    ax3.plot(tt, rate*50 - 25.0, '-k', linewidth=3)

    plt.show()
Thanks for any help,

Peter
Save our in-boxes! http://emailcharter.org

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=190641631&iu=/4140/ostg.clktrk_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users