I want to create a heatmap with different cmap for certain lines. For example I want that the baseline row uses cmap='Blues' while the other rows use cmap='Reds'. So far I have the following. I can't figure out how to do this.
Thanks.
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()

As explained in this answer, you can split the matrix and mask the unwanted parts:
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()

Applying the same concept to your code, without using 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()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With