How does subplot work and what is the difference between subplot(121) and subplot(1,2,1) in MATLAB? -
How does subplot work and what is the difference between subplot(121) and subplot(1,2,1) in MATLAB? -
i bit unclear how subplot works. specifically, difference between subplot(121) , subplot(1,2,1) in matlab? have tried search subplot document, can't understand means.
long story short, there no difference. how subplot works following:
subplot(m,n,p); %//or subplot(mnp); you have 3 numbers used within subplot. subplot places multiple figures within same window. can place plots within m x n grid, m contains number of rows , n contains number of columns in figure. p determines where want place plot within grid. number p increases 1 m x n, , plots placed left right, , top bottom.
in case, when subplot(1,2,1); or subplot(121);, have one row , two columns worth of figures. lastly number, p=1 means wish place plot in left column. when subplot(1,2,2); or subplot(122);, when p=2 , wish place plot in right column.
how utilize subplot in next fashion:
m , n). spawn blank figure window for each plot want create... call subplot , take right location(s) of want plot appear. write necessary code create plot plot occupying single window. plot data repeat step #3 each plot have until run out of subplot slots. here illustrative example. let's create window has two rows , three columns worth of figures within same window. such:
figure; rng(10); %// set seed reproducibility subplot(2,3,1); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('first plot'); subplot(2,3,2); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('second plot'); subplot(2,3,3); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('third plot'); subplot(2,3,4); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('fourth plot'); subplot(2,3,5); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('fifth plot'); subplot(2,3,6); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('sixth plot'); what above code generate random sets of points 100 x 1 each pairs of x , y , plot them in multiple locations within overall window. notice lastly parameter of subplot increases linearly, while first 2 parameters stay same. must create sure know how many figures want within overall window before start plotting. figure code above describes looks following:
you can specify a vector of points p. however, should way, must phone call subplot way: subplot(m,n,p);. if p single number, either subplot(m,n,p); or subplot(mnp) work.
if specify p vector, 1 plot create will occupy multiple spaces / slots within same figure window. example, if did: subplot(2,3,1:3);, take 1 plot , occupy entire first row of figure. can issue more plots in slots 4, 5 , 6. in other words:
figure; rng(10); %// set seed reproducibility subplot(2,3,1:3); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('first plot'); subplot(2,3,4:5); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('second plot'); subplot(2,3,6); x = rand(100,1); y = rand(100,1); plot(x,y,'b.'); title('third plot'); the figure looks like:
as can see, have occupied first row using subplot(2,3,1:3); first plot. sec plot occupies slots p=4,p=5 using subplot(2,3,4:5);. occupies sec row, , first , sec columns. our lastly plot occupies sec row, 3rd column using subplot(2,3,6);. remember, slots go left right , top bottom, , p can not single number vector well. if wanted occupy first 2 rows , 2 columns, subplot(2,3,[1 2 4 5]); now, if wanted occupy entire right column, can subplot(2,3,[3 6]);, or if want top location in right column, can subplot(2,3,3); or subplot(233);, if want tackle lastly location in lastly column , @ bottom right, can subplot(2,3,6); or subplot(236);
one final thing want create sure remember need create sure phone call subplot before decide show plot. 1 time you're finished, switch on next slot , maintain working.
hope helps! luck!
matlab
Comments
Post a Comment