在seaborn中堆叠地毯

问题描述 投票:0回答:1

有没有办法在Seaborn中堆叠地毯图,如here但在x轴下方?我在地毯上没有找到任何东西文档

python matplotlib seaborn
1个回答
0
投票

通过seaborn创建地毯图,可以将它们放入子图中。这是一个例子:

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, 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

© www.soinside.com 2019 - 2024. All rights reserved.