【问题标题】:How to remove or hide x-axis labels from a seaborn / matplotlib plot如何从 seaborn / matplotlib 图中删除或隐藏 x 轴标签
【发布时间】:2020-02-16 23:22:23
【问题描述】:

我有一个箱线图,需要删除 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)')

【问题讨论】:

    标签: python python-3.x pandas matplotlib seaborn


    【解决方案1】:
    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)
    

    示例

    使用 xticks 和 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()
    

    没有 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=[])  # 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()
    

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 1970-01-01
      • 2022-10-31
      • 2016-09-22
      • 1970-01-01
      • 2020-12-21
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多