3

I have a sample dataset:

A   B   C
23  45  3
53  78  46
23  68  24
52  68  57
52  79  76
78  79  13

I want to plot a bee-swarm plot in which each column represents on swarm/section. Like: enter image description here

How can I achieve this? I tried this:

sns.swarmplot(y=A)

But it only gives the swarmplot of 1 attribute and contains no label for the group.

Aryagm
  • 530
  • 1
  • 7
  • 18

2 Answers2

5

Here you should try to get your DataFrame into a "long" format. You can do this with DataFrame.melt.

This will give you a dataframe like

   variable  value
0         A     23
1         A     53
2         A     23
3         A     52
4         A     52
5         A     78
6         B     45
7         B     78
8         B     68
9         B     68
10        B     79
11        B     79
12        C      3
13        C     46
14        C     24
15        C     57
16        C     76
17        C     13

Then you can plot it with Seaborn like so

sns.swarmplot(x="variable", y="value", data=df.melt())
tomjn
  • 5,100
  • 1
  • 9
  • 24
1
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

dataset = {
    'A': [23,53,23,52,52,78],
    'B': [45,78,68,68,79,79],
    'C': [3,46,24,57,76,13]
}

df = pd.DataFrame(dataset)

sns.swarmplot(x="variable", y="value", data=df.melt())
plt.show()

enter image description here

Renan Lopes
  • 411
  • 4
  • 16
  • 1
    Note that seaborn also accepts the wide form. So, here `sns.swarmplot(data=df)` (or `sns.swarmplot(data=df[['A','B','C']])` to select part of the columns) would also work. Wide form is quite limited though, for example it doesn't support hue. – JohanC Mar 23 '21 at 22:11