TheGrapeBeyond
TheGrapeBeyond

Reputation: 563

Force python to auto-scale the colorbar in image every iteration

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)"

Upvotes: 1

Views: 3268

Answers (1)

Bart
Bart

Reputation: 10298

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);

Upvotes: 1

Related Questions