Faster alternative to subplot

10 ビュー (過去 30 日間)
Samuel Lehmann
Samuel Lehmann 2016 年 3 月 9 日
コメント済み: Jan 2023 年 2 月 28 日
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 件のコメント
Bruno Luong
Bruno Luong 2023 年 2 月 28 日
編集済み: Bruno Luong 2023 年 2 月 28 日
@ED If you update tye plot, take a look at animatedline
Jan
Jan 2023 年 2 月 28 日
@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.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 3 月 9 日
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

Steven Lord
Steven Lord 2016 年 3 月 9 日
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.)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by