Faster alternative to subplot
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, I have a program with four different plots that I am updating multiple times each second. In order to select the plot before I modify it, I am using the subplot function, but unfortunately this function is slowing down my program significantly. As a result, I was wondering if there was an alternative, faster version of subplot if you don't actually need to create a subplot, but simply select a preexisting subplot in order to write to it.
Thank you for any help that you can provide.
3 Commenti
Jan
il 28 Feb 2023
@ED: Looking at the code of subplot.m it is surprising, that a direct call to axes should be slower. The overhead of subplot is large and finally axes is called here also.
Instead of making a specific axes object the current one, it is faster to use it as parent for newly created objects. It is even faster to update existing objects than to create new ones. See Bruno's suggestion, which does exactly this.
Repeated calls of axes get slower, if new axes objects are created instead of activating existing axes. Maybe it gets clear, what you observe, if you post a minimal working example.
Risposte (2)
Walter Roberson
il 9 Mar 2016
Record the handles created by subplot(), and then axes() the proper one.
ax11 = subplot(2,2,1);
ax12 = subplot(2,2,2);
ax21 = subplot(2,2,3);
ax22 = subplot(2,2,4);
for K = 1 : 10
axes(ax11)
....
axes(ax12)
...
etc
end
Probably you can do even better than this by recording the handles of the graphics objects generated the first time, and after that updating their properties.
for K = 1 : 10
y11 = rand(1,10);
y12 = rand(1,10);
if K == 1
%first iteration, create the graphics
h11 = plot(ax11, t, y11);
title(ax11, 'first plot');
h12 = plot(ax12, t, y12);
title(ax12, 'second plot);
else
%for everything other than the first, just update the existing graphs
set(h11, 'ydata', y11);
set(h12, 'ydata', y12);
end
end
0 Commenti
Steven Lord
il 9 Mar 2016
Don't keep recreating the axes or forcing SUBPLOT to check if it should return the handle of an existing axes. Store the handles of the axes in a variable and use AXES to make the appropriate axes current (or explicitly pass in the handle to the appropriate axes into the plotting function; most accept axes as inputs in this way.)
0 Commenti
Vedere anche
Categorie
Scopri di più su Subplots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!