Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fill between two lineplots in seaborn?

Tags:

python

seaborn

I have a graph that plots Bayesian mean point data in a line with credible intervals around the mean value.

enter image description here

I'm trying to fill between to two credibility lines with a translucent color so the mean line really pops through. I've tried the following:

plt.fill_between(b.get_data(), c.data_get(), color='blue', alpha = .5)

I'm pulling this data out of an arviz inference set. Here is a toy dataset.

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
mean = np.array([861.98525 , 705.23875 , 640.14575 , 658.727625, 728.23775 ,
       792.4645  , 803.045375, 763.425875, 721.785375, 713.182375,
       740.543375, 781.466875])
confidence1 = np.array([788.   , 607.   , 493.   , 443.975, 435.975, 412.   , 366.975,
       295.   , 243.   , 207.   , 181.   , 161.   ])
confidence2 = np.array([ 938.   ,  811.   ,  815.   ,  935.025, 1150.025, 1391.05 ,
       1556.05 , 1624.05 , 1689.025, 1829.   , 2078.125, 2390.025])

date = list(df_sat_test['t'].unique())

fig = plt.figure(figsize=(15,4))
a=sns.lineplot(x =date, y = mean, label = 'Posterior Predictive')
b=sns.lineplot(x =date, y = confidence1, label = 'Confidence', color = 'r')
c=sns.lineplot(x =date, y = confidence2, label = 'Confidence', color = 'r')
# plt.fill_between(b.get_data(), c.data_get(), color='blue', alpha = .5)

sns.scatterplot(x =df_sat_test['t'], y = np.array(test_ppc.observed_data.obs), label = 'True Value')
plt.legend()
like image 376
Jordan Avatar asked Oct 23 '25 16:10

Jordan


1 Answers

If you get the last line graph, you will get data for three line graphs, which you can set to fill the first of the x-axis and y-axis and the second of the y-axis to fill to get the intended result.

fig = plt.figure(figsize=(15,4))
a=sns.lineplot(x=date, y=mean, label = 'Posterior Predictive')
b=sns.lineplot(x=date, y=confidence1, label='Confidence', color='r')
c=sns.lineplot(x=date, y=confidence2, label='Confidence', color='r')

line = c.get_lines()
plt.fill_between(line[0].get_xdata(), line[1].get_ydata(), line[2].get_ydata(), color='blue', alpha=.5)

#sns.scatterplot(x =df_sat_test['t'], y = np.array(test_ppc.observed_data.obs), label = 'True Value')
plt.legend()

plt.show()

enter image description here

like image 120
r-beginners Avatar answered Oct 26 '25 06:10

r-beginners



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!