colorbar uncentered plots

Hello! see below for sample code.

(1) I find that the second subplot is shifted to the right.
This seems to happen generally with colorbar. How do I fix it?

(2) is there a way to clear/remove selected parts of the figure?
I am using ipython for interactive figure drawing
I find clf() will erase everything, cla() will empty the square plot,
   but I can't see how to clear the first subfigure or the colorbar.
This would be useful as any changes to the colorbar create a new one.
(uncomment the second cbar line to see this)

(3) is it possible to modify the orientation of cbar once drawn?
cbar.orientation ='horizontal'
plt.draw()
plt.show()
leaves it unchanged.

Thank you for your help!

================ Here is my code: ==================

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
from time import sleep
x = np.arange(0, 10, 0.2)
y = np.sin(x)
fig = plt.figure()
ax1 = fig.add_subplot(211)
cax1 = ax1.plot(x, y)
ax2 = fig.add_subplot(212)
A = np.random.random_integers(0, 10, 100).reshape(10, 10)
cax2 = ax2.imshow(A, interpolation="nearest",vmin=-1,vmax=11 )
cbar = fig.colorbar(cax2)
#cbar = fig.colorbar(cax2, ticks=[0, 5, 10])
plt.savefig('colorbartest.pdf')

···

======================================================
--

Philip,

Typically, when creating a colorbar, matplotlib “steals” some space from a particular axes. This is fine for single plots, but it looks atrocious when doing subplots. Instead, what you want is axes_grid1

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/

(note, if you have a version earlier than 1.0.0, then it is called “axes_grid” and it is a little bit different)

With this, you can specify a bunch of layout options ahead of time and get an object with all of the axes you need. In particular, I think you want to look at http://matplotlib.sourceforge.net/plot_directive/mpl_toolkits/axes_grid/examples/demo_axes_grid.py

A quick note to clear up typical confusion in that demo… You can still use subplots and embed an AxesGrid within a subplot region (although, I find this unnecessary). For this reason, the linked demo has three subplots in a single figure, and each of those subplots have four subplots. Therefore, when creating each AxesGrid, one needs to specify the subplot coordinates such as 131, 132, 133, but you can simply use 111 if you want AxesGrid to handle all of your subplotting.

To address one of your other questions, you can easily specify the orientation of your colorbar to be horizontal or vertical.

I hope that helps!

Ben Root

···

On Tue, Sep 28, 2010 at 10:56 PM, Philip Vetter <pv+matplotlib@…3298…> wrote:

Hello! see below for sample code.

(1) I find that the second subplot is shifted to the right.

This seems to happen generally with colorbar. How do I fix it?

(2) is there a way to clear/remove selected parts of the figure?

I am using ipython for interactive figure drawing

I find clf() will erase everything, cla() will empty the square plot,

but I can’t see how to clear the first subfigure or the colorbar.

This would be useful as any changes to the colorbar create a new one.

(uncomment the second cbar line to see this)

(3) is it possible to modify the orientation of cbar once drawn?

cbar.orientation =‘horizontal’

plt.draw()

plt.show()

leaves it unchanged.

Thank you for your help!

================ Here is my code: ==================

#!/usr/bin/env python

import matplotlib.pyplot as plt

import numpy as np

from time import sleep

x = np.arange(0, 10, 0.2)

y = np.sin(x)

fig = plt.figure()

ax1 = fig.add_subplot(211)

cax1 = ax1.plot(x, y)

ax2 = fig.add_subplot(212)

A = np.random.random_integers(0, 10, 100).reshape(10, 10)

cax2 = ax2.imshow(A, interpolation=“nearest”,vmin=-1,vmax=11 )

cbar = fig.colorbar(cax2)

#cbar = fig.colorbar(cax2, ticks=[0, 5, 10])

plt.savefig(‘colorbartest.pdf’)

======================================================