12

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

example
(source: pydata.org)

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
sickerin
  • 475
  • 1
  • 4
  • 15

1 Answers1

17

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

sickerin
  • 475
  • 1
  • 4
  • 15
  • 1
    Suggested edit queue is full so just a note that plt comes from import matplotlib.pyplot as plt (which can be non-obvious for some). – Rob Fisher Mar 09 '21 at 12:10