区分 `colorbar` 中的剪切值

Distinguishing clipped values in `colorbar`

我有一个 pcolormesh 图,其中通过将 vminvmax 设置为严格在绘制值的范围内来裁剪颜色图。有没有办法让关联的 colorbar 从底部和顶部分离一个块,以表示相关颜色超出了颜色图的范围?

是的,你需要对colorbar使用extend = both关键字,然后为pcolormesh对象的colormap设置上下颜色

import matplotlib.pyplot as plt
import numpy as np

data=np.random.rand(10,10)

fig=plt.figure()
ax=fig.add_subplot(111)

p=ax.pcolormesh(data,vmin=0.2,vmax=0.8,cmap='gray')

p.cmap.set_over('red')
p.cmap.set_under('blue')

fig.colorbar(p,extend='both')

plt.show()