在matplotlib颜色条上设置限制,而不更改实际图 [英] Set limits on a matplotlib colorbar without changing the actual plot
问题描述
我想创建一个伪色图(例如 contour
或 contourf
)和一个色条。出于实际原因,我希望颜色条的范围与基础可映射对象不同。
在下面的示例中,数据Z的范围为0到10000,即映射到颜色图。颜色条的范围是相同的。
import numpy
从matplotlib导入pyplot
X = numpy.arange(100)
Y = numpy.arange(100)
Z = numpy.arange(100 ** 2).reshape((100,100))
f = pyplot.figure()
ax = f.gca()
cf = ax.contourf(X,Y,Z,100)
cbar = f.colorbar(cf,ticks = [3000,4000,5000,6000])
pyplot.show()
现在,我想在颜色栏上放大,即生成仅在3000至6000范围内的颜色栏。这个新的颜色条仍将用作图例,并为每个刻度线提供适当的颜色(3000 =蓝色,6000 =黄色)。 cbar.set_clim()
和 cf.set_clim()
都无法做到这一点。
通常,禁止显示颜色栏是一个坏主意,但这是一种超级技巧。
从matplotlib导入numpy
导入pyplot
X = numpy.arange(100]
Y = numpy.arange(100)
Z = numpy.arange(100 ** 2).reshape((100,100))
f = pyplot.figure()
ax = f.gca()
cf = ax.contourf(X,Y,Z,100)
cbar = f.colorbar(cf,ticks = [3000,4000,5000,6000] )
cbar.ax.set_ylim([cbar.norm(3000),cbar.norm(6000)])
cbar.outline.set_ydata([cbar.norm(3000)] * 2 + [cbar .norm(6000)] * 4 + [cbar.norm(3000)] * 3)
cbar.ax.set_aspect(60)#<-调整此值以获得您想要的
pyplot .show()
我称其为hacky是因为它触及了颜色条的大部分内部结构。 cbar.outline
是 Line2D
对象,它是颜色栏周围的黑框,即 set_ydata
在拐角处设置ydata以匹配您要查看的子区域。
您可能想研究一下colormap的 clip
功能。 / p>
I would like create a pseudocolor plot (e.g. contour
or contourf
) and a colorbar. For practical reason, I want the range of the colorbar different from the underlying mappable.
In the example below, the data Z has a range from 0 to 10000, which is mapped to a colormap. The range of the colorbar is the same.
import numpy
from matplotlib import pyplot
X = numpy.arange(100)
Y = numpy.arange(100)
Z = numpy.arange(100**2).reshape((100,100))
f = pyplot.figure()
ax = f.gca()
cf = ax.contourf(X,Y,Z,100)
cbar = f.colorbar(cf, ticks=[3000,4000,5000,6000])
pyplot.show()
Now, I would like to "zoom in" on the colorbar, i.e. generate a colorbar with a range from 3000 to 6000 only. This new colorbar shall still serve as a legend and give proper colors for each tick (3000 = blue, 6000 = yellow). Neither cbar.set_clim()
nor cf.set_clim()
accomplish this.
In general suppressing sections of your color bar is a Bad Idea, but here is a super hacky way to do it.
import numpy
from matplotlib import pyplot
X = numpy.arange(100)
Y = numpy.arange(100)
Z = numpy.arange(100**2).reshape((100,100))
f = pyplot.figure()
ax = f.gca()
cf = ax.contourf(X,Y,Z,100)
cbar = f.colorbar(cf, ticks=[3000,4000,5000,6000])
cbar.ax.set_ylim([cbar.norm(3000), cbar.norm(6000)])
cbar.outline.set_ydata([cbar.norm(3000)] * 2 + [cbar.norm(6000)] * 4 + [cbar.norm(3000)] * 3)
cbar.ax.set_aspect(60) # <- tweak this to get the aspect ratio you want
pyplot.show()
I call this hacky because it touches a whole bunch of the internals of the colorbar. cbar.outline
is a Line2D
object that is the black box around the colorbar, the set_ydata
sets the ydata on the corners to match the sub-region you want to look at. Try it with out that line and see what happens.
You might want to look into colormap's clip
feature.
这篇关于在matplotlib颜色条上设置限制,而不更改实际图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!