且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为Seaborn热图分配特定的颜色

更新时间:2022-05-24 23:04:09

df.set_index('TAG',inplace = True)告诉seaborn标记应该用作标记,而不是数据

df.set_index('TAG', inplace=True) tells seaborn that the tags should be used as tags, not as data.

二进制"颜色图从较低值的白色平滑过渡到最高值的深黑色.玩 vmin vmax ,将 vmin = 0 vmax 设置为1.5到大约5之间的值0将为白色,而1将为任何所需的灰色类型.

The 'binary' colormap goes smoothly from white for the lower values to dark black for the highest. Playing with vmin and vmax, setting vmin=0 and vmax to a value between 1.5 and about 5, value 0 will be white and 1 will be any desired type of gray.

要设置遮罩,数据框应转换为2D numpy数组,并且类型应为float.

To set a mask, the dataframe should be converted to a 2D numpy array and be of type float.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from io import StringIO

data_str = StringIO('''TAG     A   B   C   D   E   F   G   H   I   J
TAG_1   1   0   0   5   0   7   1   1   0   10
TAG_2   0   1   0   6   0   6   0   0   0   7
TAG_3   0   1   0   2   0   4   0   0   1   4
TAG_4   0   0   0   3   1   3   0   0   0   10
TAG_5   1   0   1   5   0   2   1   1   0   11
TAG_6   0   0   0   0   0   0   0   0   0   12
TAG_7   0   1   0   0   1   0   0   0   0   0
TAG_8   0   0   0   1   0   0   1   0   1   0
TAG_9   0   0   1   0   0   0   0   0   0   0
TAG_10  0   0   0   0   0   0   0   0   0   0''')

df = pd.read_csv(data_str, delim_whitespace=True)
df.set_index('TAG', inplace=True)
values = df.to_numpy(dtype=float)
ax = sns.heatmap(values, cmap='Reds', vmin=0, vmax=15, square=True)
sns.heatmap(values, xticklabels=df.columns, yticklabels=df.index,
            cmap=plt.get_cmap('binary'), vmin=0, vmax=2, mask=values > 1, cbar=False, ax=ax)
plt.show()

或者,可以创建自定义颜色图.这样,颜色栏也将显示调整后的颜色.

Alternatively, a custom colormap could be created. That way the colorbar will also show the adapted colors.

from matplotlib.colors import LinearSegmentedColormap

cmap_reds = plt.get_cmap('Reds')
num_colors = 15
colors = ['white', 'grey'] + [cmap_reds(i / num_colors) for i in range(2, num_colors)]
cmap = LinearSegmentedColormap.from_list('', colors, num_colors)
ax = sns.heatmap(df, cmap=cmap, vmin=0, vmax=num_colors, square=True, cbar=False)
cbar = plt.colorbar(ax.collections[0], ticks=range(num_colors + 1))
plt.show()