imshow() with colorbar and custom range?

Hi all,

I'd like to compare two sets of data by using subplots and imshow(). I
have two issues with this:

1) how can I have a colorbar in each subplot?
2) how can I adjust the range limits for a colorbar, so that the
comparison possible?

Below is code which could serve as a starting point, I hope it helps
to make my problem clearer...

Thanks and best regards,
Daniel

import pylab
#import matplotlib as mpl
import matplotlib.cm as cm # colormaps
#import matplotlib.colors as col # colormaps

pylab.close('all')

dat = pylab.array([[1,2,3,4],[5,6,7,8]])
datT = dat/2

fig = pylab.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('raw data')
im = ax.imshow(dat, interpolation='nearest', cmap=cm.get_cmap('rainbow', 20))
fig.colorbar(im)

ax2 = fig.add_subplot(212)
ax2.set_title('transformed')
''' here is missing:
a 2nd colorbar with the same limit than in the first subfig, i.e. 1..8
'''

pylab.show()

Hi all,

I'd like to compare two sets of data by using subplots and imshow(). I
have two issues with this:

1) how can I have a colorbar in each subplot?
2) how can I adjust the range limits for a colorbar, so that the
comparison possible?

Below is code which could serve as a starting point, I hope it helps
to make my problem clearer...

It's not quite clear to me yet, but I assume you want to use a call to imshow with a different data set in the second subplot, but have the color scale and colorbar be identical to those in the first subplot. Is that correct? If so, all you need to do is use the same norm for both calls to imshow--that is, define a norm, set the limits you want on it, and supply it as a kwarg.

Also, for this sort of comparison, sometimes it is more efficient to use a single colorbar for multiple panels, as in this example:

  http://matplotlib.sourceforge.net/examples/pylab_examples/multi_image.html

Eric

···

On 06/06/2011 11:33 AM, Daniel Mader wrote:

Thanks and best regards,
Daniel

import pylab
#import matplotlib as mpl
import matplotlib.cm as cm # colormaps
#import matplotlib.colors as col # colormaps

pylab.close('all')

dat = pylab.array([[1,2,3,4],[5,6,7,8]])
datT = dat/2

fig = pylab.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('raw data')
im = ax.imshow(dat, interpolation='nearest', cmap=cm.get_cmap('rainbow', 20))
fig.colorbar(im)

ax2 = fig.add_subplot(212)
ax2.set_title('transformed')
''' here is missing:
a 2nd colorbar with the same limit than in the first subfig, i.e. 1..8
'''

pylab.show()

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Eric,

2011/6/6 Eric Firing <efiring@...202...>:

It's not quite clear to me yet, but I assume you want to use a call to
imshow with a different data set in the second subplot, but have the
color scale and colorbar be identical to those in the first subplot. Is
that correct? If so, all you need to do is use the same norm for both
calls to imshow--that is, define a norm, set the limits you want on it,
and supply it as a kwarg.

thanks a lot, you helped me to work around my problem, see code below :slight_smile:

Also, for this sort of comparison, sometimes it is more efficient to use
a single colorbar for multiple panels, as in this example:

http://matplotlib.sourceforge.net/examples/pylab_examples/multi_image.html

Very nice example! It's a little too complex for me, though, with all
the calculations for the axes layout -- I prefer subplots :slight_smile: However,
I think I have found a nice compromise:

import pylab
import matplotlib as mpl

pylab.close('all')

dat = pylab.array([[1,2,3,4],[5,6,7,8]])
datT = dat/2

fig = pylab.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('raw data')
im1 = ax1.imshow(dat, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
fig.colorbar(im1)

ax2 = fig.add_subplot(212)
ax2.set_title('leveled')
im2 = ax2.imshow(datT, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
## apply norm:
norm = mpl.colors.Normalize(vmin=dat.min(), vmax=dat.max())
im2.set_norm(norm)
fig.colorbar(im2)

## doesn't really work :confused:
cax = fig.add_axes([0.25, 0.04, 0.5, 0.02])
fig.colorbar(im2, cax, orientation='horizontal')

pylab.show()

Thanks a lot,
best regards,

Daniel

Hi Eric,

2011/6/6 Eric Firing<efiring@...202...>:

It's not quite clear to me yet, but I assume you want to use a call to
imshow with a different data set in the second subplot, but have the
color scale and colorbar be identical to those in the first subplot. Is
that correct? If so, all you need to do is use the same norm for both
calls to imshow--that is, define a norm, set the limits you want on it,
and supply it as a kwarg.

thanks a lot, you helped me to work around my problem, see code below :slight_smile:

Also, for this sort of comparison, sometimes it is more efficient to use
a single colorbar for multiple panels, as in this example:

  http://matplotlib.sourceforge.net/examples/pylab_examples/multi_image.html

Very nice example! It's a little too complex for me, though, with all
the calculations for the axes layout -- I prefer subplots :slight_smile: However,
I think I have found a nice compromise:

Attached is a slight modification, much simpler than the example above, but retaining the single colorbar. Alternatively, if you stick with the colorbar for each panel (which is sometimes clearer), it illustrates a slightly clearer way of handling the cmap and norm, explicitly using the same instance of each for both images.

Eric

test_subplots.py (806 Bytes)

···

On 06/07/2011 01:37 AM, Daniel Mader wrote:

import pylab
import matplotlib as mpl

pylab.close('all')

dat = pylab.array([[1,2,3,4],[5,6,7,8]])
datT = dat/2

fig = pylab.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('raw data')
im1 = ax1.imshow(dat, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
fig.colorbar(im1)

ax2 = fig.add_subplot(212)
ax2.set_title('leveled')
im2 = ax2.imshow(datT, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
## apply norm:
norm = mpl.colors.Normalize(vmin=dat.min(), vmax=dat.max())
im2.set_norm(norm)
fig.colorbar(im2)

## doesn't really work :confused:
cax = fig.add_axes([0.25, 0.04, 0.5, 0.02])
fig.colorbar(im2, cax, orientation='horizontal')

pylab.show()

Thanks a lot,
best regards,

Daniel

Dear Eric,

thanks again for your comment, I am aware that the script contained
both the individual colorbars and the common one. My comment in the
code was because the placement on the figure is somewhat cramped:

## doesn't really work :confused: ## in what way?
cax = fig.add_axes([0.25, 0.06, 0.5, 0.02])
fig.colorbar(im2, cax, orientation='horizontal')

Ideally, I'd need to create a new subfig 313 with a much reduced height.

Either way, you helped me a lot!

2011/6/7 Eric Firing <efiring@...202...>:

···

On 06/07/2011 01:37 AM, Daniel Mader wrote:

Hi Eric,

2011/6/6 Eric Firing<efiring@...202...>:

It's not quite clear to me yet, but I assume you want to use a call to
imshow with a different data set in the second subplot, but have the
color scale and colorbar be identical to those in the first subplot. Is
that correct? If so, all you need to do is use the same norm for both
calls to imshow--that is, define a norm, set the limits you want on it,
and supply it as a kwarg.

thanks a lot, you helped me to work around my problem, see code below :slight_smile:

Also, for this sort of comparison, sometimes it is more efficient to use
a single colorbar for multiple panels, as in this example:

http://matplotlib.sourceforge.net/examples/pylab_examples/multi_image.html

Very nice example! It's a little too complex for me, though, with all
the calculations for the axes layout -- I prefer subplots :slight_smile: However,
I think I have found a nice compromise:

Attached is a slight modification, much simpler than the example above, but
retaining the single colorbar. Alternatively, if you stick with the
colorbar for each panel (which is sometimes clearer), it illustrates a
slightly clearer way of handling the cmap and norm, explicitly using the
same instance of each for both images.

Eric

import pylab
import matplotlib as mpl

pylab.close('all')

dat = pylab.array([[1,2,3,4],[5,6,7,8]])
datT = dat/2

fig = pylab.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('raw data')
im1 = ax1.imshow(dat, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
fig.colorbar(im1)

ax2 = fig.add_subplot(212)
ax2.set_title('leveled')
im2 = ax2.imshow(datT, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
## apply norm:
norm = mpl.colors.Normalize(vmin=dat.min(), vmax=dat.max())
im2.set_norm(norm)
fig.colorbar(im2)

## doesn't really work :confused:
cax = fig.add_axes([0.25, 0.04, 0.5, 0.02])
fig.colorbar(im2, cax, orientation='horizontal')

pylab.show()

Thanks a lot,
best regards,

Daniel

Dear Eric,

thanks again for your comment, I am aware that the script contained
both the individual colorbars and the common one. My comment in the
code was because the placement on the figure is somewhat cramped:

That's what I suspected, so the main point of the modified version was using subplots_adjust to give more room at the bottom, and then shifting the bottom colorbar so that it was less cramped. Granted, this approach takes some fiddling; but if you are using subplots and care about appearance, then sooner or later you will probably benefit from subplots_adjust, a function/method which is probably not as well-known as it deserves to be.

Eric

···

On 06/08/2011 01:28 AM, Daniel Mader wrote:

## doesn't really work :confused: ## in what way?
cax = fig.add_axes([0.25, 0.06, 0.5, 0.02])
fig.colorbar(im2, cax, orientation='horizontal')

Ideally, I'd need to create a new subfig 313 with a much reduced height.

Either way, you helped me a lot!

2011/6/7 Eric Firing<efiring@...202...>:

On 06/07/2011 01:37 AM, Daniel Mader wrote:

Hi Eric,

2011/6/6 Eric Firing<efiring@...202...>:

It's not quite clear to me yet, but I assume you want to use a call to
imshow with a different data set in the second subplot, but have the
color scale and colorbar be identical to those in the first subplot. Is
that correct? If so, all you need to do is use the same norm for both
calls to imshow--that is, define a norm, set the limits you want on it,
and supply it as a kwarg.

thanks a lot, you helped me to work around my problem, see code below :slight_smile:

Also, for this sort of comparison, sometimes it is more efficient to use
a single colorbar for multiple panels, as in this example:

  http://matplotlib.sourceforge.net/examples/pylab_examples/multi_image.html

Very nice example! It's a little too complex for me, though, with all
the calculations for the axes layout -- I prefer subplots :slight_smile: However,
I think I have found a nice compromise:

Attached is a slight modification, much simpler than the example above, but
retaining the single colorbar. Alternatively, if you stick with the
colorbar for each panel (which is sometimes clearer), it illustrates a
slightly clearer way of handling the cmap and norm, explicitly using the
same instance of each for both images.

Eric

import pylab
import matplotlib as mpl

pylab.close('all')

dat = pylab.array([[1,2,3,4],[5,6,7,8]])
datT = dat/2

fig = pylab.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('raw data')
im1 = ax1.imshow(dat, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
fig.colorbar(im1)

ax2 = fig.add_subplot(212)
ax2.set_title('leveled')
im2 = ax2.imshow(datT, interpolation='nearest',
cmap=mpl.cm.get_cmap('rainbow', 20))
## apply norm:
norm = mpl.colors.Normalize(vmin=dat.min(), vmax=dat.max())
im2.set_norm(norm)
fig.colorbar(im2)

## doesn't really work :confused:
cax = fig.add_axes([0.25, 0.04, 0.5, 0.02])
fig.colorbar(im2, cax, orientation='horizontal')

pylab.show()

Thanks a lot,
best regards,

Daniel

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users