【问题标题】:How to add error bars to a Data Frame Grouped bar plot or even an array of arrays grouped bar plot?如何将误差线添加到数据框分组条形图甚至数组分组条形图中?
【发布时间】: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


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 1970-01-01
      • 2014-05-24
      • 2014-05-02
      • 2016-06-28
      • 1970-01-01
      • 2017-06-20
      • 2023-01-27
      • 2021-07-15
      相关资源
      最近更新 更多