Issue
Assume a dataframe
df = pd.DataFrame({"X" : [1, 1, 2, 2, 3, 3, 4, 4],
"Model" : ["A", "B", "A", "B", "A", "B", "A", "B"],
"Lower" : [0.2, 0.3, 0.2, 0.2, 0.25, 0.3, 0.3, 0.25],
"Median" : [0.5, 0.55, 0.6, 0.55, 0.5, 0.6, 0.5, 0.5],
"Upper" : [0.6, 0.7, 0.65, 0.7, 0.7, 0.65, 0.55, 0.7]})
and a plot:
pl1 = sns.catplot(data = df, kind = 'point',
hue = 'Model',
x = 'X',
y = 'Median', sharey = False, heigth = 3, aspect = 1.5)
pl1.set(ylim = (0, 1))
that looks like this
What I'd like to do, is to add a confidence interval based on columns Lower
and `Upper' that, for example, looks like this (for the blue curve)
Is it possible?
Solution
To get a similar blue area, Lower and upper values are updated. The plot part can be handled with matplotlib.pyplot
.
Code:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({"X": [1, 1, 2, 2, 3, 3, 4, 4],
"Model": ["A", "B", "A", "B", "A", "B", "A", "B"],
"Lower": [0.4, 0.3, 0.45, 0.2, 0.40, 0.3, 0.45, 0.25],
"Median": [0.5, 0.55, 0.6, 0.55, 0.5, 0.6, 0.5, 0.5],
"Upper": [0.6, 0.7, 0.65, 0.7, 0.65, 0.65, 0.65, 0.7]})
pl1 = sns.catplot(data=df, kind='point', hue='Model', x='X', y='Median', sharey=False, height=3, aspect=1.5, legend=False)
# fill the area between lower and upper for Model A in blue
model_a_lower = df[df['Model'] == 'A']['Lower'].values
model_a_upper = df[df['Model'] == 'A']['Upper'].values
model_a_x = df[df['Model'] == 'A']['X'].values
model_a_x = model_a_x - model_a_x[0] # to make it => model_a_x = [0, 1, 2, 3] due to fill_between function start and end points.
plt.fill_between(x=model_a_x, y1=model_a_lower, y2=model_a_upper, alpha=0.2, color='blue')
pl1.set(ylim=(0, 1))
plt.show()
Output:
Answered By - Ömer Sezer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.