将 Matplotlib 误差条放置在条的中心 [英] Place Matplotlib errorbars NOT in center of bar

查看:65
本文介绍了将 Matplotlib 误差条放置在条的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Matplotlib中生成带有错误条的堆积条形图.不幸的是,其中某些层相对较小,并且数据各不相同,因此多个层的错误栏可能会重叠,从而使它们难以读取或无法读取.

示例:

有没有办法设置每个误差条的位置(即沿 x 轴移动它),以便重叠线显示在旁边而不是彼此之上?

解决方案

感谢tcaswell的评论,我发现了:诀窍是不要在条形图中包含误差条(通过 yerr kword),而是分别绘制条形图和误差条.通过这种方式,您可以指定误差条的 x 和 y 位置.

仅使用 yerr = numpy.vstack([[0] * len(std),std]).

这里合并到绘图函数中,我的代码如下所示:

for j in range(set_count):tmp_accum = numpy.zeros(len(props[0]))对于范围内的k(len(props)):ax1.bar(ind + j * b_width,props [k] [set_label == j],width = b_width,color = color_sets [j] [k],bottom = tmp_accum [set_label == j],label = labels [k])ax1.errorbar(ind + j*b_width +(1+k)*0.01, props[k][set_label==j]+tmp_accum[set_label==j],yerr= numpy.vstack([[0]*1,std_devs[k][j]]), ecolor="black",elinewidth =3)tmp_accum += props[k]

I'm generating a stacked bar graph in Matplotlib with errorbars. Unfortunately, some of the tiers are relatively small and the data diverse, so that the errorbars of several tiers can overlap, making them hard or impossible to read.

Example:

Is there a way to set the location for each errorbar (i.e. move it along the x-axis), so the overlapping lines are displayed next to instead of on top of each other?

解决方案

Thanks to tcaswell's comment, I figured it out: The trick is to not include the errorbars in the bar (via the yerr kword), but plot the bar and errorbar separately. This way you can specify the x and y location of the errorbar.

It also helps clearness to plot the errorbars upwards only using yerr= numpy.vstack([[0]*len(std), std]).

Incorporated into the plotting function from here my code looks like this:

for j in range(set_count):
    tmp_accum = numpy.zeros(len(props[0]))
    for k in range(len(props)):
        ax1.bar(ind + j*b_width, props[k][set_label==j], width=b_width, color=color_sets[j][k], bottom=tmp_accum[set_label==j],label=labels[k])
        ax1.errorbar(ind + j*b_width +(1+k)*0.01, props[k][set_label==j]+tmp_accum[set_label==j],yerr= numpy.vstack([[0]*1, std_devs[k][j]]), ecolor="black",elinewidth =3)
        tmp_accum += props[k]

这篇关于将 Matplotlib 误差条放置在条的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆