python - 如何从 matplotlib/seaborn 图中删除或隐藏 y 轴刻度标签

标签 python matplotlib plot seaborn data-visualization

我做了一个看起来像这样的情节
enter image description here
我想关闭沿 y 轴的刻度标签。要做到这一点,我正在使用

plt.tick_params(labelleft=False, left=False)
现在剧情是这样的。即使标签关闭了规模 1e67仍然存在。
enter image description here
关闭体重秤 1e67会让剧情更好看。我怎么做?

最佳答案

  • seaborn用于绘制绘图,但它只是 matplotlib 的高级 API .
  • 被调用以删除 y 轴标签和刻度的函数是 matplotlib方法。

  • 创建情节后 , 使用 .set() .
  • .set(yticklabels=[])应该删除刻度标签。
  • 如果您使用 .set_title(),这将不起作用,但您可以使用 .set(title='')

  • .set(ylabel=None)应该删除轴标签。
  • .tick_params(left=False)将去除蜱虫。
  • 同样,对于 x 轴:How to remove or hide x-axis labels from a seaborn / matplotlib plot?

  • 示例 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()
    
    enter image description here
    删除标签
    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()
    
    enter image description here
    示例 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)
    
    enter image description here
    删除标签
    # 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
    
    enter image description here

    关于python - 如何从 matplotlib/seaborn 图中删除或隐藏 y 轴刻度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63756623/

    相关文章:

    Python 循环 | "do-while"在树上

    python - Pylint:在 "else"(no-else-return)警告后禁用不必要的 "return"

    python - 如何使 ndimage.filters.maximum_filter 像 MATLAB 的 imregionalmax 函数一样工作?

    python - 如何修复左侧刻度? Matplotlib 和 Pandas

    python - 错误栏不在数据处

    javascript - Plotly - 根据值隐藏悬停工具提示上的数据?

    python - Scikit 学习多元线性回归和多项式特征的系数顺序

    python - 结合 seaborn 在 matplotlib 中自动选择线条样式的最可靠方法

    python - 绘制被数据中断的虚线(类似于等高线图)

    python - 在 python 上使用 graphviz 时如何设置节点的大小?