英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论