如何为数据框分组条形图或数组分组条形图添加误差条?
我在制作一个分组柱状图时遇到了一些麻烦,花了好几天试图弄明白怎么加上误差条,现在我觉得是时候请教一下这里的专家们了(拜托了!)
经过计算、排序和数据处理后,我把绘图部分的代码贴在下面。这个代码可以运行,你可以直接复制粘贴到你喜欢的编辑器里。我想在这个 df 图上加上误差条,误差条的值可以来自 df2 或 e1。我并不一定要用数据框(Data Frame)来绘图,但这是我唯一能让柱状图以分组格式显示的方法。有没有人能帮我用下面的数据在分组图上加上误差条?我非常感谢任何人提供的帮助!:-)
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()
1 个回答
2
如果有人以后遇到这个问题,我想分享一下我最终找到的解决办法。显然,在早期版本的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()