我有一个seaborn热图,我正在建立一个价值矩阵。矩阵的每个元素对应于我想要为矩阵中的每行/列制作刻度标签的权利。
我尝试使用ax.set_xticklabel()
函数来实现这一点,但似乎什么也没做。这是我的代码:
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()))
仍然有矩阵索引作为标签:
任何有关如何正确更改这些标签的想法都将非常感激。
编辑:如果这很重要,我正在使用jupyter笔记本。
您正在设置刚刚创建的轴的x和y刻度标签。然后您正在绘制seaborn热图,它将覆盖您刚设置的刻度标签。
解决方案是首先创建热图,然后设置刻度标签:
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)