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


You鈥檙e welcome.

While I appreciate that you are trying to cut down on unnecessary emails (as per - interesting read), it is appropriate to include the list in your responses, especially one indicating that a solution has been found, so that others on the list stop thinking about how they could help, and so that a person reading the archived list messages can feel confident that the suggestions were worthwhile. :slight_smile: NNTR



On Feb 20, 2015, at 3:20PM, Peter Rowat <peter@...4612...> wrote:

Thank you both for your help, my problem is solved. You both had similar suggestions, and now I have a slightly better understanding of matplotlib.

Save our in-boxes!

On Feb 20, 2015, at 12:48 AM, Fabrice Silva <silva@...1918...> wrote:

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

I apologize for asking such a trivial question, but I鈥檝e 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,\
聽聽聽ax.set_title("Dummy title")
聽聽聽# Add colorbar
聽聽聽cbar = fig.colorbar(cax)

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


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,\
聽聽聽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

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)



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
Matplotlib-users mailing list