pythonmatplotlibseaborn

stacking rugplots in seaborn


Is there a way to stack rugplots in Seaborn as in here but below the x-axis?. I have not found anything on the rugplots documentation.


Solution

  • Creating the rugplots via seaborn, they can be placed into subplots. Here is an example:

    from matplotlib import pyplot as plt
    import seaborn as sns
    
    tips = sns.load_dataset('tips')
    
    days = tips['day'].unique()
    fig, axs = plt.subplots(nrows=5, figsize=(12, 12), sharex=True,
                            gridspec_kw={'hspace': 0, 'height_ratios': [6, 1, 1, 1, 1]})
    palette = sns.color_palette('tab10', n_colors=4)
    sns.kdeplot(data=tips, x='tip', hue='day', hue_order=days, fill=True, ax=axs[0])
    for day, color, ax in zip(days, palette, axs[1:]):
        # draw a rugplot for one specific day, occupying 95% of the plot's height
        sns.rugplot(data=tips[tips['day'] == day], x='tip', height=0.95, color=color, ax=ax)
        ax.set_ylabel(day)
        ax.set_yticks([])  # hide y ticks
        ax.set_facecolor('none')  # make transparent, to see all  x ticks
    plt.tight_layout()
    plt.subplots_adjust(bottom=0.15)
    plt.show()
    

    stacked rugplots