【问题标题】:How to centre align rotated yticklabels in seaborn?如何在seaborn中居中对齐旋转的yticklabels?
【发布时间】:2018-04-24 09:34:34
【问题描述】:

我试图通过 seaborn 绘制一个混淆矩阵,但当 yticklabels 旋转到 90 度时无法居中对齐它们。虽然 ha 可以在没有旋转的情况下工作,但会因旋转而失败。这是最小的工作示例 -

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

cmat = np.matrix([[2, 3], [4, 5]])
fig, ax = plt.subplots(figsize=(4, 4))

sns.heatmap(cm_greina, annot=True, xticklabels=['Faulty', 'Healthy'], cbar=False, ax=ax)
ax.set_yticklabels(['Faulty', 'Healthy'], rotation=90, ha='center')

任何帮助将不胜感激。

【问题讨论】:

  • 尝试添加rotation_mode='anchor'
  • 完美!那行得通!感谢您的及时回复。
  • 你们中的某个人会发布一个答案,这样这个问题就会被关闭吗?

标签: python matplotlib plot alignment seaborn


【解决方案1】:

您可能想要使用垂直对齐而不是水平对齐:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

cmat = np.matrix([[2, 3], [4, 5]])
fig, ax = plt.subplots(figsize=(4, 4))

sns.heatmap(cmat, annot=True, xticklabels=['Faulty', 'Healthy'], cbar=False, ax=ax)
ax.set_yticklabels(['Faulty', 'Healthy'], va='center', rotation = 90, position=(0,0.28))
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多