好吧,要做到这一点肯定不止一种方法。在这种情况下,
由于只需要三种颜色,我会选择自己的颜色创建一个
LinearSegmentedColormap而不是使用“cubehelixu palete”生成它们。
如果有足够的颜色来保证使用“cubehelixu调色板”,我会的
使用
cbar_kws参数。无论哪种方式,都可以使用手动指定记号
设置记号和标签。下面的代码示例演示如何手动创建LinearSegmentedColormap`,并包含有关如何指定边界的注释
如果改用“cubehelixu palete”。
import matplotlib.pyplot as pltimport pandasimport seaborn.apionly as snsfrom matplotlib.colors import LinearSegmentedColormapsns.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 generatedcolorbar = ax.collections[0].colorbarcolorbar.set_ticks([-0.667, 0, 0.667])colorbar.set_ticklabels(['B', 'A', 'C'])# X - Y axis labelsax.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()