× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How does subplot work and what is the difference between subplot(121) and subplot(1,2,1) in MATLAB?

Ray Phan
Jan 30, 2015
<p>Long story short, there is no difference. How <code>subplot</code> works is the following:</p> <pre><code>subplot(m,n,p); %//or subplot(mnp); </code></pre> <p>You have three numbers that are used within <code>subplot</code>. <code>subplot</code> places multiple <strong>figures</strong> within the same window. You can place plots within a <code>m x n</code> grid, where <code>m</code> contains the number of rows and <code>n</code> contains the number of columns in your figure. <code>p</code> determines <strong>where</strong> you want to place your plot within the grid. The number <code>p</code> increases from <code>1</code> up to <code>m x n</code>, and the plots are placed from left to right, and top to bottom. </p> <p>In this case, when you do <code>subplot(1,2,1);</code> or <code>subplot(121);</code>, you would like to have <strong>one</strong> row and <strong>two</strong> columns worth of figures. The last number, <code>p=1</code> means that you wish to place the plot in the <strong>left</strong> most column. When you do <code>subplot(1,2,2);</code> or <code>subplot(122);</code>, this is when <code>p=2</code> and you wish to place the plot in the <strong>right</strong> most column. </p> <p>How you use <code>subplot</code> is in the following fashion:</p> <ol> <li>Determine <strong>how many</strong> rows and columns of plots you want within this window first (i.e. <code>m</code> and <code>n</code>).</li> <li>Spawn a blank <code>figure</code> window</li> <li>For each plot you want to create... <ul> <li>Call <code>subplot</code> and choose the right location(s) of where you want the plot to appear.</li> <li>Write the necessary code to create your plot like you would for just a plot occupying a <strong>single window</strong>.</li> <li>Plot your data</li> </ul></li> <li>Repeat Step #3 for each plot we have until we run out of <code>subplot</code> slots.</li> </ol> <p>Here is an illustrative example. Let's create a window that has <strong>two</strong> rows and <strong>three</strong> columns worth of figures within the same window. As such:</p> <pre><code>figure; rng(10); %// Set seed for 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'); </code></pre> <p>What the above code does is that we generate random sets of points that are <code>100 x 1</code> each for pairs of <code>x</code> and <code>y</code> and we plot them in multiple locations within the overall window. Notice that the last parameter of <code>subplot</code> increases linearly, while the first two parameters <strong>stay the same</strong>. You <strong>must</strong> make sure that you <strong>know</strong> how many figures you want within the overall window <strong>before</strong> you start plotting. The figure that the code above describes looks like the following:</p> <p><img src="http://i.stack.imgur.com/rEn7o.png" alt="enter image description here"></p> <p>You can also specify <strong>a vector</strong> of points for <code>p</code>. However, should you do it this way, you <strong>must</strong> call <code>subplot</code> this way: <code>subplot(m,n,p);</code>. If <code>p</code> is a <em>single number</em>, then either <code>subplot(m,n,p);</code> or <code>subplot(mnp)</code> will work. </p> <p>If you specify <code>p</code> to be a <em>vector</em>, what this will do is that one plot you make <strong>will occupy multiple</strong> spaces / slots within the same figure window. As an example, if you did: <code>subplot(2,3,1:3);</code>, this will take one plot and <strong>occupy the entire first row of your figure</strong>. You can then issue more plots in slots 4, 5 and 6. In other words:</p> <pre><code>figure; rng(10); %// Set seed for 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'); </code></pre> <p>The figure looks like:</p> <p><img src="http://i.stack.imgur.com/uAQy3.png" alt="enter image description here"></p> <p>As you can see, we have occupied the <strong>first row</strong> using <code>subplot(2,3,1:3);</code> with the first plot. The second plot occupies slots <code>p=4,p=5</code> using <code>subplot(2,3,4:5);</code>. This occupies the second row, and first and second columns. Finally our last plot occupies the second row, third column using <code>subplot(2,3,6);</code>. Remember, the slots go from left to right and top to bottom, and <code>p</code> can not only be a single number but a vector as well. If you wanted to occupy the <strong>first two rows and two columns</strong>, you would do <code>subplot(2,3,[1 2 4 5]);</code> Now, if you wanted to occupy the entire right most column, you can do <code>subplot(2,3,[3 6]);</code>, or if you just want the top most location in the right most column, you can do <code>subplot(2,3,3);</code> or <code>subplot(233);</code>, then if you want to tackle the last location in the last column and at the bottom right, you can do <code>subplot(2,3,6);</code> or <code>subplot(236);</code></p> <p>One final thing that I want to make sure that you remember is that you need to make sure you call <code>subplot</code> <strong>before</strong> you decide to show your plot. Once you're finished, switch over to the next slot and keep working.</p> <p>Hope this helps! Good luck!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/24337813/How%20does%20subplot%20work%20and%20what%20is%20the%20difference%20between%20subplot(121)%20and%20subplot(1,2,1)%20in%20MATLAB?/24337903">Stack Overflow</a>.</p>
comments powered by Disqus