重新标记海洋热图中的轴刻度 [英] Relabel axis ticks in seaborn heatmap
问题描述
我有一个极好的热图,该热图是根据值矩阵构建的.矩阵的每个元素对应于一个实体,我想为矩阵中的每个行/列打上勾号.
I have a seaborn heatmap that I am building from a matrix of values. Each element of the matrix corresponds to an entitiy that I would like to make the tick label for each row/col in the matrix.
我尝试使用ax.set_xticklabel()
函数来完成此操作,但它似乎无能为力.这是我的代码:
I tried using the ax.set_xticklabel()
function to accomplish this but it seems to do nothing. Here is my code:
type(jr_matrix)
>>> numpy.ndarray
jr_matrix.shape
>>> (15, 15)
short_cols = ['label1','label2',...,'label15'] # list of strings with len 15
fig, ax = plt.subplots(figsize=(13,10))
ax.set_xticklabels(tuple(short_cols)) # i also tried passing a list
ax.set_yticklabels(tuple(short_cols))
sns.heatmap(jr_matrix,
center=0,
cmap="vlag",
linewidths=.75,
ax=ax,
norm=LogNorm(vmin=jr_matrix.min(), vmax=jr_matrix.max()))
蒸馏器具有矩阵索引作为标签:
The still has the matrix indices as labels:
任何有关如何正确更改这些标签的想法将不胜感激.
Any ideas on how to correctly change these labels would be much appreciated.
如果重要的话,我正在使用jupyter笔记本进行此操作.
I am doing this using jupyter notebooks if that matters.
推荐答案
您正在设置刚创建的轴的x和y刻度标签.然后,您将绘制海上热图,该热图将覆盖刚刚设置的刻度线标签.
You are setting the x and y tick labels of the axis you have just created. You are then plotting the seaborn heatmap which will overwrite the tick labels you have just set.
解决方案是先创建热图,然后设置刻度标签:
The solution is to create the heatmap first, then set the tick labels:
fig, ax = plt.subplots(figsize=(13,10))
sns.heatmap(jr_matrix,
center=0,
cmap="vlag",
linewidths=.75,
ax=ax,
norm=LogNorm(vmin=jr_matrix.min(), vmax=jr_matrix.max()))
# passing a list is fine, no need to convert to tuples
ax.set_xticklabels(short_cols)
ax.set_yticklabels(short_cols)
这篇关于重新标记海洋热图中的轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!