【问题标题】:Python imshow cmap for Binary data用于二进制数据的 Python imshow cmap
【发布时间】:2021-06-23 18:39:34
【问题描述】:

我有一个二进制数据框。我想使用 imshow 将其显示并存储在 PDF 中。我想开一个定制的颜色,比如绿色代表真,红色代表假。 我的代码:

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()

预期情节:

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

【问题讨论】:

    标签: python dataframe matplotlib imshow


    【解决方案1】:

    cmap = 'gray' 表示明确使用白/黑渐变。

    您可以制作自己的地图

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

    然后在imshow中调用这个地图

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

    附:你可以在这里找到所有可以使用的地图颜色https://matplotlib.org/stable/tutorials/colors/colormaps.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 2018-11-25
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多