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 CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by