using subplot(m,n,p,ax) in a loop

11 Ansichten (letzte 30 Tage)
Payal Bal am 11 Jun. 2015
Kommentiert: Ingrid am 3 Feb. 2017
Hi, If I have this plot
x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')
And want to assign it to a subplot with the same axes, I will do
ax1 = gca;
subplot(2,1,1,ax1)
But now if I want a new plot at subplot(2,1,2) with the same axes, I can’t do
ax1 = gca;
subplot(2,1,2,ax1)
because it deletes the previous plot. How can I keep both plots?
Cheers, p.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ingrid am 11 Jun. 2015
you cannot define an axes handle in a subplot command because the subplot command generates an axes
You should just write
h1 = subplot(2,1,1)
plot(h1,x1,y1)
h2 = subplot(2,1,2)
plot(h2,x2,y2)
2 KommentareKeine anzeigenKeine ausblenden
Payal Bal am 3 Feb. 2017
Hi Ingrid,
Thanks for replying. I want to specify figure and axes properties outside of a loop and use subplot to plot the outputs from each run (with similar colours, axes limits etc.) of the loop in one plot window. If I specify figure and axes specifications within the loop, I get a new window for each plot.
Also I am using image() instead of plot so plot(h1,x1,x1) won't work.
Here's what I am trying to do (please ignore the colourmap matrix, it's this long because I had to customize it)
figure1 = figure('Colormap',...
[0.600000023841858 0.600000023841858 0.600000023841858;0.540000021457672 0.540000021457672 0.64000004529953;0.480000019073486 0.480000019073486 0.680000007152557;0.420000016689301 0.420000016689301 0.720000028610229;0.360000014305115 0.360000014305115 0.759999990463257;0.300000011920929 0.300000011920929 0.800000011920929;0.240000009536743 0.240000009536743 0.840000033378601;0.180000007152557 0.180000007152557 0.879999995231628;0.120000004768372 0.120000004768372 0.920000016689301;0.0600000023841858 0.0600000023841858 0.959999978542328;0 0 1;0 0.0909090936183929 1;0 0.181818187236786 1;0 0.272727280855179 1;0 0.363636374473572 1;0 0.454545468091965 1;0 0.545454561710358 1;0 0.636363625526428 1;0 0.727272748947144 1;0 0.818181812763214 1;0 0.909090936183929 1;0 1 1;0 0.954367220401764 0.909090936183929;0 0.908734381198883 0.818181812763214;0 0.863101601600647 0.727272748947144;0 0.817468822002411 0.636363625526428;0 0.77183598279953 0.545454561710358;0 0.726203203201294 0.454545468091965;0 0.680570423603058 0.363636374473572;0 0.634937584400177 0.272727280855179;0 0.589304804801941 0.181818187236786;0 0.543672025203705 0.0909090936183929;0 0.498039215803146 0;0.100000001490116 0.548235297203064 0;0.200000002980232 0.598431348800659 0;0.300000011920929 0.648627460002899 0;0.400000005960464 0.698823511600494 0;0.5 0.749019622802734 0;0.600000023841858 0.79921567440033 0;0.699999988079071 0.84941178560257 0;0.800000011920929 0.899607837200165 0;0.899999976158142 0.949803948402405 0;1 1 0;1 0.9454545378685 0;1 0.890909075737 0;1 0.836363613605499 0;1 0.781818211078644 0;1 0.727272748947144 0;1 0.672727286815643 0;1 0.618181824684143 0;1 0.563636362552643 0;1 0.509090900421143 0;1 0.454545468091965 0;1 0.400000005960464 0;0.959999978542328 0.360000014305115 0;0.920000016689301 0.319999992847443 0;0.879999995231628 0.280000001192093 0;0.840000033378601 0.240000009536743 0;0.800000011920929 0.200000002980232 0;0.759999990463257 0.159999996423721 0;0.720000028610229 0.120000004768372 0;0.680000007152557 0.0799999982118607 0;0.64000004529953 0.0399999991059303 0;0.600000023841858 0 0]);
axes1 = axes('Parent',figure1,'Layer','top','FontSize',18,'CLim',[0 6]);
Within loop:
% Subplot 1
figure(1)
subplot(4,3,i,axes1); % i is the counter of the loop for example
xlim (axes1,[0 1]);
ylim (axes1,[0 100]);
box(axes1,'off');
hold(axes1,'all')
h=image(x,y,z,'Parent',axes1,'CDataMapping','scaled');
colorbar;
% Subplot2
figure(1)
subplot(4,3,i+3,axes1);
xlim (axes1,[0 1]);
ylim (axes1,[0 100]);
box(axes1,'off');
hold(axes1,'all')
h=image(x,y,z,'Parent',axes1,'CDataMapping','scaled');
colorbar;
So basically, I want to plot an image the properties of which I want to specify beforehand, and each image is an output from one step of a for loop. I therefore want to make a plot window and send the output images to their respective subplot locations. I hope this makes sense.
Cheers, p.
Ingrid am 3 Feb. 2017
when you type
doc image
you can learn that what you want is to not generate a new plot (A high-level function that calls newplot to determine where to draw the graphics objects and sets the following axes properties) as you are currently doing, but that you require A low-level function that adds the image to the current axes without calling newplot. The low-level function argument list can contain only property name/property value pairs. So basically you have to change your call to image so that you are plotting to the currently selected axes
hFig = figure;
for i= 1:12
% Subplot 1
figure(1)
hAX(i) = subplot(4,3,i); % i is the counter of the loop for example
xlim (hAX(i),[0 1]);
ylim (hAX(i),[0 100]);
box(hAX(i),'off');
hold(hAX(i),'all')
image('PropertyName',PropertyValue,...) % fill in for your specific case
colorbar;
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Salaheddin Hosseinzadeh am 11 Jun. 2015
Just put figure outside the loop.
If you want to change each axis or maybe axes properties, use xlim ylim in your for loop you can only have
for i = 1:N
subplot(1,N);
plot(data);
xlim([-x,x]);
ylim([-y,y]);
end
something like this
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Payal Bal am 13 Jun. 2015
Thanks. Yes was doing it all wrong.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Subplots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by