I'm trying to make a line plot with a smooth looking confidence interval. Something that looks like this:
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.
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.
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.
You could set sns. lineplot(..., ci=None) to suppress the confidence interval.
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.
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)
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