필터 지우기
필터 지우기

Barplot with errorbar for matrix of variances

조회 수: 14 (최근 30일)
MiauMiau
MiauMiau 2015년 9월 10일
편집: Basílio Gonçalves 2018년 8월 16일
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)

채택된 답변

the cyclist
the cyclist 2015년 9월 10일
편집: the cyclist 2015년 9월 10일

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
MiauMiau
MiauMiau 2015년 9월 10일
wow..why is there no standardprocedure for this in matlab? and thanks!
Basílio Gonçalves
Basílio Gonçalves 2018년 8월 16일
편집: Basílio Gonçalves 2018년 8월 16일
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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by