Discussion:
[Matplotlib-users] How to place an image colorbar next to the image when there are several subplots ?
Peter Rowat
2015-02-20 07:10:55 UTC
Permalink
I apologize for asking such a trivial question, but I’ve 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,\
interpolation='nearest')
ax.set_title("Dummy title")
# Add colorbar
cbar = fig.colorbar(cax)
ax.set_ylabel('mV')

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

plt.show()
========
IN FACT,
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,\
interpolation='nearest')
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
ax1.set_ylabel('mV')
ax2.plot(emtrate, emrate, '.r')
ax3.plot(tt, rate*50 - 25.0, '-k', linewidth=3)

plt.show()
Thanks for any help,

Peter
Save our in-boxes! http://emailcharter.org
Sterling Smith
2015-02-20 08:56:30 UTC
Permalink
Peter,

I think that you want
cax = ax1.imshow(…)
cbar = fig.colorbar(cax,ax=ax1) # Where the ax keyword tells the colorbar which axes to steal space from. [1]

If you want the colorbar to be to the right of the first axes, and have the second and third axes line up with the first, then you need to create your own axes instance [2] {cbarax = fig.add_axes([…]) } for the colorbar and give that instance to the cax keyword of fig.colorbar (without an ax keyword). [1]

Also, I wouldn’t refer to the object returned by imshow as cax, as that is confusing when looking at the possible arguments of fig.colorbar. I would call the image object im or something similar.

-Sterling

[1] http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar
[2] http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.add_axes
Post by Peter Rowat
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.
fig,ax = plt.subplots()
cax = ax.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\
interpolation='nearest')
ax.set_title("Dummy title")
# Add colorbar
cbar = fig.colorbar(cax)
ax.set_ylabel('mV')
ax.plot(emtrate, emrate, '.r') #curve 1
ax.plot(tt, rate*50 - 25.0, '-k', linewidth=3) #curve 2
plt.show()
========
IN FACT,
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,\
interpolation='nearest')
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
ax1.set_ylabel('mV')
ax2.plot(emtrate, emrate, '.r')
ax3.plot(tt, rate*50 - 25.0, '-k', linewidth=3)
plt.show()
Thanks for any help,
Peter
Save our in-boxes! http://emailcharter.org
------------------------------------------------------------------------------
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=190641631&iu=/4140/ostg.clktrk_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Fabrice Silva
2015-02-20 08:48:17 UTC
Permalink
Post by Peter Rowat
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.
fig,ax = plt.subplots()
cax = ax.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\
interpolation='nearest')
ax.set_title("Dummy title")
# Add colorbar
cbar = fig.colorbar(cax)
ax.set_ylabel('mV')
ax.plot(emtrate, emrate, '.r') #curve 1
ax.plot(tt, rate*50 - 25.0, '-k', linewidth=3) #curve 2
plt.show()
========
IN FACT,
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,\
interpolation='nearest')
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)

See
http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar
--
Fabrice
Sterling Smith
2015-02-20 23:45:48 UTC
Permalink
Peter,

You’re welcome.

While I appreciate that you are trying to cut down on unnecessary emails (as per emailcharter.org - 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. :-) NNTR

-Sterling
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.
Peter
Save our in-boxes! http://emailcharter.org
Post by Fabrice Silva
Post by Peter Rowat
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.
fig,ax = plt.subplots()
cax = ax.imshow(bighistT, extent=myextent, cmap = cm.coolwarm, aspect = myaspect,\
interpolation='nearest')
ax.set_title("Dummy title")
# Add colorbar
cbar = fig.colorbar(cax)
ax.set_ylabel('mV')
ax.plot(emtrate, emrate, '.r') #curve 1
ax.plot(tt, rate*50 - 25.0, '-k', linewidth=3) #curve 2
plt.show()
========
IN FACT,
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,\
interpolation='nearest')
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
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)
See
http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar
--
Fabrice
------------------------------------------------------------------------------
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=190641631&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Loading...