Why do plotting functions like bar or barh take a long time to execute?
조회 수: 7 (최근 30일)
이전 댓글 표시
I have a 1x600 vector of doubles, where each element represent the duration of a certain engineering process. I want to get a horizontal stacked bar graph which shows the duration of each process sequentially. For that I am using the following code
BHd --> 1x600 vector
BHd = [zeros(length(BHd),1), BHd];
figure(3)
barh(BHd', 'stacked');
However, I noticed that it takes a long time for barh function to run in this case (58.2 s). Are there any alternative ways of plotting the stacked bar graphs, that would take less time?
The plots should have the following format.
Btw, I am running MATLAB2017b on my computer
댓글 수: 3
채택된 답변
jonas
2018년 8월 9일
편집: jonas
2018년 8월 9일
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.
%%Method 1, SURF
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;
%%Method 2, BARH
subplot(1,2,2)
tic
x=[nan(length(x),1)'; x];
barh(x, 'stacked');
t2=toc;
%%Display performance
t2/t1
ans =
2.9408
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!