How to get current plot's clim in matplotlib
matlab, matplotlib, numpy, python
Solution
If you didn't keep the returned image object, you can use `pylab.gci` to get the current ScalarMappable (i.e. 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()
Problem
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?