copy subplots within same figure

I am copying subplots within a figure, to show the same data with an expaned time base.
As you can see from the example below, the copied subplots overlap somewhat, and the legends do not get copied. Is there a better way to do this?
t=(0:200)';
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),'-r',t,x(:,2),'-g');
xlabel('Time'); ylabel('XA'); title('A'); legend({'cos','sin'})
ax1(2,1)=subplot(323);
plot(t,x(:,3),'-b',t,x(:,4),'-c');
xlabel('Time'); ylabel('XB'); title('B'); legend({'cos','sin'})
ax1(3,1)=subplot(325);
plot(t,x(:,5),'-m',t,x(:,6),'-y');
xlabel('Time'); ylabel('XC'); title('C'); legend({'cos','sin'})
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3
axtemp=subplot(3,2,2*i);
ax1(i,2)=copyobj(ax1(i,1),fig1);
ax1(i,2).Position=axtemp.Position;
delete(axtemp)
xlim([0,20])
end
Thank you.

1 个评论

If I remove the horizontal axis labels ('Time') from the upper and middle plots on the left, the problem is less bad, but still, the copied plots are taller than the column 1 plots, which doesn't look good.

请先登录,再进行评论。

 采纳的回答

Hi William,
Use the subplot command as shown below to get the new axes aligned with the old. The legends need to be copyobj'd along with the axes.
t=(0:200)';
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),'-r',t,x(:,2),'-g');
xlabel('Time'); ylabel('XA'); title('A'); L(1,1) = legend({'cos','sin'});
ax1(2,1)=subplot(323);
plot(t,x(:,3),'-b',t,x(:,4),'-c');
xlabel('Time'); ylabel('XB'); title('B'); L(2,1) = legend({'cos','sin'});
ax1(3,1)=subplot(325);
plot(t,x(:,5),'-m',t,x(:,6),'-y');
xlabel('Time'); ylabel('XC'); title('C'); L(3,1) = legend({'cos','sin'});
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3
% axtemp=subplot(3,2,2*i);
htemp = copyobj([ax1(i,1) L(i)],fig1);
ax1(i,2) = htemp(1);
% Save handle to new legend if desired.
% Corrected based on W. Rose comment below.
% L(3,2) = htemp(2);
L(i,2) = htemp(2);
subplot(3,2,2*i,ax1(i,2))
% ax1(i,2).Position=axtemp.Position;
% delete(axtemp)
xlim([0,20])
end

3 个评论

Thank you. It works. I would have expected line
L(3,2) = htemp(2);
to be
L(i,2) = htemp(2);
but L(3,2)=... works. In fact, I can comment out this line completely (see below), and it still works. Perhaps the line has a hidden function of which I am unaware.
t=(0:200)';
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),'-r',t,x(:,2),'-g');
xlabel('Time'); ylabel('XA'); title('A'); L(1,1) = legend({'cosA','sinA'});
ax1(2,1)=subplot(323);
plot(t,x(:,3),'-b',t,x(:,4),'-c');
xlabel('Time'); ylabel('XB'); title('B'); L(2,1) = legend({'cosB','sinB'});
ax1(3,1)=subplot(325);
plot(t,x(:,5),'-m',t,x(:,6),'-y');
xlabel('Time'); ylabel('XC'); title('C'); L(3,1) = legend({'cosC','sinC'});
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3 % Paul's solution
htemp = copyobj([ax1(i,1) L(i)],fig1);
ax1(i,2) = htemp(1);
%L(3,2) = htemp(2);
subplot(3,2,2*i,ax1(i,2))
xlim([0,20])
end
OK
Paul
Paul 2024-4-20
编辑:Paul 2024-4-20
You're right, it should've been L(i,2). It doesn't serve any purpose other than to provide a handle to the new legends should such legend handles be needed. I'll edit the post and fix code.
I was not aware of ax.Legend shown by @Voss, which avoids the need to create any legend handles.
So I can combine the @Voss and @Paul solutions. Forr some reason, on my system at least, Voss's solution makes column 2 plots that are sightly less tall than the corresponding column 1 plots. The combination of both approaches is below, and I appreciate it. Thank you both.
t=(0:200)';
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),'-r',t,x(:,2),'-g');
xlabel('Time'); ylabel('XA'); title('A'); legend({'cosA','sinA'});
ax1(2,1)=subplot(323);
plot(t,x(:,3),'-b',t,x(:,4),'-c');
xlabel('Time'); ylabel('XB'); title('B'); legend({'cosB','sinB'});
ax1(3,1)=subplot(325);
plot(t,x(:,5),'-m',t,x(:,6),'-y');
xlabel('Time'); ylabel('XC'); title('C'); legend({'cosC','sinC'});
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3 % Voss's & Paul's solution
htemp = copyobj([ax1(i,1) ax1(i,1).Legend],fig1);
ax1(i,2) = htemp(1);
subplot(3,2,2*i,ax1(i,2))
xlim([0,20])
end

请先登录,再进行评论。

更多回答(1 个)

To copy the legends, copy each with its associated axes (vector of objects to copyobj).
To fix the overlapping, set the OuterPosition the same, rather than the Position.
t=(0:200)';
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),'-r',t,x(:,2),'-g');
xlabel('Time'); ylabel('XA'); title('A'); legend({'cos','sin'})
ax1(2,1)=subplot(323);
plot(t,x(:,3),'-b',t,x(:,4),'-c');
xlabel('Time'); ylabel('XB'); title('B'); legend({'cos','sin'})
ax1(3,1)=subplot(325);
plot(t,x(:,5),'-m',t,x(:,6),'-y');
xlabel('Time'); ylabel('XC'); title('C'); legend({'cos','sin'})
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3
axtemp=subplot(3,2,2*i);
temp=copyobj([ax1(i,1) ax1(i,1).Legend],fig1);
ax1(i,2) = temp(1);
ax1(i,2).OuterPosition=axtemp.OuterPosition;
delete(axtemp)
xlim([0,20])
end

2 个评论

@Voss, Thank you. I'm sorry I can only accept 1 answer.
You're welcome!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Graphics Object Properties 的更多信息

产品

版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by