如何将误差线添加到分组的柱状图中? [英] How to add error bars to a grouped bar plot?
本文介绍了如何将误差线添加到分组的柱状图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在绘图中添加误差条,以显示每个绘图的最小最大值.拜托,任何人都可以帮助我.预先感谢.
最小最大值如下:
延迟=(53.46(最小0,最大60),36.22(最小12,最大70),83(最小21,最大54),17(最小12,最大70))延迟=(38(最小2,最大70),44(最小12,最大87),53(最小9,最大60),10(最小11,最大77))
将matplotlib.pyplot导入为plt将熊猫作为pd导入从熊猫导入DataFrame从matplotlib.dates导入date2num导入日期时间延迟=(53.46,36.22,83,17)延迟=(38,44,53,10)索引= ['T = 0','T = 26','T = 50','T = 900']df = pd.DataFrame({'Delay':Delay,'Latency':Latency},index = index)轴= df.plot.bar(rot = 0)plt.xlabel('时间')plt.ylabel('(%)')plt.ylim(0,101)plt.savefig('TestX.png',dpi = 300,bbox_inches ='tight')plt.show()
解决方案
- 为了在条形图上的正确位置上进行绘制,必须提取每个条的补丁数据.
- 返回一个
ndarray
,其中包含一个- 如果两个
dict
中存在非唯一值,因此无法将它们组合在一起,我们可以根据条形图的顺序选择正确的dict
. - 首先绘制单个标签的所有条形图.
- 在这种情况下,索引0-3是
Dalay
条,而索引4-7是Latency
条
- 在这种情况下,索引0-3是
对于i,p枚举(ax.patches):打印(i,p)x = p.get_x()w = p.get_width()h = p.get_height()如果我<len(ax.patches)/2:#选择要使用的词典d =延迟错误别的:d =延迟错误min_y = d [h] ['min']max_y = d [h] ['max']plt.vlines(x + w/2,min_y,max_y,color ='k')
I would like to add error bar in my plot that I can show the min max of each plot. Please, anyone can help me. Thanks in advance.
The min max is as follow:
Delay = (53.46 (min 0, max60) , 36.22 (min 12,max 70), 83 (min 21,max 54), 17 (min 12,max 70)) Latency = (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()
解决方案- In order to plot in the correct location on a bar plot, the patch data for each bar must be extracted.
- An
ndarray
is returned with onematplotlib.axes.Axes
per column.- In the case of this figure,
ax.patches
contains 8matplotlib.patches.Rectangle
objects, one for each segment of each bar.- By using the associated methods for this object, the
height
,width
, andx
locations can be extracted, and used to draw a line withplt.vlines
.
- By using the associated methods for this object, the
- In the case of this figure,
- The
height
of the bar is used to extract the correctmin
andmax
value fromdict
,z
.- Unfortunately, the patch data does not contain the bar label (e.g.
Delay
&Latency
).
- Unfortunately, the patch data does not contain the bar label (e.g.
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
- If there are non-unique values in the two
dicts
, so they can't be combined, we can select the correctdict
based on the bar plot order. - All the bars for a single label are plotted first.
- In this case, index 0-3 are the
Dalay
bars, and 4-7 are theLatency
bars
- In this case, index 0-3 are the
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')
这篇关于如何将误差线添加到分组的柱状图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 如果两个
查看全文