1

I have a loop that utilizes the 'set_data' command so that I can rapidly image data in Python, while in a loop. This was inspired by this answer here.

However one issue is that it seems that the color-axis is not being rescaled every time a new image comes in. This makes it harder and harder to see detail in the images every time a new one is plotted.

I am trying to force Python to simply auto-scale the color axes on every iteration, but I cannot seem to do so. This is what I have:

fig, ax = plt.subplots()
im = ax.imshow(someImage[:,:,0], interpolation='none')
ax.set_aspect('auto'); fig.show(); 
for ii in range(numberOfImages):
    im.set_data(someImage[:,:,ii]);
    fig.show();
    plt.pause(0.2);

This works nicely for quickly updating an image in a for loop in Python, but how to force the color-scale to be auto at every iteration?

Thanks.

Error message after Bart's answer:

2016-05-30 13:34:06.574 python[26795:3953860] setCanCycle: is deprecated. Please use setCollectionBehavior instead 2016-05-30 13:34:06.586 python[26795:3953860] setCanCycle: is deprecated. Please use setCollectionBehavior instead 2016-05-30 13:34:06.588 python[26795:3953860] setCanCycle: is deprecated. Please use setCollectionBehavior instead 2016-05-30 13:34:06.920 python[26795:3953860] setCanCycle: is deprecated. Please use setCollectionBehavior instead /Users/roger/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.py:2437: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation)"

Community
  • 1
  • 1
TheGrapeBeyond
  • 543
  • 2
  • 8
  • 19
  • Add `plt.draw()` in cycle to redraw plot. – Serenity May 28 '16 at 21:08
  • If you are showing the time dependency of something, you want to apply the same colormap to all the images and the behavior of `matplotlib` is correct. What you would like to change is to choose a range for the initial colormap larger than the one that is initially chosen, tailored to the range of data in the first image, so that it can be applied to all the image sequence. – gboffi May 28 '16 at 21:09
  • @gboffi Yes, if I care about the time dependency, you are right, that is what I do. However in this case I do not care about the time-dependency, and that so I would like to simply re-set the colormap scale every iteration so that I can see the signals in proper contrast. How can I re-scale it on an image basis? Thanks – TheGrapeBeyond May 28 '16 at 21:39
  • @StanleyR That does not seem to fix the issue - the color scaling is still not automatically changed. – TheGrapeBeyond May 28 '16 at 22:31

1 Answers1

1

You could manually change vmin and vmax using im.set_clim(), then the colorbar (assuming that's what you are refering to as "color axis") is rescaled automatically:

import matplotlib.pylab as pl
import numpy as np

pl.close('all')

someImage = np.random.random((4,4,3))
someImage[:,:,1] *= 10
someImage[:,:,2] *= 100

fig, ax = pl.subplots()
im = ax.imshow(someImage[:,:,0], interpolation='none')
pl.colorbar(im)
ax.set_aspect('auto'); fig.show(); 
for ii in range(1,3):
    im.set_data(someImage[:,:,ii]);
    im.set_clim(vmin=someImage[:,:,ii].min(), vmax=someImage[:,:,ii].max())
    fig.show();
    pl.pause(1.0);
Bart
  • 9,825
  • 5
  • 47
  • 73
  • Hi Bart, thanks, however unfortunately it does not work: I get an error. (See my edited post for the error). Thanks. – TheGrapeBeyond May 30 '16 at 20:37
  • No idea where that is coming from. A quick search provided me with this: http://stackoverflow.com/questions/30818222/python-2-7-9-mac-os-10-10-3-message-setcancycle-is-deprecated-please-use-setc – Bart May 30 '16 at 20:41
  • It seems to be related to the matplotlib backend (I'm using `MacOSX`); I'll try some others tomorrow, but you might also want to try some others to see if that indeed causes this error/warning. – Bart May 30 '16 at 21:12
  • To simplify my search: which backend are you using? – Bart May 30 '16 at 21:13
  • Hi @Bart, I am also using "MacOSX" it seems as well... (I just checked the backend). – TheGrapeBeyond May 31 '16 at 20:37