Barplot with errorbar for matrix of variances

10 次查看(过去 30 天)
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
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