【问题标题】:matplotlib heatmap with multiple cmap [duplicate]具有多个cmap的matplotlib热图[重复]
【发布时间】:2021-10-23 15:48:06
【问题描述】:

我想为某些行创建一个具有不同 cmap 的热图。例如,我希望基线行使用cmap='Blues',而其他行使用cmap='Reds'。到目前为止,我有以下内容。我不知道该怎么做。
谢谢。

import numpy as np
import matplotlib.pyplot as plt

y_ticks = ["f", "e", "d", "c",
              "b", "a", "baseline"]
x_ticks = ["A", "B", "C",
           "D", "E", "F", "G"]

data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
                    [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
                    [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
                    [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
                    [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
                    [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
                    [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])

data_1 = np.delete(data, (1,2,3,4,5,6), axis=0)
data_2 = np.delete(data, (0), axis=0)


fig, ax = plt.subplots()

im_1 = ax.imshow(data_1, cmap='Blues')
im_2 = ax.imshow(data_2, cmap='Reds')
im = np.vstack((im_1, im_2))


ax.set_xticks(np.arange(len(x_ticks)))
ax.set_yticks(np.arange(len(y_ticks)))

ax.set_xticklabels(x_ticks)
ax.set_yticklabels(y_ticks)


plt.setp(ax.get_xticklabels(), ha="right", rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
for i in range(len(y_ticks)):
    for j in range(len(x_ticks)):
        text = ax.text(j, i, data[i, j], ha="center", va="center")

fig.tight_layout()
plt.show()

【问题讨论】:

    标签: python numpy matplotlib matrix seaborn


    【解决方案1】:

    this answer 中所述,您可以拆分矩阵并屏蔽不需要的部分:

    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    y_ticks = ["f", "e", "d", "c", "b", "a", "baseline"]
    x_ticks = ["A", "B", "C", "D", "E", "F", "G"]
    
    data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
                        [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
                        [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
                        [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
                        [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
                        [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
                        [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
    
    data_1 = data.copy()
    data_1[6, :] = float('nan')
    
    data_2 = data.copy()
    data_2[:6, :] = float('nan')
    
    
    fig, ax = plt.subplots()
    
    sns.heatmap(ax = ax, data = data_1, annot = True, cmap = 'Reds')
    sns.heatmap(ax = ax, data = data_2, annot = True, cmap = 'Blues')
    
    ax.set_xticklabels(x_ticks)
    ax.set_yticklabels(y_ticks)
    
    fig.tight_layout()
    plt.show()
    


    将相同的概念应用于您的代码,而不使用seaborn.heatmap

    import numpy as np
    import matplotlib.pyplot as plt
    
    y_ticks = ["f", "e", "d", "c", "b", "a", "baseline"]
    x_ticks = ["A", "B", "C", "D", "E", "F", "G"]
    
    data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
                        [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
                        [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
                        [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
                        [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
                        [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
                        [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
    
    data_1 = data.copy()
    data_1[6, :] = float('nan')
    
    data_2 = data.copy()
    data_2[:6, :] = float('nan')
    
    
    fig, ax = plt.subplots()
    
    im_1 = ax.imshow(data_1, cmap='Reds')
    im_2 = ax.imshow(data_2, cmap='Blues')
    im = np.vstack((im_1, im_2))
    
    
    ax.set_xticks(np.arange(len(x_ticks)))
    ax.set_yticks(np.arange(len(y_ticks)))
    
    ax.set_xticklabels(x_ticks)
    ax.set_yticklabels(y_ticks)
    
    
    plt.setp(ax.get_xticklabels(), ha="right", rotation_mode="anchor")
    
    # Loop over data dimensions and create text annotations.
    for i in range(len(y_ticks)):
        for j in range(len(x_ticks)):
            text = ax.text(j, i, data[i, j], ha="center", va="center")
    
    fig.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-06
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2016-01-30
      • 2019-06-15
      • 1970-01-01
      相关资源
      最近更新 更多