3

For a minimal, reproducible example (reprex) let's assume I have reaction times (seconds) of animals in the following data structure, saved in a .csv file. The file is called "ReactionTimes.csv"

"Birds","Mammals"
1.15878,1.494555
1.418479,1.738676
1.034765,1.541106
1.310064,1.328025
1.087671,1.583186
1.001802,1.770486

So far I was not able to adapt the solution from here How to create swarm plot with matplotlib to my data structure, thus my code looks like this:

import pandas as pd
import pylab as plt
import seaborn as sns

RT = pd.read_csv('ReactionTimes.csv')

print(RT) prints the table correctly.

The commands sns.scatterplot(RT["Birds"],RT["Mammals"]) and sns.swarmplot(RT["Birds"],RT["Mammals"]) produce plots, but those don't look like the desired plot. The goal should look like this:

Comparative scatter plot

So how can I create a comparative scatter plot/swarm plot? I am fine with a solution using any of the libraries Pylab, Seaborn or Plotly.

PolII
  • 107
  • 8

2 Answers2

1

You need to melt the dataframe to go from a wide-form to a long-form frame. Seaborn works mostly with long-form frames.

u ="""Birds,Mammals
1.15878,1.494555
1.418479,1.738676
1.034765,1.541106
1.310064,1.328025
1.087671,1.583186
1.001802,1.770486"""

import io
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

dfin = pd.read_csv(io.StringIO(u))


df = pd.melt(dfin)

sns.swarmplot(x="variable", y="value", data=df)
sns.boxplot(x="variable", y="value", data=df,
            boxprops={'facecolor':'None'}, showfliers=False,)
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Aaahhh, melting the data frame and combining the swarmplot with the boxplot is a great solution. Thank you for your answer! It worked here for the reprex and also for my real, bigger dataset - thanks! – PolII Jul 24 '19 at 14:12
0

Here is what I did:

RT = {'Birds': [1.15878, 1.418479, 1.034765, 1.310064, 1.087671, 1.001802],
     'Mammals': [1.494555, 1.738676, 1.541106, 1.328025, 1.583186, 1.770486]}
RT = pd.DataFrame(data=RT)
ax = sns.swarmplot(data=RT[["Birds", 'Mammals']])
ax.set_ylabel('Reaction time [s]')
ax.set_xlabel('Species')
ax.tick_params(labelrotation = 45)

Output:

enter image description here

Is this what you want?

AlyM.Aly
  • 68
  • 1
  • 6
  • Thank you for your answer! This solution is closer to my goal than any of my attempts. Although, to reach the final goal, the additional plotting of statistical signs like median and quantiles is still missing. – PolII Jul 24 '19 at 11:49