MATLAB Answers


barwitherr() Position of errorbars

Asked by Gero Knittel on 7 Jul 2016
Latest activity Answered by Martina Callaghan on 3 Jun 2018
Hi everybody,
This is what I want to do (higher quality image as attachment):
A grouped barplot with errorbars. There are two "groups of groups" which should have different colors. Since I did not find a way to change the colors of individual bars but only groups of bars, I put two separate plots with different x values into one figure using "hold on". However, as you can see from the graph, the second plot has a problem with the location of the errorbars. If I remove the "hold on" before the second plot and generate a new figure, the problem disappears, without changing anything else in the code.
hold on
second = barwitherr(errors(a+1:end,:),[a+1:size(errors,2)],data(a+1:end,:),'BarWidth',0.8);
It would be truly great if you could help me out here.
With best regards, Gero


Show 1 older comment
Thank you very much for the reply. I attached a higher quality image (to the original post).
The error bars at the second set are all plotted at one exact location. If I open a new figure instead of plotting the second plot into the first figure (without changing the x-values), the error bars are at the correct positions.
And you're right, I will contact the author.
And I will try to find out how to retrieve the x locations of the errorbars.
I'd suspect a handle isn't being retrieved correctly when there's two objects on a single axis--that the error bars are at the precise midpoint of the bar groups indicates that likely the function works to place them by retrieving the patch x-positions and adjusts the x-values given by those factors to place them in the middle of the individual bars. That they're all at the midpoint implies that adjustment is coming out as zero for the second case.
I posted a solution to adding error to bar graph here not terribly long ago in which I did figure out a way to get that position; I'll see if I can find that posting and provide a link.
Meanwhile, I'd use the debugger and step through the function and see what seems to go wrong in this may not be too hard to fix up when you see what is actually done.
I note there's some comments on the FEx submission about HG2 causing some issues; what Matlab release are you using; that might have a bearing. I'm still at r2012b so can't check out anything regarding that...
<error-bars-added-to-column-plot> is link to my previous submission; you're welcome to see if it'll handle the second case on two axes; I've no idea at the moment how confused it might get. For certain you'll have to retain identifiable handles to the bar plots for the "handle diving" done to retrieve the coordinates. Also, whether this works at all for HG2 is unknown and I've no way to test.

Sign in to comment.

1 Answer

Answer by Martina Callaghan on 3 Jun 2018

The graphics need to be updated immediately, e.g. via drawnow or putting in a pause. In this case the figure gets updated properly and the errorbars are put in the right place. I've updated the file in File Exchange.


Sign in to comment.