将 alpha 区域(置信区间)添加到 sns 点图

Adding alpha area (confidence interval) to sns pointplot

提问人:thesecond 提问时间:11/10/2023 最后编辑:toyota Suprathesecond 更新时间:11/10/2023 访问量:40

问:

假设一个数据帧

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]})

和一个情节:

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))

看起来像这样

enter image description here

我想做的是添加一个基于列和“上部”的置信区间,例如,看起来像这样(对于蓝色曲线)Lower

enter image description here

可能吗?

Python 熊猫 matplotlib seaborn 可视化

评论


答:

1赞 Ömer Sezer 11/10/2023 #1

为了获得类似的蓝色区域,需要更新下限值和上限值。绘图部分可以用 处理。matplotlib.pyplot

法典:

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()

输出:

enter image description here

评论

0赞 thesecond 11/10/2023
谢谢!对于不均匀分布的 X 是否有可能这样做?
0赞 Ömer Sezer 11/10/2023
欢迎您的光临。重要的一点是,X的长度,下部和上部应该相同(否则会给出错误)。不可能均匀分布 X。您可以对其进行测试(例如,model_a_x[3]=4,在plt.fill_between之前)。为了更好地理解,请使用 prints 表示下、上和 X.(例如 print(model_a_lower))。