您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

海本热图图中的离散图例

海本热图图中的离散图例

好吧,要做到这一点肯定不止一种方法在这种情况下, 由于只需要三种颜色,我会选择自己的颜色创建一个LinearSegmentedColormap而不是使用“cubehelix\u palete”生成它们。 如果有足够的颜色来保证使用“cubehelix\u调色板”,我会的 使用cbar_kws参数。无论哪种方式,都可以使用手动指定记号设置记号和标签。 下面的代码示例演示如何手动创建LinearSegmentedColormap`,并包含有关如何指定边界的注释 如果改用“cubehelix\u palete”。

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()
其他 2022/1/1 18:40:36 有416人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶