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

6.5k Views Asked by At

Let's say I have a df like this:

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

I would like to see a plot, where on the x axis, I have the col1 names ONCE, and on the y axis, the col2 data, as individual dots, so above 'a' I would have two dots at the height of 1 and 3, and above b I would have three dots at the heights of 1, 5 and 3. My main problem is that anything I try results in several a and b on the x axis, not grouped.

1

There are 1 best solutions below

0
On BEST ANSWER

Beeswarm, strip, and scatter plots are all options, depending on your data and preferred aesthetic.


plt.scatter or df.plot.scatter (most basic)

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


sns.swarmplot (avoid collisions)

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


sns.stripplot (random jitter)

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