Skip to main content
 首页 » 编程设计

python之如何将误差线添加到数据框分组条形图甚至数组分组条形图

2025年05月04日6soundcode

我在必须制作的分组条形图上获取误差条时遇到了一些麻烦,在花了几天时间试图弄清楚如何做到这一点之后,我认为是时候向这里的专家寻求帮助了(非常感谢!)

在进行计算、排序和数据缩减之后,我在下面发布了代码的绘图部分。它会运行,您可以将其剪切并粘贴到您喜欢的编辑器中。我想使用 df2 或 e1 中的值将误差线添加到 df 图。我不同意将此作为数据框图的想法,但这是我能够以组格式绘制条形图的唯一方法。比我更了解 Python 的人能否帮助获得分组图,并从下面包含的数据中添加误差线。我很感激任何人都可以提供的帮助! :-)

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from pylab import * 
 
opacity = 0.6 
 
Measured = (1010, 1119, 1124, 1852, 1862, 876, 889, 891, 873, 873, 872, 1900, 1890, 1901) 
 
C = (80,70,70,70,70,70) 
 
myarray = [[1009, 1010], 
 [1122, 1119, 1124], 
 [1842, 1852, 1862], 
 [881, 876, 889, 891], 
 [880, 873, 873, 872], 
 [1890, 1900, 1890, 1901]] 
 
e1 = [[4.3, 16.4], 
 [4.6, 16.8, 16.2], 
 [11.4, 14.3, 14.2], 
 [3.7, 11.4, 11.6, 11.6], 
 [3.9, 16.7, 17.2, 16.6], 
 [8.3, 13.4, 13.9, 13.6]] 
 
 
 
length = len(sorted(myarray,key=len, reverse=True)[0]) 
s=np.array([myarrayi+[None]*(length-len(myarrayi)) for myarrayi in myarray]) 
 
length2 = len(sorted(e1,key=len, reverse=True)[0]) 
e2=np.array([e1i+[None]*(length2-len(e1i)) for e1i in e1]) 
 
df = pd.DataFrame(s, columns=['Theo.', 'Exp1', 'Exp2', 'Exp3']) #DataFrame of the Theoritical and experimental values 
df2 = pd.DataFrame(e1, columns=['Theo.', 'Exp1', 'Exp2', 'Exp3']) #DataFrame of the error that I wan to add as error bars to the first data frame 
df.plot(kind='bar', color=['r', 'b', 'b', 'b'], alpha = opacity) 
 
plt.xlim([0, len(C)+0.25])  
plt.ylim([min(Measured)-500, max(Measured)+200]) # Uses the min and max values of the matrix elements to set the axis boundaries so the scaling is the same all of the time 
xlabel("Theoretical vs Experimental Values") 
ylabel("Arbitrary units") 
title('Comparison Data Grouped Plots', color='#000000', fontsize=18) 
plt.tight_layout() 
plt.show() 

请您参考如下方法:

如果有人曾经搜索过这个特定问题,我想发布最终有效的解决方案。显然,在 Pandas 的早期版本(我之前使用的是 0.013.1)中,尝试对第二个数据框进行操作时存在问题。在 0.15 中,它现在似乎可以工作了!因此,为了完整起见,这里是我能够用来绘制分组条形图以及每个单独测量值的相关误差的代码:

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from pylab import * 
 
opacity = 0.6 
 
Measured = (1010, 1119, 1124, 1852, 1862, 876, 889, 891, 873, 873, 872, 1900, 1890, 1901) 
 
C = (80,70,70,70,70,70) 
 
myarray = [[1009, 1010], 
 [1122, 1119, 1124], 
 [1842, 1852, 1862], 
 [881, 876, 889, 891], 
 [880, 873, 873, 872], 
 [1890, 1900, 1890, 1901]] 
 
e1 = [[4.3, 16.4], 
 [4.6, 16.8, 16.2], 
 [11.4, 14.3, 14.2], 
 [3.7, 11.4, 11.6, 11.6], 
 [3.9, 16.7, 17.2, 16.6], 
 [8.3, 13.4, 13.9, 13.6]] 
 
length = len(sorted(myarray,key=len, reverse=True)[0]) 
s=np.array([myarrayi+[None]*(length-len(myarrayi)) for myarrayi in myarray]) 
 
length2 = len(sorted(e1,key=len, reverse=True)[0]) 
e2=np.array([e1i+[None]*(length2-len(e1i)) for e1i in e1]) 
 
df = pd.DataFrame(s, columns=['Theo', 'Exp1', 'Exp2', 'Exp3']) #DataFrame of the Theoritical and experimental values 
df2 = pd.DataFrame(e1, columns=['Theo', 'Exp1', 'Exp2', 'Exp3']) #DataFrame of the error that I wan to add as error bars to the first data frame 
df[['Theo', 'Exp1', 'Exp2', 'Exp3']].plot(kind='bar', yerr=df2[['Theo', 'Exp1', 'Exp2', 'Exp3']].values.T, color=['r', 'b', 'b', 'b'], alpha = opacity,error_kw=dict(ecolor='k')) 
 
 
plt.xlim([-.5, len(C)-.5])  
plt.ylim([min(Measured)-500, max(Measured)+200]) # Uses the min and max values of the matrix elements to set the axis boundaries so the scaling is the same all of the time 
xlabel("Theoretical vs Experimental Values") 
ylabel("Arbitrary units") 
title('Comparison Data Grouped Plots', color='#000000', fontsize=18) 
plt.tight_layout() 
plt.show()