Seaborn热图图中的离散图例 [英] Discrete legend in seaborn heatmap plot
问题描述
我正在使用此处提供的数据,通过seaborn和pandas构建此热图.
I am using the data present here to construct this heat map using seaborn and pandas.
代码:
import pandas
import seaborn.apionly as sns
# Read in csv file
df_trans = pandas.read_csv('LUH2_trans_matrix.csv')
sns.set(font_scale=0.8)
cmap = sns.cubehelix_palette(start=2.8, rot=.1, light=0.9, as_cmap=True)
cmap.set_under('gray') # 0 values in activity matrix are shown in gray (inactive transitions)
df_trans = df_trans.set_index(['Unnamed: 0'])
ax = sns.heatmap(df_trans, cmap=cmap, linewidths=.5, linecolor='lightgray')
# X - Y axis labels
ax.set_ylabel('FROM')
ax.set_xlabel('TO')
# Rotate tick labels
locs, labels = plt.xticks()
plt.setp(labels, rotation=0)
locs, labels = plt.yticks()
plt.setp(labels, rotation=0)
# revert matplotlib params
sns.reset_orig()
从csv文件中可以看到,它包含3个离散值:0,-1和1.我想要离散图例而不是颜色条.分别将0标记为A,将-1标记为B,将1标记为C.
As you can see from csv file, it contains 3 discrete values: 0, -1 and 1. I want a discrete legend instead of the colorbar. Labeling 0 as A, -1 as B and 1 as C. How can I do that?
推荐答案
当然,有多种方法可以实现此目的.在这种情况下,只需要三种颜色,我自己创建一个LinearSegmentedColormap
而不是用cubehelix_palette
生成它们来自己选择颜色.如果有足够的颜色需要使用cubehelix_palette
进行担保,则可以使用cbar_kws
参数的boundaries
选项在色图上定义线段.无论哪种方式,都可以使用set_ticks
和set_ticklabels
手动指定刻度.
Well, there's definitely more than one way to accomplish this. In this case, with only three colors needed, I would pick the colors myself by creating a LinearSegmentedColormap
instead of generating them with cubehelix_palette
. If there were enough colors to warrant using cubehelix_palette
, I would define the segments on colormap using the boundaries
option of the cbar_kws
parameter. Either way, the ticks can be manually specified using set_ticks
and set_ticklabels
.
下面的代码示例演示了LinearSegmentedColormap
的手动创建,并包含有关如何使用cubehelix_palette
来指定边界的注释.
The following code sample demonstrates the manual creation of LinearSegmentedColormap
, and includes comments on how to specify boundaries if using a cubehelix_palette
instead.
import matplotlib.pyplot as plt
import pandas
import seaborn.apionly as sns
from matplotlib.colors import LinearSegmentedColormap
sns.set(font_scale=0.8)
dataFrame = pandas.read_csv('LUH2_trans_matrix.csv').set_index(['Unnamed: 0'])
# For only three colors, it's easier to choose them yourself.
# If you still really want to generate a colormap with cubehelix_palette instead,
# add a cbar_kws={"boundaries": linspace(-1, 1, 4)} to the heatmap invocation
# to have it generate a discrete colorbar instead of a continous one.
myColors = ((0.8, 0.0, 0.0, 1.0), (0.0, 0.8, 0.0, 1.0), (0.0, 0.0, 0.8, 1.0))
cmap = LinearSegmentedColormap.from_list('Custom', myColors, len(myColors))
ax = sns.heatmap(dataFrame, cmap=cmap, linewidths=.5, linecolor='lightgray')
# Manually specify colorbar labelling after it's been generated
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([-0.667, 0, 0.667])
colorbar.set_ticklabels(['B', 'A', 'C'])
# X - Y axis labels
ax.set_ylabel('FROM')
ax.set_xlabel('TO')
# Only y-axis labels need their rotation set, x-axis labels already have a rotation of 0
_, labels = plt.yticks()
plt.setp(labels, rotation=0)
plt.show()
这篇关于Seaborn热图图中的离散图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!