【问题标题】:Error Bars not displaying Seaborn Relplot错误栏不显示 Seaborn Relplot
【发布时间】:2021-12-29 19:10:30
【问题描述】:

使用此代码,我创建了一个 seaborn 图来可视化长格式数据集中的多个变量。

import pandas as pd
import seaborn as sns

data = {'Patient ID': [11111, 11111, 11111, 11111, 22222, 22222, 22222, 22222, 33333, 33333, 33333, 33333, 44444, 44444, 44444, 44444, 55555, 55555, 55555, 55555],
        'Lab Attribute': ['% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)'],
        'Baseline': [46.0, 94.0, 21.0, 18.0, 56.0, 104.0, 31.0, 12.0, 50.0, 100.0, 33.0, 18.0, 46.0, 94.0, 21.0, 18.0, 46.0, 94.0, 21.0, 18.0],
        '3 Month': [33.0, 92.0, 19.0, 25.0, 33.0, 92.0, 21.0, 11.0, 33.0, 102.0, 18.0, 17.0, 23.0, 82.0, 13.0, 17.0, 23.0, 82.0, 13.0, 17.0],
        '6 Month': [34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0]}

df = pd.DataFrame(data)

# reshape the dataframe
dfm = df_labs.melt(id_vars=['Patient_ID', 'Lab_Attribute'], var_name='Months')

# change the Months values to numeric
dfm.Months = dfm.Months.map({'Baseline': 0, '3 Month': 3, '6 Month': 6})

# plot a figure level line plot with seaborn
p = sns.relplot(data=dfm, col='Lab_Attribute', x='Months', y='value', hue='Patient_ID', kind='line', col_wrap=5, marker='o', palette='husl',facet_kws={'sharey': False, 'sharex': True},err_style="bars", ci=95,)

plt.savefig('gmb_nw_labs.jpg')

绘图效果很好,但由于某种原因,即使在添加后也没有显示错误栏:

err_style="bars", ci=95,

到 sns.replot()

p = sns.relplot(data=dfm, col='Lab_Attribute', x='Months', y='value', hue='Patient_ID', kind='line', col_wrap=5, marker='o', palette='husl',facet_kws={'sharey': False, 'sharex': True},err_style="bars", ci=95,)

谁能告诉我这是为什么,我的数据集中的数据点可能太少了吗?

【问题讨论】:

    标签: python pandas matplotlib seaborn errorbar


    【解决方案1】:
    • 每个数据点都由hue 分隔,因此没有错误栏,因为没有合并数据。删除 hue='Patient ID' 以仅显示平均线和误差线。
    • 或者,seaborn.lineplot 可以映射到seaborn.relplot。通过不指定hue,API 将创建错误栏
      • 指定了linestyle='',因此不绘制平均线
    • python 3.8.12pandas 1.3.4matplotlib 3.4.3seaborn 0.11.2中测试
    data = {'Patient ID': [11111, 11111, 11111, 11111, 22222, 22222, 22222, 22222, 33333, 33333, 33333, 33333, 44444, 44444, 44444, 44444, 55555, 55555, 55555, 55555],
            'Lab Attribute': ['% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)', '% Saturation- Iron', 'ALK PHOS', 'ALT(SGPT)', 'AST (SGOT)'],
            'Baseline': [46.0, 94.0, 21.0, 18.0, 56.0, 104.0, 31.0, 12.0, 50.0, 100.0, 33.0, 18.0, 46.0, 94.0, 21.0, 18.0, 46.0, 94.0, 21.0, 18.0],
            '3 Month': [33.0, 92.0, 19.0, 25.0, 33.0, 92.0, 21.0, 11.0, 33.0, 102.0, 18.0, 17.0, 23.0, 82.0, 13.0, 17.0, 23.0, 82.0, 13.0, 17.0],
            '6 Month': [34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0, 34.0, 65.0, 10.0, 14.0]}
    
    df = pd.DataFrame(data)
    
    # reshape the dataframe
    dfm = df.melt(id_vars=['Patient ID', 'Lab Attribute'], var_name='Months')
    
    # change the Months values to numeric
    dfm.Months = dfm.Months.map({'Baseline': 0, '3 Month': 3, '6 Month': 6})
    
    # plot a figure level line plot with seaborn
    p = sns.relplot(data=dfm, col='Lab Attribute', x='Months', y='value', hue='Patient ID', kind='line', col_wrap=3, marker='o', palette='husl', facet_kws={'sharey': False, 'sharex': True}, err_style="bars", ci=95,)
    p.map(sns.lineplot, 'Months', 'value',  linestyle='', err_style="bars", color='k')
    

    • 没有hue='Patient ID'的原始实现
    p = sns.relplot(data=dfm, col='Lab Attribute', x='Months', y='value', kind='line', col_wrap=3, marker='o', palette='husl', facet_kws={'sharey': False, 'sharex': True}, err_style="bars", ci=95)
    

    【讨论】:

    • 好的,升级 seaborn 和 matplotlib 并重新启动内核和瞧!完美运行。再次感谢您今天对我的帮助。这是我的数据分析技能向前迈出的一大步。
    猜你喜欢
    • 2022-09-30
    • 2019-02-02
    • 2020-08-26
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多