如何使用具有相同分组变量的两个数据框创建带有标准差的误差条图?

huangapple go评论79阅读模式
英文:

How to create an error bar plot with standard deviation from two data-frames with the same grouping variable?

问题

我正在使用一个数据集,需要构建类似于这个的误差条形图。

我有两个数据框,一个包含变量的均值,另一个包含标准差:

df_media = media_grupos[["LDH1", "LDH2", "CPK1", "CPK2", "AST1", "AST2", "FAL1", "FAL2", "PPT1", "PPT2", "GlutamatoDesh1", "GlutamatoDesh2", "Albumina1", "Albumina2"]]
df_desv = desv_grupos[["LDH1", "LDH2", "CPK1", "CPK2", "AST1", "AST2", "FAL1", "FAL2", "PPT1", "PPT2", "GlutamatoDesh1", "GlutamatoDesh2", "Albumina1", "Albumina2"]]

每个数据框都按两个组(1和2)进行分组,与关于饮食类型和LDH1、LDH2等的数据有关。
变量均值的数据框如下:

标准差的数据框如下:

由于饮食组是相同的,我不知道如何构建一个误差条形图,其中x轴显示饮食组,在条形上显示LDH1 vs LDH2等。

英文:

I'm working with a dataset where I have to build an error bar plot similar to this.

如何使用具有相同分组变量的两个数据框创建带有标准差的误差条图?

I have two data frames, one with the means of the variables and the other with the standard deviations:

df_media = media_grupos[["LDH1", "LDH2", "CPK1", "CPK2", "AST1", "AST2", "FAL1", "FAL2", "PPT1", "PPT2", "GlutamatoDesh1", "GlutamatoDesh2", "Albumina1", "Albumina2"]]
df_desv = desv_grupos[["LDH1", "LDH2", "CPK1", "CPK2", "AST1", "AST2", "FAL1", "FAL2", "PPT1", "PPT2", "GlutamatoDesh1", "GlutamatoDesh2", "Albumina1", "Albumina2"]]

Each data frame is grouped by two groups 1 and 2, related to the type of feeding in terms of cereal and LDH1, LDH2, etc... are the data before and after the treatment.
Data frame for the means of the variable is the following

如何使用具有相同分组变量的两个数据框创建带有标准差的误差条图?

And the data frame for the standard deviations is:

如何使用具有相同分组变量的两个数据框创建带有标准差的误差条图?

As the feeding groups are the same I do not know how to build an error bar plot with the standard deviation that shows in the x-axis the feeding group and in the bars the LDH1 vs LDH2, for example.

答案1

得分: 1

使用pandas,你可以这样做:

df_media.plot.bar(yerr=df_desv, y=["LDH1", "LDH2"])

但如果你可以使用其他包,比如 plotly ,就可以得到更具交互性的结果:

import plotly.graph_objects as go

fig = go.Figure()
for name in df_media.columns:
    fig.add_trace(
        go.Bar(
            x=["Group 1", "Group 2"],
            y=df_media[name],
            name=name,
            error_y=dict(type="data", array=df_desv[name]),
        )
    )
fig.show()
英文:

With pandas you can just do :

df_media.plot.bar(yerr=df_desv, y=["LDH1", "LDH2"])

But if you can use other packages, with plotly for example it is possible to have something more interactive :

import plotly.graph_objects as go

fig = go.Figure()
for name in df_media.columns:
    fig.add_trace(
        go.Bar(
            x=["Group 1", "Group 2"],
            y=df_media[name],
            name=name,
            error_y=dict(type="data", array=df_desv[name]),
        )
    )
fig.show()

huangapple
  • 本文由 发表于 2023年7月23日 19:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定