可以使用色图的
set_over
和
set_under
方法来设置越界颜色,具体请参见
文档。在创建色图时需要指定这些值。虽然我没有看到任何 matplotlibrc 设置来设置默认值,但您也可以在 matplotlib 邮件列表上提问。
编辑:我明白了。您所描述的白色区域并未超出颜色范围的限制,它只是轴的空白背景。因为您仅绘制特定级别,超出该范围的任何级别都不会被绘制,留下那些空白区域。要得到您想要的效果,请执行以下操作:
cs = pyplot.contourf(x,y,z,levels=np.arange(50, 220, 20), cmap=pyplot.cm.jet, extend="both")
cs.cmap.set_under('k')
cs.set_clim(50, 210)
cb = pyplot.colorbar(cs)
"extend"参数是关键,它告诉contourf绘制所有轮廓,但将给定范围外的所有轮廓折叠成“太大”和“太小”的类别。调用
cs.set_clim
是必要的,以解决我在调试过程中发现的contourf中的奇怪问题;由于某种原因,当您使用
extend
时,它会操纵数据限制,因此我们需要将它们重置回我们想要的状态。
另外,仅作为一种风格问题,你不应该做像
Colormap.set_under(cmap,color='k')
这样的事情。这是调用类方法并明确传递实例的一种奇怪方式。只需执行
cmap.set_under(color="k")
即可。
cs = m.contourf(x,y,data,clevs,cmap=pyplot.cm.Spectral)
这是我刚刚尝试的内容:cmap=matplotlib.colors.Colormap('Spectral') matplotlib.colors.Colormap.set_under(cmap,color=(0.4667,0,0.5333)) cs = m.contourf(x,y,data,clevs,cmap=cmap)
这导致了以下错误:File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 943, in _process_colors self.monochrome = self.cmap.monochrome AttributeError: Colormap instance has no attribute 'monochrome'
- Levi Cowancmap = pyplot.cm.Spectral
,然后执行cmap.set_under(...)
。这将修改Spectral colormap以具有指定的下限值。请注意,由于您正在修改实际的Spectral colormap对象,因此这将在所有使用Spectral colormap的后续绘图中保持有效。 - BrenBarnfrom matplotlib import pyplot as plt from numpy import arange,meshgrid from matplotlib.colors import Colormap x,y = arange(-10,10),arange(-10,10) x,y=meshgrid(x,y) z = x**2+y**2 clevs=arange(50,220,20) cmap=plt.cm.jet Colormap.set_under(cmap,color='k') cs = plt.contourf(x,y,z,clevs,cmap=cmap) plt.colorbar(cs) plt.show()
- Levi Cowan