matplotlib 每行颜色不同的热图[重复]

y4ekin9u  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(132)
    • 此问题在此处已有答案**:

Annotated heatmap with multiple color schemes(2个答案)
How to create a heat-map with multiple colormaps?(2个答案)
Heatmap with multiple colormaps by column(2个答案)
2天前关闭。
我正在尝试用seaborn构建一个热图,"表"的每一行都有不同的颜色范围。例如:A:黄色B:蓝色C:绿色D:红色
必须在同一个表中,以便比较一年中自己的行和同一个月中的不同行。我的问题清楚了吗,非常感谢?
我只能绘制热图,比较所有表格:

d = {'jan': [44, 2, 3, 4],
'feb': [33, 4, 6, 8],
'mar': [50, 10, 15, 20],
'abr': [11, 12, 13, 14],
'mai': [3, 40, 6, 60],
'jun': [40, 8, 12, 16]}

idx = ['A', 'B', 'C', 'D']

df = pd.DataFrame(d, index = idx)

sns.heatmap(df, annot = True, cmap = 'Blues')
plt.yticks(rotation = 0)

这就是我想要策划的。用谷歌床单做的:

643ylb08

643ylb081#

我建议你每行画一个图,然后把它们放到plt子图中:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

d = {'jan': [44, 2, 3, 4], 'feb': [33, 4, 6, 8], 'mar': [50, 10, 15, 20], 'abr': [11, 12, 13, 14], 'mai': [3, 40, 6, 60], 'jun': [40, 8, 12, 16]}

idx = ['A', 'B', 'C', 'D']

df = pd.DataFrame(d, index = idx)
cm = ['Blues', 'Reds', 'Greens', 'Purples']
f, axs = plt.subplots(4, 1, gridspec_kw={'hspace': 0})

counter = 0
for index, row in df.iterrows():
    sns.heatmap(np.array([row.values]), yticklabels=[idx[counter]], xticklabels=df.columns, annot=True, fmt='.2f', ax=axs[counter], cmap=cm[counter], cbar=False)
    counter += 1

plt.show()
    • 输出:**

相关问题