Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a column-wise bee-swarm plot in Python

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.

like image 994
Aryagm Avatar asked May 22 '26 09:05

Aryagm


2 Answers

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())
like image 126
tomjn Avatar answered May 26 '26 19:05

tomjn


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

like image 34
Renan Lopes Avatar answered May 26 '26 20:05

Renan Lopes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!