如何从matplotlib / seaborn图中删除或隐藏y轴刻度标签? [英] How to remove or hide y-axis ticklabels from a matplotlib / seaborn plot?

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

问题描述

我制作了一个看起来像这样的情节



我想转身沿y轴剔除刻度标签。为此,我正在使用

  plt.tick_params(labelleft = False,left = False)

现在情节看起来像这样。即使标签已关闭,刻度 1e67 仍然保留。


关闭比例尺 1e67 即可绘制看上去好些。我该怎么办?

解决方案


  • seaborn 用于绘制图,但这只是 matplotlib 的高级API。

    • 用来删除y轴标签和刻度的函数是 matplotlib 方法。



  • 创建绘图后,使用 .set()

  • .set(yticklabels = [])应该删除刻度标签。

    • 如果使用 .set_title(),则此方法不起作用,但可以使用 .set(title ='')



  • .set (ylabel = None)应该删除轴标签。

  • .tick_params(left = False)

  • 类似地,对于x轴:


    删除标签


      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(yticklabels = [])#删除刻度线标签
    g1.set(title ='锻炼:按锻炼类型的时间脉动')#添加标题
    g1。 set(ylabel = None)#删除轴标签

    g2 = sns.boxplot(x ='species',y ='body_mass_g',hue ='sex',data = pen,ax = ax [1])$ ​​b
    $ b g2.set(yticklabels = [])
    g2.set(title ='企鹅:按性别划分的体重')
    g2.set( ylabel = None)#删除y轴标签
    g2.tick_params(left = False)#删除刻度线

    plt.tight_layout()
    plt.show()


    示例2


     将numpy导入为np 
    import matplotlib .pyplot as plt
    进口熊猫as pd

    #正弦采样数据
    sample_length = range(1,1 + 1)#频率列数
    rads = np.arange(0,2 * np.pi,0.01)
    data = np.array([[np.cos(t * rads)* 10 ** 67)+ 3 * 10 ** 67 for t in sample_length])
    df = pd.DataFrame(data.T,index = pd.Series(rads.tolist(),name ='radians'),column = [f'freq:{i} x'for i in sample_length])
    df.reset_index(inplace = True)

    #图
    图,ax = plt.subplots(figsize =(8,8))
    ax.plot('radians','freq:1x',data = df)


    删除标签


     #图
    图,ax = plt.subplots(figsize =(8,8))
    ax.plot('radians','freq:1x',data = df)
    ax.set(yticklabels = [])#删除刻度标签
    ax.tick_params(left = False)#删除刻度


    I made a plot that looks like this

    I want to turn off the ticklabels along the y axis. And to do that I am using

    plt.tick_params(labelleft=False, left=False)
    

    And now the plot looks like this. Even though the labels are turned off the scale 1e67 still remains.

    Turning off the scale 1e67 would make the plot look better. How do I do that?

    解决方案

    • seaborn is used to draw the plot, but it's just a high-level API for matplotlib.
      • The functions called to remove the y-axis labels and ticks are matplotlib methods.
    • After creating the plot, use .set().
    • .set(yticklabels=[]) should remove tick labels.
      • This doesn't work if you use .set_title(), but you can use .set(title='')
    • .set(ylabel=None) should remove the axis label.
    • .tick_params(left=False) will remove the ticks.
    • Similarly, for the x-axis: How to remove or hide x-axis labels from a seaborn / matplotlib plot?

    Example 1

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

    Remove Labels

    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(yticklabels=[])  # remove the tick labels
    g1.set(title='Exercise: Pulse by Time for Exercise Type')  # add a title
    g1.set(ylabel=None)  # remove the axis label
    
    g2 = sns.boxplot(x='species', y='body_mass_g', hue='sex', data=pen, ax=ax[1])
    
    g2.set(yticklabels=[])  
    g2.set(title='Penguins: Body Mass by Species for Gender')
    g2.set(ylabel=None)  # remove the y-axis label
    g2.tick_params(left=False)  # remove the ticks
    
    plt.tight_layout()
    plt.show()
    

    Example 2

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    # sinusoidal sample data
    sample_length = range(1, 1+1) # number of columns of frequencies
    rads = np.arange(0, 2*np.pi, 0.01)
    data = np.array([(np.cos(t*rads)*10**67) + 3*10**67 for t in sample_length])
    df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=[f'freq: {i}x' for i in sample_length])
    df.reset_index(inplace=True)
    
    # plot
    fig, ax = plt.subplots(figsize=(8, 8))
    ax.plot('radians', 'freq: 1x', data=df)
    

    Remove Labels

    # plot
    fig, ax = plt.subplots(figsize=(8, 8))
    ax.plot('radians', 'freq: 1x', data=df)
    ax.set(yticklabels=[])  # remove the tick labels
    ax.tick_params(left=False)  # remove the ticks
    

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

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