Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a seaborn lineplot with standard deviation/ confidence interval specified for each point

I'm trying to make a line plot with a smooth looking confidence interval. Something that looks like this:

example

Currently, what I've done is to use errorbars to show the confidence interval. So I have 100 (x,y) pairs and I pass it to sns.lineplot which creates a line for me, and then each of these points, I have standard deviation I want to plot Sigma_new_vec.

axs[(e-1)//2, (e-1)%2].errorbar(x, y ,yerr = Sigma_new_vec, linestyle="None")
sns.lineplot(x='x', y='y', data = predicted_line, ax= axs[(e-1)//2, (e-1)%])
sns.lineplot(x='x', y='y', data = true_line, ax = axs[(e-1)//2, (e-1)%2] )

So currently what I have looks something like this, where I have confidence intervals for each of the 100 points, but I would like it to be smoothened out. my example

like image 693
sickerin Avatar asked Oct 15 '19 16:10

sickerin


People also ask

How does seaborn calculate confidence interval?

That was about Matplotlib; Seaborn uses bootstrapping to calculate the 95% confidence interval of data. In essence, it's a method of repeatedly resampling from a sample of the population, which gives good estimates of the true mean and 95% confidence.

How do you draw a Lineplot?

To plot a line plot in Matplotlib, you use the generic plot() function from the PyPlot instance. There's no specific lineplot() function - the generic one automatically plots using lines or markers. This results in much the same line plot as before, as the values of x are inferred.

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

You could set sns. lineplot(..., ci=None) to suppress the confidence interval.

How do you put markers on seaborn Lineplot?

You can also plot markers on a Seaborn line plot. Markers are special symbols that appear at the places in a line plot where the values for x and y axes intersect. To plot markers, you have to pass a list of symbols in a list to the markers attribute. Each symbol corresponds to one line plot.


1 Answers

With @ImportanceOfBeingErnest's suggestion, I got it to work!

lower_bound = [M_new - Sigma for M_new, Sigma in zip(M_new_vec, Sigma_new_vec)]
upper_bound = [M_new + Sigma for M_new, Sigma in zip(M_new_vec, Sigma_new_vec)]
plt.fill_between(x_axis, lower_bound, upper_bound, alpha=.3)

If numpy is available:

import numpy as np
import matplotlib.pyplot as plt 

M_new_vec = np.array(M_new_vec)
Sigma_new_vec = np.array(Sigma_new_vec)

lower_bound = M_new_vec - Sigma_new_vec
upper_bound = M_new_vec + Sigma_new_vec

plt.fill_between(x_axis, lower_bound, upper_bound, alpha=.3)

enter image description here

like image 94
sickerin Avatar answered Sep 23 '22 22:09

sickerin