为 python 中的每个子图创建一个灰度颜色条

Create a grayscale colorbar for each subplot in python

我有两个带有灰度阴影的多边形补丁图,每个补丁都添加到一个轴上。我想在每个子图下方添加一个颜色条。 我正在使用

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt 

poly=mpatches.Polygon(verts,color=rgb,ec='black') #create patch 
ax.add_patch(poly) #add patch to axis 

plt.set_cmap('gray') #grayscale colormap 

        #verts,rgb input created in above lines of code

颜色条应为灰度,范围为 [0,1],0 为黑色,0.5 为标记,1 为白色。我正在使用子图 (121) 和 (122)。 提前致谢。

要使用颜色条,您必须有某种 ScalarMappable 实例(用于 imshowscatter 等):

mappable = plt.cm.ScalarMappable(cmap='gray')
# the mappable usually contains an array of data, here we can
# use that to set the limits
mappable.set_array([0,1])   
ax.colorbar(mappable)