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

Stacking multiple 2D plots into a single 3D plot in MATLAB

Ray Phan
Feb 02, 2015
<p>That's not so bad to do. You can use <a href="http://www.mathworks.com/help/matlab/ref/plot3.html" rel="nofollow"><code>plot3</code></a> to help you facilitate this kind of plotting. With <code>plot3</code>, what you need to do is make the <code>y</code> values for each of your plots <code>z</code> values instead, and if you want to separate the graphs, you need to vary the <code>y</code> values in this 3D plot. Let's do an example. Let's say I want to place 4 graphs on a single plot in that fashion. The graphs are:</p> <ul> <li><code>y = sin x</code></li> <li><code>y = cos x</code></li> <li><code>y = exp(-x)*sin(x)</code></li> <li><code>y = exp(-x)*cos(x)</code></li> </ul> <p>As such, you'll have a set of <code>x</code> values that are the same for each of the plots. You'll have a set of <code>y</code> values that are going to be different, and is dependent on the graph itself. You'll make these the <code>z</code> values, then for each of these graphs, you'll have different <code>y</code> values, but for each plot they will <strong>all</strong> be the same, as you'll want to use these to offset each of your graphs accordingly to separate them. As such:</p> <pre><code>%// Define the x values x = (0:0.001:10).'; xMat = repmat(x, 1, 4); %// For plot3 %// Define y values y = 0:0.001:0.003; yMat = repmat(y, numel(x), 1); %//For plot3 %// Define z values z1 = sin(x); z2 = cos(x); z3 = exp(-x).*sin(x); z4 = exp(-x).*cos(x); zMat = [z1 z2 z3 z4]; %// For plot3 plot3(xMat, yMat, zMat, 'b'); %// Make all traces blue grid; xlabel('x'); ylabel('y'); zlabel('z'); view(40,40); %// Adjust viewing angle so you can clearly see data </code></pre> <p>This is the figure I get:</p> <p><img src="http://i.stack.imgur.com/qCAtP.jpg" alt="enter image description here"></p> <hr> <p>The trick is to form the right matrices so that this goes into <code>plot3</code> correctly. How <code>plot3</code> works is that you can either place single vectors in for your <code>x,y,z</code> values, or you can use <strong>matrices</strong> <code>xMat,yMat,zMat</code> instead. Each column of each matrix of <code>xMat</code>, <code>yMat</code> and <code>zMat</code> denote a <strong>single</strong> trace to be placed in your plot. Therefore, each column of <code>xMat</code> for each signal goes into a single column. As such, I created <strong>one</strong> vector for my <code>x</code> values, and replicated them over for as many signals as we have. In this case, we have 4 signals. The <code>y</code> values will have to be played with differently. You need to figure out how much <strong>spacing</strong> is going to be allowed for in between each signal. I chose <code>0.001</code> for each signal. As such, I've created that matrix, which is stored in <code>yMat</code>, accordingly and each column represents either <code>0</code>, <code>0.001</code>, <code>0.002</code> or <code>0.003</code>. The <code>z</code> values are going to be your <code>y</code> values for each signal placed in different columns, which I created with <code>zMat</code>.</p> <p>You'll need to play around with this to get the right spacing and the right bounds of each axes, but this should get you started. Good luck!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/24987216/Stacking%20multiple%202D%20plots%20into%20a%20single%203D%20plot%20in%20MATLAB/24987375">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus