On Friday, November 4, 2011, Tony Yu <tsyu80@gmail.com> wrote:
>
>
> On Fri, Nov 4, 2011 at 4:49 PM, Rahul Mahajan <aerorahul@gmail.com> wrote:
>>
>> Hi,
>> I am trying to put 1 colorbar on 2 subplots, but failing miserably.
>>
>> I have tried multiple approaches, involving subplot2grid, add_axes, AxesGrid etc.
>>
>> If anyone has a "canned" routine or knows what to do, your help is greatly appreciated.
>>
>> Here is my code block:
>>
>>   fig = pyplot.figure()
>>
>>     ax1 = pyplot.subplot2grid((2,2),(0,0))
>>     cx1  = pyplot.contourf(x,y,var1,var_cint,origin='lower',extend='both')
>>
>>     ax2 = pyplot.subplot2grid((2,2),(0,1))
>>     cx2  = pyplot.contourf(x,y,var2,var_cint,origin='lower',extend='both')
>>
>> Now I want to put 1 colorbar to these subplots.
>
> This example seems to be a very simple example of what you want:
> http://matplotlib.sourceforge.net/examples/pylab_examples/subplots_adjust.html
>
> Basically, you need to create an axes specifically for the colorbar (e.g. `colorbar_ax`) and call pyplot.colorbar(cax=colorbar_ax); or you can let mpl automatically take space from one of your existing axes: for example, pyplot.colorbar(ax=ax2).
>
> You can also pass a mappable to pyplot.colorbar (in your example, cx1 or cx2), but by default, it will grab the most recently created one. Note, using the single colorbar for both subplots *only* makes sense if var_cint (in your code) is defining the *values* of the contour levels (as opposed to the *number* of contour levels). Otherwise, the colorbar will only be accurate for one of the two plots.
>
> HTH,
> -Tony
>

Even easier is to use the axes_grid1 module and create an AxesGrid object. In the constructor, you can specify the colorbar mode as 'single'.  Then, the AxesGrid object has the axes for plots, and a separate list for any axes for use for colobars.

Does that help?
Ben Root