【问题标题】:Visualize sample size in multiple Seaborn Stripplot在多个 Seaborn Stripplot 中可视化样本大小
【发布时间】:2020-11-06 21:04:44
【问题描述】:
我正在尝试使用 Seaborn stripplot 可视化多个数据分布。该图提供了有关样本量的一些见解。但是当分布相似时,很难比较样本量。
例如,在下图中,阶段 7 和阶段 10 的分布非常相似,但阶段 7 有 5990 个值,阶段 10 有 32320 个。
我尝试了不同的 alpha 值来获得具有更多数据样本的较暗区域,但效果不佳。
有什么想法可以使用 stripplot 或任何其他可视化替代方法来区分多个分布中的样本大小吗?
谢谢!
帕特里西奥
【问题讨论】:
标签:
python
seaborn
data-visualization
【解决方案1】:
将两张seaborn的图放在matplotlib的GridSpec()中。一个是条形图,另一个是样本数量的条形图。
2行1列,高度比(4:1),上下间距:0
gridspec.GridSpec(ncols=1, nrows=2, height_ratios=[4, 1], hspace=0)
完整代码:
import matplotlib.pyplot as plt
from matplotlib import gridspec
import seaborn as sns
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
fig = plt.figure()
spec = gridspec.GridSpec(ncols=1, nrows=2, height_ratios=[4, 1], hspace=0)
ax0 = fig.add_subplot(spec[0])
sns.stripplot(x="day", y="total_bill", data=tips, jitter=0.4, ax=ax0)
ax1 = fig.add_subplot(spec[1])
sns.barplot(x=tips.day.unique(), y=tips.groupby('day')['total_bill'].count().tolist(), ax=ax1)
ax1.set_ylabel('counts')
plt.show()