Python 二进制数据的 imshow cmap

Python imshow cmap for Binary data

我有一个二进制数据框。我想使用 imshow 以 PDF 格式显示和存储它。我想指定一种自定义颜色,比如 Green 代表 True,Red 代表 False。 我的代码:

df = 
       A         B          C         D          E            F
0     True       True       True     True        True        True
1     True       True       True     True        True        True
2     True      False      False     True        True        True
3     True       True       True     True        True        True
4     True       True       True     True        True        True
5    True       True       True     True        True        True
6   False      False       True    False       False       False
7    True       True       True     True        True        True

plt.imshow(df,interpolate='none',cmap='gray')
plt.show()

预期剧情:

在上图中,绿色代表白色,红色代表黑色。

cmap = 'gray' 表示明确使用 white/black 梯度。

您可以制作自己的地图

from matplotlib.colors import ListedColormap
cmp=ListedColormap(['red','green'])

然后在imshow中调用这个地图

plt.imshow(df,cmap=cmp)
plt.show()

P.S。您可以在此处找到可以使用的地图的所有颜色 https://matplotlib.org/stable/tutorials/colors/colormaps.html