Error bars added to column plot instead of regular line plot, and stacked 2x2

8 views (last 30 days)
I would like to create a 2x2 plot with error bars, where each mean is represented by a column rather than by a marker (data point), something like this:
The errorbar function in Matlab seems to not have the option to make columns instead of simple markers, and also no option to stack the four entities as 2x2, instead giving me something that looks like this:

Accepted Answer

dpb
dpb on 8 Jun 2016
Edited: dpb on 11 Jun 2016
Would be worth a search on File Exchange to see if somebody has submitted a function, but--
It's somewhat of a pit(proverbial)a(ppendage) to do, but is achievable with bar and errorbar
A trivial sample --
>> y=randi(6,[2,2]); % trivial dataset
y =
1 2
4 5
>> hB=bar(y); % make a bar plot of y data
>> ylim([0 6]) % make y axes look better
>> hold on % get ready to add errorbar on top of
>> barX=get(cell2mat(get(hB,'children')),'xdata'); % bar x-data (patches x locations)
>> barXpos=cell2mat(cellfun(@mean,barX,'uniform',0)).'; % return x position of midpoint of bars
>> arrayfun(@errorbar,barXpos,y,zeros(size(y)),0.1*y) % add the error bar at top+10% range
>>
The above gives the following image--
Fix up the colors as desired and perhaps(?) a heavier line width, but shows the way to get the hard piece of data--where's the center of each bar drawn by bar? that's not a retrievable property; the above computes it as the average of the face positions.
Using arrayfun to invoke errorbar saves the drawing of the line between the points if used the array; that's also solvable by using linestyle but this seems pretty easy workaround to simply pass each element one-at-a-time. Do need an array of associated colors to match, of course...
Hope that helps...seems more trouble than should be, granted...

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!