gpt4 book ai didi

python - 具有多个 cmap 的 matplotlib 热图

转载 作者:行者123 更新时间:2023-12-04 13:07:07 29 4
gpt4 key购买 nike

<分区>

我想为某些行创建一个具有不同 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()

enter image description here

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com