我在制作组合条形图上的错误栏时遇到一些麻烦,在花了几天试图弄清楚如何做之后,我想是时候向专家们求助了一下(非常请!)
在进行计算,排序和数据缩减之后,我发布了以下代码的绘图部分。它运行,您可以将其剪切并粘贴到您喜欢的编辑器中。我想使用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()
答案 0 :(得分:1)
如果其他人曾经搜索过这个特定问题,我想发布最终有效的解决方案。显然,在早期版本的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()