【发布时间】:2014-10-28 17:03:24
【问题描述】:
我在必须制作的分组条形图上获取错误栏时遇到了一些麻烦,在花了几天时间试图弄清楚如何做到这一点之后,我认为是时候向这里的专家寻求帮助了(请!)
在进行计算、排序和数据缩减之后,我将代码的绘图部分发布在下面。它运行,您可以将其剪切并粘贴到您最喜欢的编辑器中。我想使用 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()
【问题讨论】:
标签: python matplotlib pandas plot