Home > Net >  How to plot a swarmplot of a grid of data, on one plot with seaborn
How to plot a swarmplot of a grid of data, on one plot with seaborn

Time:03-08

I having been trying for ages to plot this data the way I want on seaborn, from a pandas dataframe. Any advice would be appreciated.

The Data is in a dataframe and looks like this, with 12 months, and 11 columns plus a month column:

  Month   FSRPX1M     VOX1M     XLB1M  ...     XLP1M     XLU1M     XLV1M     XLY1M
0   Jan  0.087643 -0.561428 -0.409286  ... -0.177143  0.232858  0.521428  0.457857
1   Feb  0.132429 -0.265715  0.470715  ...  0.067142 -0.479286 -0.177143  0.363571
2   Mar  0.152429  1.002142  0.437857  ...  0.588573  0.671428  0.055000  0.727856
3   Apr  0.150071  2.445000  1.331428  ...  0.551428  1.100715  0.790715  2.175714
4   May -0.089429 -0.115714  0.275000  ...  0.240000  0.113572  0.650716 -0.366429

What I want to do is plot a swarm plot, with Months along the X Axis, and the number along the Y axis, with 11 'dots' for each month, representing the 11 columns, with an associated a color coded key.

I have tried various things including [where df is the dataframe and val_list is a list of the columns I want as dots]:

sns.swarmplot(data = df, x=df['Month'], y = df[val_list])

and

sns.swarmplot(data = df, x=df['Month'], hue = df[val_list])

as well as trying to slice the dataframe, and make a plot, one column at a time using a for loop:

for e in val_list:
    sns.stripplot(data = df, x=df['Month'], y = df[e])

Any help and explanation of what I'm doing wrong would be great, thanks

The last one makes a plot, but all the dots are the same color and unlabelled

CodePudding user response:

To work with hue, Seaborn needs the data in sns.swarmplot with long dataframe

  • Related