Search code examples
pythongraphseabornswarmplot

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.


Solution

  • 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())