how to prevent colorbar from taking space from axes

Hi,

I’m trying to make a plot with four panes (2x2). The two on top are images, created with pcolormesh, while the ones on the bottom are line plots. I’d like to the axes to line up – which they do – but when I add a colorbar to the top right, it steals space from the image plot. I only need one colorbar since the two images are forced to have the same scaling. Can anyone tell me how to insert the colorbar on the top right without it taking space from the top right image – or force the space to be taken from the horizontal space of all the plots so the shapes are uniform? I’ve tried several approaches including using make_axes_locatable from mpl_toolkits.axes_grid1 with no success. I must admit that I don’t really understand what make_axes_locatable does, so it may be entirely the wrong thing to use. On the other hand, not using it, i.e. just using plt.colorbar or fig.colorbar seems to do essentially the same thing, steal space from the axes. The basics of the plot are like this:

x = np.arange(20.)

xc = (x[:-1] + x[1:])/2.

y = np.arange(15.)

xx,yy = np.meshgrid(x,y)

z1 = np.random.randn(14,19)

z2 = np.random.randn(14,19)

fig,ax = plt.subplots(2,2)

pc = ax[0,0].pcolormesh(x,y,z1)

ax[1,0].plot(xc,z1[0,:])

ax[0,1].pcolormesh(x,y,z2)

ax[1,1].plot(xc,z2[0,:])

cb = plt.colorbar(pc, ax=ax[0,1])

plt.show()

Any help would be appreciated.

Regards,

Jon

···


Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@…1081… 60 Garden Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
fax: (617) 496-7577 USA


If you have matplotlib v1.3 (maybe 1.2, I can’t remember), you can pass a list of axes to the “ax” argument of colorbar, and it should steal space equally from all the specified axes.

I am also particularly fond of using the mpl_toolkit axis_grid1, which provides you a nice way to preallocate space for colorbars in different ways:
http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html?highlight=axes_grid1#axes-grid1

I hope that helps!
Ben Root

···

On Thu, Dec 11, 2014 at 1:49 PM, Slavin, Jonathan <jslavin@…1081…> wrote:

Hi,

I’m trying to make a plot with four panes (2x2). The two on top are images, created with pcolormesh, while the ones on the bottom are line plots. I’d like to the axes to line up – which they do – but when I add a colorbar to the top right, it steals space from the image plot. I only need one colorbar since the two images are forced to have the same scaling. Can anyone tell me how to insert the colorbar on the top right without it taking space from the top right image – or force the space to be taken from the horizontal space of all the plots so the shapes are uniform? I’ve tried several approaches including using make_axes_locatable from mpl_toolkits.axes_grid1 with no success. I must admit that I don’t really understand what make_axes_locatable does, so it may be entirely the wrong thing to use. On the other hand, not using it, i.e. just using plt.colorbar or fig.colorbar seems to do essentially the same thing, steal space from the axes. The basics of the plot are like this:

x = np.arange(20.)

xc = (x[:-1] + x[1:])/2.

y = np.arange(15.)

xx,yy = np.meshgrid(x,y)

z1 = np.random.randn(14,19)

z2 = np.random.randn(14,19)

fig,ax = plt.subplots(2,2)

pc = ax[0,0].pcolormesh(x,y,z1)

ax[1,0].plot(xc,z1[0,:])

ax[0,1].pcolormesh(x,y,z2)

ax[1,1].plot(xc,z2[0,:])

cb = plt.colorbar(pc, ax=ax[0,1])

plt.show()

Any help would be appreciated.

Regards,

Jon


Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@…1081… 60 Garden Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
fax: (617) 496-7577 USA



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=164703151&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Ben,

Thanks for those tips. I ended up giving a list of axes to the ax argument of colorbar as you suggested. That took care of the misalignment of the top plots and bottom plots but then the colorbar extended the full vertical height of both rows of axes. Fiddling with the shrink and anchor parameter values then got me what I wanted. I’m sure that axis_grid1 could work as well, but my situation is sufficiently complicated that I don’t want to spend the time fiddling with it at this point.

Regards,

Jon

···

On Thu, Dec 11, 2014 at 2:30 PM, Benjamin Root <ben.root@…3176…> wrote:

If you have matplotlib v1.3 (maybe 1.2, I can’t remember), you can pass a list of axes to the “ax” argument of colorbar, and it should steal space equally from all the specified axes.

I am also particularly fond of using the mpl_toolkit axis_grid1, which provides you a nice way to preallocate space for colorbars in different ways:
http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html?highlight=axes_grid1#axes-grid1

I hope that helps!
Ben Root

On Thu, Dec 11, 2014 at 1:49 PM, Slavin, Jonathan <jslavin@…1081…> wrote:

Hi,

I’m trying to make a plot with four panes (2x2). The two on top are images, created with pcolormesh, while the ones on the bottom are line plots. I’d like to the axes to line up – which they do – but when I add a colorbar to the top right, it steals space from the image plot. I only need one colorbar since the two images are forced to have the same scaling. Can anyone tell me how to insert the colorbar on the top right without it taking space from the top right image – or force the space to be taken from the horizontal space of all the plots so the shapes are uniform? I’ve tried several approaches including using make_axes_locatable from mpl_toolkits.axes_grid1 with no success. I must admit that I don’t really understand what make_axes_locatable does, so it may be entirely the wrong thing to use. On the other hand, not using it, i.e. just using plt.colorbar or fig.colorbar seems to do essentially the same thing, steal space from the axes. The basics of the plot are like this:

x = np.arange(20.)

xc = (x[:-1] + x[1:])/2.

y = np.arange(15.)

xx,yy = np.meshgrid(x,y)

z1 = np.random.randn(14,19)

z2 = np.random.randn(14,19)

fig,ax = plt.subplots(2,2)

pc = ax[0,0].pcolormesh(x,y,z1)

ax[1,0].plot(xc,z1[0,:])

ax[0,1].pcolormesh(x,y,z2)

ax[1,1].plot(xc,z2[0,:])

cb = plt.colorbar(pc, ax=ax[0,1])

plt.show()

Any help would be appreciated.

Regards,

Jon


Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@…1081… 60 Garden Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
fax: (617) 496-7577 USA



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=164703151&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@…120…1081… 60 Garden Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
fax: (617) 496-7577 USA


Hi Ben,

Thanks for those tips. I ended up giving a list of axes to the ax
argument of colorbar as you suggested. That took care of the
misalignment of the top plots and bottom plots but then the colorbar
extended the full vertical height of both rows of axes. Fiddling with
the shrink and anchor parameter values then got me what I wanted. I'm
sure that axis_grid1 could work as well, but my situation is
sufficiently complicated that I don't want to spend the time fiddling
with it at this point.

For the record, another way to handle this situation is to use subplot parameters to leave adequate room on the right, then use the Axes.add_axes() method to make an Axes for the colorbar, and provide it as a cax kwarg.

Eric

···

On 2014/12/12, 5:11 AM, Slavin, Jonathan wrote:

Regards,
Jon

On Thu, Dec 11, 2014 at 2:30 PM, Benjamin Root <ben.root@…1304… > <mailto:ben.root@…1304…>> wrote:

    If you have matplotlib v1.3 (maybe 1.2, I can't remember), you can
    pass a list of axes to the "ax" argument of colorbar, and it should
    steal space equally from all the specified axes.

    I am also particularly fond of using the mpl_toolkit axis_grid1,
    which provides you a nice way to preallocate space for colorbars in
    different ways:
    http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html?highlight=axes_grid1#axes-grid1

    I hope that helps!
    Ben Root

    On Thu, Dec 11, 2014 at 1:49 PM, Slavin, Jonathan > <jslavin@…1081… <mailto:jslavin@…1081…>> wrote:

        Hi,

        I'm trying to make a plot with four panes (2x2). The two on top
        are images, created with pcolormesh, while the ones on the
        bottom are line plots. I'd like to the axes to line up -- which
        they do -- but when I add a colorbar to the top right, it steals
        space from the image plot. I only need one colorbar since the
        two images are forced to have the same scaling. Can anyone tell
        me how to insert the colorbar on the top right without it taking
        space from the top right image -- or force the space to be taken
        from the horizontal space of all the plots so the shapes are
        uniform? I've tried several approaches including using
        make_axes_locatable from mpl_toolkits.axes_grid1 with no
        success. I must admit that I don't really understand what
        make_axes_locatable does, so it may be entirely the wrong thing
        to use. On the other hand, not using it, i.e. just using
        plt.colorbar or fig.colorbar seems to do essentially the same
        thing, steal space from the axes. The basics of the plot are
        like this:

        x = np.arange(20.)
        xc = (x[:-1] + x[1:])/2.
        y = np.arange(15.)
        xx,yy = np.meshgrid(x,y)
        z1 = np.random.randn(14,19)
        z2 = np.random.randn(14,19)

        fig,ax = plt.subplots(2,2)
        pc = ax[0,0].pcolormesh(x,y,z1)
        ax[1,0].plot(xc,z1[0,:])
        ax[0,1].pcolormesh(x,y,z2)
        ax[1,1].plot(xc,z2[0,:])
        cb = plt.colorbar(pc, ax=ax[0,1])
        plt.show()

        Any help would be appreciated.

        Regards,
        Jon
        –
        ________________________________________________________
        Jonathan D. Slavin Harvard-Smithsonian CfA
        jslavin@…1081… <mailto:jslavin@…1081…>
        60 Garden Street, MS 83
        phone: (617) 496-7981 <tel:%28617%29%20496-7981>
        Cambridge, MA 02138-1516
        fax: (617) 496-7577 <tel:%28617%29%20496-7577> USA
        ________________________________________________________

        ------------------------------------------------------------------------------
        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=164703151&iu=/4140/ostg.clktrk
        _______________________________________________
        Matplotlib-users mailing list
        Matplotlib-users@lists.sourceforge.net
        <mailto:Matplotlib-users@lists.sourceforge.net>
        https://lists.sourceforge.net/lists/listinfo/matplotlib-users


________________________________________________________
Jonathan D. Slavin Harvard-Smithsonian CfA
jslavin@…1081… <mailto:jslavin@…1081…> 60 Garden
Street, MS 83
phone: (617) 496-7981 Cambridge, MA 02138-1516
fax: (617) 496-7577 USA
________________________________________________________

------------------------------------------------------------------------------
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=164703151&iu=/4140/ostg.clktrk

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users