【问题标题】:How to add more grids in seaborn barplot?如何在 seaborn barplot 中添加更多网格?
【发布时间】:2020-06-06 06:09:34
【问题描述】:
df.columns = ['feature','nunique']

sns.set(rc={'figure.figsize':(10,8)})
ax = sns.barplot(y='feature', x='nunique', data=df, orient = 'h')

如何将 nunique(x 轴)划分为更小的网格?

【问题讨论】:

    标签: python matplotlib plot seaborn


    【解决方案1】:

    您可以使用 matplotlib 中的ticker,所以从vignette 可以:

    类 matplotlib.ticker.MultipleLocator(base=1.0)

    在视图中基数的每个整数倍上设置一个刻度 间隔。

    我们需要将绘图分配给轴类并像这样修改它:

    import seaborn as sns
    import matplotlib.pyplot as plt
    import pandas as pd
    import matplotlib.ticker as ticker
    import numpy as np
    
    sns.set()
    tips = sns.load_dataset("tips")
    fig, ax = plt.subplots(1, 2,figsize=(10,4))
    
    sns.barplot(x="total_bill",y="day",data=tips,ax=ax[0],ci=None)
    ax[0].xaxis.set_major_locator(ticker.MultipleLocator(2.5))
    
    sns.barplot(x="total_bill",y="day",data=tips,ax=ax[1],ci=None)
    ax[1].xaxis.set_major_locator(ticker.MultipleLocator(1))
    

    【讨论】:

      【解决方案2】:

      your_ticks 更改为一个数组,其中包含您希望在 x 轴上显示的数字。

      fig, ax = plt.subplots()
      
      sns.barplot(y='feature', x='nunique', data=df, orient = 'h')
      
      ax.set_xticks(your_ticks)
      
      

      【讨论】:

        猜你喜欢
        • 2021-03-24
        • 1970-01-01
        • 2016-07-01
        • 2020-09-12
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        • 1970-01-01
        • 2017-06-29
        相关资源
        最近更新 更多