如何从Seaborn/Matplotlib图中删除或隐藏X轴标签 [英] How to remove or hide x-axis labels from a seaborn / matplotlib plot

查看:188
本文介绍了如何从Seaborn/Matplotlib图中删除或隐藏X轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个箱线图,需要删除 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-类型(0 =非订户,1 =订户)')sb.boxplot(x="member_gender", y="Seconds", data=df, color = default_color, ax = ax[1,0], sym='').set_title('Gender (0=Male, 1=女性,2=其他)')

解决方案

  • 创建箱线图后,使用 .set().
  • .set(xticklabels = [])应该删除刻度标签.
    • 如果您使用 .set_title(),则此方法无效,但是您可以使用 .set(title ='')
  • .set(xlabel = None)应该删除轴标签.
  • .tick_params(bottom=False) 将删除刻度.
  • 类似地,对于y轴:

    没有 xticks 和 xlabel

    fig, ax = plt.subplots(2, 1, figsize=(8, 8))g1 = sns.boxplot(x ='time',y ='pulse',hue ='kind',data = exercise,ax = ax [0])g1.set(xticklabels = [])#删除刻度线标签g1.set(title='Exercise: Pulse by Time for Exercise Type') # 添加标题g1.set(xlabel = None)#删除轴标签g2 = sns.boxplot(x='物种', y='body_mass_g', 色调='sex', 数据=笔, ax=ax[1])g2.set(xticklabels=[])g2.set(title ='企鹅:按性别分类的体重')g2.set(xlabel=None)g2.tick_params(bottom=False) # 删除刻度plt.show()

    I have a boxplot and need to remove the x-axis ('user_type' and 'member_gender') label. How do I do this given the below format?

    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)')
    

    解决方案

    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 = sb.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)
    

    Example

    With xticks and xlabel

    import seaborn as sns
    import matplotlib.pyplot as plt
    
    # load data
    exercise = sns.load_dataset('exercise')
    pen = sns.load_dataset('penguins')
    
    # create figures
    fig, ax = plt.subplots(2, 1, figsize=(8, 8))
    
    # plot data
    g1 = sns.boxplot(x='time', y='pulse', hue='kind', data=exercise, ax=ax[0])
    
    g2 = sns.boxplot(x='species', y='body_mass_g', hue='sex', data=pen, ax=ax[1])
    
    plt.show()
    

    Without xticks and xlabel

    fig, ax = plt.subplots(2, 1, figsize=(8, 8))
    
    g1 = sns.boxplot(x='time', y='pulse', hue='kind', data=exercise, ax=ax[0])
    
    g1.set(xticklabels=[])  # remove the tick labels
    g1.set(title='Exercise: Pulse by Time for Exercise Type')  # add a title
    g1.set(xlabel=None)  # remove the axis label
    
    g2 = sns.boxplot(x='species', y='body_mass_g', hue='sex', data=pen, ax=ax[1])
    
    g2.set(xticklabels=[])  
    g2.set(title='Penguins: Body Mass by Species for Gender')
    g2.set(xlabel=None)
    g2.tick_params(bottom=False)  # remove the ticks
    
    plt.show()
    

    这篇关于如何从Seaborn/Matplotlib图中删除或隐藏X轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆