简体   繁体   中英

How to get current plot's clim in matplotlib

After plotting in Matlab we do caxis(max(caxis()) - [0.5, 0]) to scale the color limits to go from the current max color limit to, say, 0.5 below this max. This works because caxis() in Matlab both gets and sets the color limits. How does one do this in matplotlib?

That is, I want to achieve the following:

import numpy.random, numpy, pylab
arr = numpy.random.randn(100,100)
pylab.figure()
pylab.imshow(arr)
pylab.colorbar()
pylab.clim([numpy.max(arr.ravel())-0.5, numpy.max(arr.ravel())]) # [*]
pylab.show()

without the asterisked call to pylab.clim() having recourse to arr , the array being plotted. In other words, how can I get the current figure's "clim" in matplotlib?

If you didn't keep the returned image object, you can use pylab.gci to get the current ScalarMappable (ie whatever the current colorbar would be based on).

From there, you just want the get_clim method of the ScalarMappable object.

So, you could do:

vmin, vmax = plt.gci().get_clim()

Found a 2006 mailing list conversation showing me the way:

im = pylab.imshow(arr)
pylab.clim(im.norm.vmax - numpy.array([0.5, 0]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM