如何在 matplotlib 或 seaborn 中绘制点图类型的散点图?

How to plot a dot plot type scatterplot in matplotlib or seaborn?

假设我有一个这样的 df:

df = pd.DataFrame({'col1': list('aabbb'), 'col2': [1, 3, 1, 5, 3]})

  col1  col2
0    a     1
1    a     3
2    b     1
3    b     5
4    b     3

我想看一个图,在 x 轴上,我有 col1 名称一次,在 y 轴上,col2 数据,作为单独的点,所以在 'a' 以上我会在 1 和 3 的高度处有两个点,在 b 之上,我将在 1、5 和 3 的高度处有三个点。 我的主要问题是,我尝试的任何操作都会在 x 轴上产生多个 a 和 b,而不是分组。

蜂群图、带状图和散点图都是选项,具体取决于您的数据和偏好的审美。


plt.scatter or df.plot.scatter(最基本的)

plt.scatter(data=df, x='col1', y='col2') # or df.plot.scatter(x='col1', y='col2')
plt.margins(x=0.5)


sns.swarmplot(避免碰撞)

sns.swarmplot(data=df, x='col1', y='col2')


sns.stripplot(随机抖动)

sns.stripplot(data=df, x='col1', y='col2')