Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn lineplot using median instead of mean

I'm using seaborn.lineplot() to create a line figure like this (a line representing mean, surrounded by a band representing the std):

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band", ci='sd', data=df)

enter image description here

My only issue is that since my data is not Gaussian, I care more about the median instead of mean. How to do that in Seaborn?

Or are there similar tools that are capable? I know I can do from scratch in matplotlib but that requires a lot of work to make it this nice.

like image 947
F.S. Avatar asked Sep 26 '18 20:09

F.S.


People also ask

How do I change my line style in Seaborn?

Customize line style by passing a dict mapping to “dashes” “dashes” parameter is used along with “style” parameter. In a dictionary, you set a “category to style” mapping. The style will be in a tuple (as discussed in above section). Pass this dictionary mapping to “dashes” parameter.

How do you get rid of the confidence interval in Seaborn?

By default, seaborn line plots show confidence intervals for the dataset. You can remove the confidence interval by setting the ci parameter of the lineplot() function to None as shown below.


2 Answers

estimator should be a pandas method.

Use estimator=np.median instead of estimator="median".

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band",
             ci='sd',
             estimator=np.median,
             data=df)
like image 189
Chang Ye Avatar answered Jan 02 '23 06:01

Chang Ye


From the documentation:

estimator : name of pandas method or callable or None, optional
Method for aggregating across multiple observations of the y variable at the same x level. If None, all observations will be drawn.

Hence try

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band", 
             ci='sd', estimator="median", data=df)

Here is a minimal example:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"trial" : np.random.randint(10, size=350),
                   "rvalues" : np.random.randn(350),
                   "subject" : np.random.randint(4, size=350)})

sns.lineplot(x="trial", y="rvalues", hue="subject", err_style="band", 
             ci='sd', estimator="median", data=df)
plt.show()

enter image description here

like image 21
ImportanceOfBeingErnest Avatar answered Jan 02 '23 05:01

ImportanceOfBeingErnest