Decouple the colorbar form subplot

Hi all!

following code shows the problem I have with placing a colorbar for several
subplots.

#-----------------------------code------------
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()

plt.subplot(211)
CS = plt.contourf(X, Y, Z)

plt.subplot(212)
CS = plt.contourf(X, Y, Z)

plt.colorbar(orientation='horizontal')
plt.show()

#-------------end code--------------
result:
http://old.nabble.com/file/p32037832/inttest.png

is ther a way to not let the colorbar cut space from the 2nd subplot. I
would like to have them both the same size.

Thanks!

Siggi

···

--
View this message in context: http://old.nabble.com/Decouple-the-colorbar-form-subplot-tp32037832p32037832.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

Hi all!

following code shows the problem I have with placing a colorbar for several
subplots.

#-----------------------------code------------
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()

plt.subplot(211)
CS = plt.contourf(X, Y, Z)

plt.subplot(212)
CS = plt.contourf(X, Y, Z)

plt.colorbar(orientation='horizontal')
plt.show()

#-------------end code--------------
result:
http://old.nabble.com/file/p32037832/inttest.png

is ther a way to not let the colorbar cut space from the 2nd subplot. I
would like to have them both the same size.

the subplot command only takes from the last axes as default. What you
need to do (I think and I always do it like this) Define 3 sets of axes
by hand instead of subplot(211) you use plt.axes(...) to create them (I
guess there may be a nicer method, not sure). Then you can pass which
axes to draw into with the colorbar function.

Regards,

Sebastian

···

On Mon, 2011-07-11 at 07:13 -0700, SiggiN wrote:

Personally, I prefer to use the axes_grid toolkit that allows you to specify if and how you want colorbar axes and provides an array of colorbar axes. http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/index.html#toolkit-axesgrid-index

Enjoy!
Ben Root

···

On Mon, Jul 11, 2011 at 11:16 AM, Sebastian Berg <sebastian@…878…3476…> wrote:

Hi,

On Mon, 2011-07-11 at 07:13 -0700, SiggiN wrote:

Hi all!

following code shows the problem I have with placing a colorbar for several

subplots.

#-----------------------------code------------

import matplotlib

import numpy as np

import matplotlib.cm as cm

import matplotlib.mlab as mlab

import matplotlib.pyplot as plt

matplotlib.rcParams[‘xtick.direction’] = ‘out’

matplotlib.rcParams[‘ytick.direction’] = ‘out’

delta = 0.025

x = np.arange(-3.0, 3.0, delta)

y = np.arange(-2.0, 2.0, delta)

X, Y = np.meshgrid(x, y)

Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)

difference of Gaussians

Z = 10.0 * (Z2 - Z1)

plt.figure()

plt.subplot(211)

CS = plt.contourf(X, Y, Z)

plt.subplot(212)

CS = plt.contourf(X, Y, Z)

plt.colorbar(orientation=‘horizontal’)

plt.show()

#-------------end code--------------

result:

http://old.nabble.com/file/p32037832/inttest.png

is ther a way to not let the colorbar cut space from the 2nd subplot. I

would like to have them both the same size.

the subplot command only takes from the last axes as default. What you

need to do (I think and I always do it like this) Define 3 sets of axes

by hand instead of subplot(211) you use plt.axes(…) to create them (I

guess there may be a nicer method, not sure). Then you can pass which

axes to draw into with the colorbar function.

Regards,

Sebastian

Sebastian Berg, on 2011-07-11 18:16, wrote:

> is ther a way to not let the colorbar cut space from the 2nd subplot. I
> would like to have them both the same size.

yes, you can pass the cax parameter to colorbar which will put
it inside that axis.

replace your code from line 'plt.figure()' with this:

  f,(ax,ax2,ax3) = plt.subplots(3,1)
  CS = ax.contourf(X, Y, Z)
  CS = ax2.contourf(X, Y, Z)
  plt.colorbar(CS,cax=ax3,orientation='horizontal')

What you need to do (I think and I always do it like this)
Define 3 sets of axes by hand instead of subplot(211) you use
plt.axes(...) to create them (I guess there may be a nicer
method, not sure).

I belive the plt.subplots() command is what Sebastian was
referring to.

best,

···

On Mon, 2011-07-11 at 07:13 -0700, SiggiN wrote:

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

First, thanks to you for your answers!
I tried all the solutions and the one with the AxisGrid Toolkit worked out
best for me.

There are only some small questions left.
How do I change the origin and how do I change fontsize of the ticklabels?

#-----------------------------code------------
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
import pylab as pyl
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.5
x = np.arange(0, 90.0, delta)
y = np.arange(0, 90.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 16.0, 17.0, 60.0, 60.0)
Z2 = mlab.bivariate_normal(X, Y, 16.5, 23.5, 45, 45)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

#plt.figure()

f,(ax,ax2,ax3) = plt.subplots(3,1)
CS = ax.contourf(X, Y, Z)
CS = ax2.contourf(X, Y, Z)
plt.colorbar(CS,cax=ax3,orientation='horizontal')

fig00 = plt.figure(figsize=(10,3.2), dpi=100, facecolor='w', edgecolor='k')
    
grid = AxesGrid(fig00, 111, # similar to subplot(132)
                    nrows_ncols = (1, 2),
                    axes_pad = 0.4,
                    share_all=True,
                    label_mode = "L",
                    cbar_pad = 0.2,
                    cbar_location = "right",
                    cbar_mode="single",
                )
p0 = grid[0].contourf(X, Y, Z,origin='upper')
p20 = grid[1].contourf(X,Y,Z,origin= 'upper')

#-------------not working---------
ylim = pyl.get(plt.gca(), 'ylim')
plt.setp(plt.gca(), ylim=ylim[::-1])#

···

#---------------------------------------

plt.colorbar(p0, cax = grid.cbar_axes[0])
    
for cax in grid.cbar_axes:
    cax.toggle_label(True)
            
grid[0].set_xticks([0,90,45])
grid[0].set_yticks(pyl.arange(0,100,10))
grid[1].set_xticks([0,90,45])
grid[1].set_yticks(pyl.arange(0,100,10))
grid[0].set_xlabel('phi 1', fontsize=8)
grid[1].set_xlabel('phi 1', fontsize=8)
grid[0].set_ylabel('$\Phi$', fontsize=12)
grid[1].set_ylabel('$\Phi$', fontsize=12)

plt.show()

Siggi
--
View this message in context: http://old.nabble.com/Decouple-the-colorbar-form-subplot-tp32037832p32117770.html
Sent from the matplotlib - users mailing list archive at Nabble.com.