如何从Seaborn Boxplot中删除或隐藏x轴标签?

时间:2019-10-20 19:43:10

标签: python-3.x pandas matplotlib seaborn

我有一个箱线图,需要删除x轴(“ user_type”和“ member_gender”)标签。给定以下格式,我该怎么办?

sb.boxplot(x="user_type", y="Seconds", data=df, color = default_color, ax = ax[0,0], sym='').set_title('User-Type (0=Non-Subscriber, 1=Subscriber)')
sb.boxplot(x="member_gender", y="Seconds", data=df, color = default_color, ax = ax[1,0], sym='').set_title('Gender (0=Male, 1=Female, 2=Other)')

1 个答案:

答案 0 :(得分:0)

  • set(xticklabels=[])应该删除刻度线标签
    • 如果您使用.set_title(),则无法使用,但是可以使用.set(title='')
  • set(xlabel=None)应该删除标签
fig, ax = plt.subplots(2, 1)

g1 = sb.boxplot(x="user_type", y="Seconds", data=df, color = default_color, ax = ax[0], sym='')
g1.set(xticklabels=[])
g1.set(title='User-Type (0=Non-Subscriber, 1=Subscriber)')
g1.set(xlabel=None)

g2 = .boxplot(x="member_gender", y="Seconds", data=df, color = default_color, ax = ax[1], sym='')
g2.set(xticklabels=[])
g2.set(title='Gender (0=Male, 1=Female, 2=Other)')
g2.set(xlabel=None)

示例

  • 我没有您的数据,所以使用其他一些数据

带有xticks和xlabel

fig, ax = plt.subplots(2, 1)

g1 = sns.boxplot(y='score', x='par', data=df, ax=ax[0])

g2 = sns.boxplot(y='score', x='par', data=df, ax=ax[1])

plt.show()

enter image description here

没有xticks和xlabel

fig, ax = plt.subplots(2, 1)

g1 = sns.boxplot(y='score', x='par', data=df, ax=ax[0])
g1.set(xticklabels=[])
g1.set(title='Test1')
g1.set(xlabel=None)


g2 = sns.boxplot(y='score', x='par', data=df, ax=ax[1])
g2.set(xticklabels=[])
g2.set(title='Test2')
g2.set(xlabel=None)

plt.show()

enter image description here