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)
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.
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.
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.
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With