Why do plotting functions like bar or barh take a long time to execute?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
theblueeyeswhitedragon
il 9 Ago 2018
Commentato: theblueeyeswhitedragon
il 9 Ago 2018
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193471/image.png)
Btw, I am running MATLAB2017b on my computer
3 Commenti
Risposta accettata
jonas
il 9 Ago 2018
Modificato: jonas
il 9 Ago 2018
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Graphics Object Programming in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!