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