【问题标题】:How to add error bars to a grouped bar plot?如何将误差线添加到分组条形图中?
【发布时间】:2020-12-31 02:16:58
【问题描述】:

我想在我的情节中添加错误栏,我可以显示每个情节的最小最大值。拜托,任何人都可以帮助我。提前致谢。

最小值最大值如下:

延迟 = (53.46(最小 0,最大 60),36.22(最小 12,最大 70),83(最小 21,最大 54),17(最小 12,最大 70)) 延迟 = (38 (min 2,max 70), 44 (min 12,max 87), 53 (min 9,max 60), 10 (min 11,max 77))

import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
from matplotlib.dates import date2num
import datetime

Delay = (53.46, 36.22, 83, 17)
Latency = (38, 44, 53, 10)
index = ['T=0', 'T=26', 'T=50','T=900']
df = pd.DataFrame({'Delay': Delay, 'Latency': Latency}, index=index)
ax = df.plot.bar(rot=0)
plt.xlabel('Time')
plt.ylabel('(%)')
plt.ylim(0, 101)
plt.savefig('TestX.png', dpi=300, bbox_inches='tight')
plt.show()

【问题讨论】:

    标签: python pandas matplotlib data-science


    【解决方案1】:
    • 为了在条形图上的正确位置进行绘图,必须提取每个条形的补丁数据。
    • 返回一个ndarray,每列一个matplotlib.axes.Axes
      • 在此图中,ax.patches 包含 8 个matplotlib.patches.Rectangle 对象,每个条形的每个段一个。
        • 通过使用该对象的关联方法,可以提取heightwidthx位置,并用于与plt.vlines绘制一条线。
    • 条形图的height 用于从dictz 中提取正确的minmax 值。
      • 很遗憾,补丁数据不包含条形标签(例如Delay & Latency)。
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # create dataframe
    Delay = (53.46, 36.22, 83, 17)
    Latency = (38, 44, 53, 10)
    index = ['T=0', 'T=26', 'T=50','T=900']
    df = pd.DataFrame({'Delay': Delay, 'Latency': Latency}, index=index)
    
    # dicts with errors
    Delay_error = {53.46: {'min': 0,'max': 60}, 36.22: {'min': 12,'max': 70}, 83: {'min': 21,'max': 54}, 17: {'min': 12,'max': 70}}
    Latency_error = {38: {'min': 2, 'max': 70}, 44: {'min': 12,'max': 87}, 53: {'min': 9,'max': 60}, 10: {'min': 11,'max': 77}}
    
    # combine them; providing all the keys are unique
    z = {**Delay_error, **Latency_error}
    
    # plot
    ax = df.plot.bar(rot=0)
    plt.xlabel('Time')
    plt.ylabel('(%)')
    plt.ylim(0, 101)
    
    for p in ax.patches:
        x = p.get_x()  # get the bottom left x corner of the bar
        w = p.get_width()  # get width of bar
        h = p.get_height()  # get height of bar
        min_y = z[h]['min']  # use h to get min from dict z
        max_y = z[h]['max']  # use h to get max from dict z
        plt.vlines(x+w/2, min_y, max_y, color='k')  # draw a vertical line
    

    • 如果两个dicts中有非唯一值,无法合并,我们可以根据柱状图顺序选择正确的dict
    • 首先绘制单个标签的所有条形图。
      • 在这种情况下,索引 0-3 是 Dalay 柱,索引 4-7 是 Latency
    for i, p in enumerate(ax.patches):
        print(i, p)
        x = p.get_x()
        w = p.get_width()
        h = p.get_height()
        
        if i < len(ax.patches)/2:  # select which dictionary to use
            d = Delay_error
        else:
            d = Latency_error
            
        min_y = d[h]['min']
        max_y = d[h]['max']
        plt.vlines(x+w/2, min_y, max_y, color='k')
    

    【讨论】:

    • 非常感谢特伦顿!这就是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2016-06-28
    • 1970-01-01
    • 2014-05-24
    • 2014-05-02
    • 2017-06-20
    • 2023-03-19
    相关资源
    最近更新 更多