# Tutorial :3D scatter plots in Matlab ### Question:

I'm fairly new to Matlab and would appreciate some help. I'm trying to graph a scatter plot of a function. Later on I'm going to fit other functions to this data and plot that on the same figure. But what I have so far plots the markers all in one plane, which isn't what I want. The function is 2D, the graph should be 3D. How do I get this?

Here's what I've been trying so far. There's some other code before this that generates different values for f(i,j) given different parameters, so when I implement the code I get a series of figures.

``for i=1:somenumber      for j=1:somenumber          f(i,j)=etc.      end  end    figure;  x=1:somenumber;  plot3(x,f,x,'rs');  hold on;  ``

### Solution:1

See my comment on why you likely don't want to do this, but the general way of plotting in 3D is

``x = 1:10;  y = 1:5;  [X Y] = meshgrid(x, y);  Z = X.^2 + 2 .* Y;      % in general, Z = f(X, Y)  plot3(X, Y, Z, '+')  ``

### Solution:2

Here is an avenue worth exploring:

``nSamples = nX * nY;  xValues  = zeros( nSamples, 1 );  yValues  = zeros( nSamples, 1 );  zValues  = zeros( nSamples, 1 );    iSample = 0;  for iX = 1:nX     for iY = 1:nY        iSample = iSample + 1;          xValues( iSample ) = iX;        yValues( iSample ) = iY;        zValues( iSample ) = someFunction( iX, iY );       end  end    figure;  plot3( xValues(:), yValues(:), zValues(:), 'r.' );  ``

This should make it easy to add noise to any or all of x, y or z to test your function fitting algorithm.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »