1

I am relatively new to python and am currently trying to generate a scatterplot based off of some data using pandas & seaborn.

The data I'm using ('ErrorMedianScatter') is as follows (apologies for the link, I have yet to get permissions to embed images!):

Image of data

Each participant has two data points of interest. The mean when MissingLimb = 0 or 1

I want to create a scatterplot for participants where the x-axis represents their value for 'mean' when 'MissingLimb' = 0, and the y-axis represents their value for 'mean' when 'MissingLimb' = 1.

I am using the current code so far to create a scatterplot:

sns.lmplot(("mean",
       "mean",
       data=ErrorMedianScatter,
       fit_reg=False,
       hue="participant")

This generates a perfectly functional, but very uninteresting, scatterplot. What I'm stuck on is creating an x-/y-axis variable that allows for me to specify that I'm interested in the mean of a participant based on the value of 'MissingLimb' column.

Many thanks in advance!

MDhar94
  • 13
  • 3

1 Answers1

0

There are most likely multiple ways to solve your problem. The method I'd take is to first transform you dataset in such a way that there is a single row (observation) for each participant, and where (for each row) there is one column that reports the means where MissingLimb is 0 and another column that reports the means where MissingLimb is 1.

You can accomplish this data transformation with this code:

df = pd.pivot_table(ErrorMedianScatter, 
                    values='mean', 
                    index='participant', 
                    columns='MissingLimb')

df.columns = ['MissingLimb 0', 'MissingLimb 1']

You can then use this (transformed) dataframe to create the scatterplot:

sns.lmplot(data=df, x='MissingLimb 0', y='MissingLimb 1')

Notice that in addition to specifying the data to plot (using the data parameter), I also specified the data to plot on the x- and y-axis (using the x and y parameters, respectively). You can add additional arguments to the sns.lmplot call and customize the plot to your specifications.

Keith Dowd
  • 631
  • 4
  • 10