简体   繁体   English

如何在matplotlib中获取当前图的爬升

[英]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. 在Matlab中绘制后,我们执行caxis(max(caxis()) - [0.5, 0])来缩放颜色限制,以将颜色限制从当前的最大颜色限制扩展到该最大值以下的0.5。 This works because caxis() in Matlab both gets and sets the color limits. 之所以caxis()是因为Matlab中的caxis()既获取并设置了颜色限制。 How does one do this in matplotlib? 如何在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. 没有pylab.clim()的星号调用可以求助于arr ,可以绘制数组。 In other words, how can I get the current figure's "clim" in matplotlib? 换句话说,如何在matplotlib中获取当前图形的“ clim”?

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). 如果没有保留返回的图像对象,则可以使用pylab.gci获取当前的ScalarMappable(即,当前颜色条将基于的内容)。

From there, you just want the get_clim method of the ScalarMappable object. 从那里开始,您只需要ScalarMappable对象的get_clim方法。

So, you could do: 所以,你可以这样做:

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

Found a 2006 mailing list conversation showing me the way: 找到了2006年邮件列表对话,向我展示了方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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