Place Matplotlib errorbars NOT in center of bar

老子叫甜甜 提交于 2021-01-27 19:00:50

问题


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:

example screenshot

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?


回答1:


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]


来源:https://stackoverflow.com/questions/12933259/place-matplotlib-errorbars-not-in-center-of-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!