I would guess that barh is slow because it creates 600 different patch objects, each with a different handle. Try this solution with a single surface object.
n=10;
figure;
subplot(1,2,1)
x=rand(1,n);
tic
X=cumsum(x);
X=[0 X;0 X];
Y=[1.1.*ones(1,n+1);0.9.*ones(1,n+1)];
Z=rand(1,n+1);
Z=[Z;Z];
surf(X,Y,Z)
view([0 90])
set(gca,'ylim',[-2 3])
t1=toc;
subplot(1,2,2)
tic
x=[nan(length(x),1)'; x];
barh(x, 'stacked');
t2=toc;
t2/t1
ans =
so surf takes 1/3 of the time in this case. If you increase the number of elements, then the difference will increase further. At 600 elements, they differ by a factor of 500 on my machine.
You can use a different colormap and a different method for calculating the Z-values, if you need more distinct color differences.
I'm guessing you cannot increase the performance further by using line objects, as I suggested in the comments. You would have to create 600 line objects, which would hurt the performance.
3 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414253-why-do-plotting-functions-like-bar-or-barh-take-a-long-time-to-execute#comment_598140
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414253-why-do-plotting-functions-like-bar-or-barh-take-a-long-time-to-execute#comment_598140
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414253-why-do-plotting-functions-like-bar-or-barh-take-a-long-time-to-execute#comment_598148
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414253-why-do-plotting-functions-like-bar-or-barh-take-a-long-time-to-execute#comment_598148
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414253-why-do-plotting-functions-like-bar-or-barh-take-a-long-time-to-execute#comment_598154
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414253-why-do-plotting-functions-like-bar-or-barh-take-a-long-time-to-execute#comment_598154
Sign in to comment.