Barplot with errorbar for matrix of variances

3 Ansichten (letzte 30 Tage)
MiauMiau
MiauMiau am 10 Sep. 2015
Hi
I have the following 5x6 matrix
A = [1 16 12 7 9 5; 4 18 7 8 9 10; 7 8 10 9 14 17; 13 15 9 8 4 12; 8 7 23 9 12 11]
Each column of the matrix represents a different city, and each row a different year. With bar(A) I easily can plot a barplot with each of the 5 years grouped together, and each year containing one bar for each city.
Now I also have the 5x6 matrix varia, which contains the variances for each entry in the matrix A. So for instance, varia(1,1) is the variance of the data collected for city1 in the year1, and should be used in the bar for city1 and year1 as an error bar - how can that be done?
I tried:
bar(A)
errorbar(A,varia,'.')
Error:
Error using errorbar (line 74) X, Y and error bars must all be the same length
Error in exampleScript (line 31) errorbar(A,varia)

Akzeptierte Antwort

the cyclist
the cyclist am 10 Sep. 2015
Bearbeitet: the cyclist am 10 Sep. 2015

It is a bit painful to add error bars to a grouped bar graph. Here is a small example that you may be able to adapt to your purpose:

mean_velocity = [5 6 7; 8 9 10]; % mean velocity
std_velocity = randn(2,3);  % standard deviation of velocity
figure
hold on
hb = bar(1:3,mean_velocity');
% For each set of bars, find the centers of the bars, and write error bars
pause(0.1); %pause allows the figure to be created
for ib = 1:numel(hb)
    %XData property is the tick labels/group centers; XOffset is the offset
    %of each distinct group
    xData = hb(ib).XData+hb(ib).XOffset;
    errorbar(xData,mean_velocity(ib,:),std_velocity(ib,:),'k.')
end

I got that code from this question I asked in the past.

Here is the result:

  2 Kommentare
MiauMiau
MiauMiau am 10 Sep. 2015
wow..why is there no standardprocedure for this in matlab? and thanks!
Basílio Gonçalves
Basílio Gonçalves am 16 Aug. 2018
Bearbeitet: Basílio Gonçalves am 16 Aug. 2018
Thanks a lot mate, this was super helpful!
if anyone tries to use this code, make sure the "velocity" data is organized horizontally (with each y value a different column)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by