Code covered by the BSD License  

Highlights from
3D Surface Plot For Data Visualization

image thumbnail

3D Surface Plot For Data Visualization

by

 

14 May 2013 (Updated )

Easy to use function for 3d surface plotting of sample data.

vecsurf_demo
´╗┐<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>


      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
      --><title>vecsurf_demo</title>
   <meta name="generator" content="MATLAB 7.12"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2013-06-03"><meta name="DC.source" content="vecsurf_testmain.m"><style type="text/css">

body {
  background-color: white;
  margin:10px;
}

h1 {
  color: #990000; 
  font-size: x-large;
}

h2 {
  color: #990000;
  font-size: medium;
}

/* Make the text shrink to fit narrow windows, but not stretch too far in 
wide windows. */ 
p,h1,h2,div.content div {
  max-width: 600px;
  /* Hack for IE6 */
  width: auto !important; width: 600px;
}

pre.codeinput {
  background: #EEEEEE;
  padding: 10px;
}
@media print {
  pre.codeinput {word-wrap:break-word; width:100%;}
} 

span.keyword {color: #0000FF}
span.comment {color: #228B22}
span.string {color: #A020F0}
span.untermstring {color: #B20000}
span.syscmd {color: #B28C00}

pre.codeoutput {
  color: #666666;
  padding: 10px;
}

pre.error {
  color: red;
}

p.footer {
  text-align: right;
  font-size: xx-small;
  font-weight: lighter;
  font-style: italic;
  color: gray;
}

  </style></head><body><div class="content"><h2>Contents</h2><div><ul><li><a href="#1">Basic Function Purpose - Organizing Vector Data In A 3D Surface Form</a></li><li><a href="#2">Function Option - Comeplete Missing Data In The 3D Surface Using The Mosaic Picture Option</a></li><li><a href="#3">Function Option - 1)Adding Extra Grid Points Using Mosaic Grid Points Option</a></li><li><a href="#4">Function Option - Saving Time With Larg Calculations Using Representational Three Dimensional Surface Option</a></li><li><a href="#5">Function
Option - Zoom In On Specific Parts Of The 3D Surface Without Losing
Detail Using Representational Three Dimensional Surface Grid Value
Boundry</a></li></ul></div><h2>Basic Function Purpose - Organizing Vector Data In A 3D Surface Form<a name="1"></a></h2><pre class="codeinput">x = 1:50;
y = 1:50;
z = sin(x) + cos(y);

x = x';
y = y';
z = z';

figure
plot3(x,y,z,<span class="string">'.b'</span>);
grid <span class="string">on</span>
title(<span class="string">'Actual Data Points In 3D Space For Demonstration 1/2'</span>);

[x_3d,y_3d,z_3d] = vecsurf(x,y,z);

figure
surf(x_3d,y_3d,z_3d)
colormap(jet(256))
camlight <span class="string">right</span>
lighting <span class="string">phong</span>
title(sprintf([<span class="string">'Demonstration 1:\nBasic Function Purpose - '</span><span class="keyword">...</span>
              <span class="string">'Organizing Vector Data In A 3D Surface Form'</span>]));
pause;
</pre><img src="vecsurf_testmain3_files/vecsurf_testmain_01.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_02.png" alt="" hspace="5" vspace="5"> <h2>Function Option - Comeplete Missing Data In The 3D Surface Using The Mosaic Picture Option<a name="2"></a></h2><pre class="codeinput">x = 1:50;
y = 1:50;
z = sin(x) + cos(y);

x = x';
y = y';
z = z';

[x_3d,y_3d,z_3d] = vecsurf(x,y,z,<span class="string">'MosaicPicture'</span>);

figure
surf(x_3d,y_3d,z_3d)
colormap(jet(256))
camlight <span class="string">right</span>
lighting <span class="string">phong</span>
title(sprintf([<span class="string">'Demonstration 2:\nComeplete Missing'</span> <span class="keyword">...</span>
               <span class="string">'Data In The 3D\n'</span> <span class="keyword">...</span>
               <span class="string">'Surface Using The Mosaic Picture Option'</span>]));

clear

x =-8:0.5:8;
y =x';
[xi,yi] = meshgrid(x,y);
r = sqrt(xi.^2+yi.^2)+eps;
zi= sin(r)./r;

X(1,1:33) = xi(1:33,17);
Y(1,1:33) = yi(1:33,17);
Z(1,1:33) = zi(1:33,17);

X(1,34:66) = xi(17,1:33);
Y(1,34:66) = yi(17,1:33);
Z(1,34:66) = zi(17,1:33);

x = X';
y = Y';
z = Z';

figure
plot3(x,y,z,<span class="string">'.r'</span>);
title(<span class="string">'Actual Data Points In 3D Space For Demonstration 3'</span>);
grid <span class="string">on</span>

[x_3d,y_3d,z_3d] = vecsurf(x,y,z,<span class="string">'MosaicPicture'</span>);

figure
surf(x_3d,y_3d,z_3d);
colormap(hsv(256))
camlight <span class="string">right</span>
lighting <span class="string">phong</span>
title(sprintf([<span class="string">'Demonstration 3:\nComeplete'</span> <span class="keyword">...</span>
               <span class="string">'Missing Data In The 3D\n'</span> <span class="keyword">...</span>
               <span class="string">'Surface Using The Mosaic Picture Option'</span>]));
pause;
</pre><img src="vecsurf_testmain3_files/vecsurf_testmain_03.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_04.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_05.png" alt="" hspace="5" vspace="5"> <h2>Function Option - 1)Adding Extra Grid Points Using Mosaic Grid Points Option<a name="3"></a></h2><p>-  2)Adding A value Boundry To The Extra Grid Points -  Using Mosaic Grid Points Value Boundry</p><pre class="codeinput">clear

x = linspace(-1.5,1.5,50);
y(1,1:50) = 0;
z = exp(-(x.^2));

x = x';
y = y';
z = z';

figure
plot3(x,y,z,<span class="string">'.g'</span>);
grid <span class="string">on</span>
title(<span class="string">'Actual Data Points In 3D Space For Demonstration 4/5'</span>);
xlabel(<span class="string">'Dimension One'</span>);
ylabel(<span class="string">'Dimension Tow'</span>);

[x_3d,y_3d,z_3d] = vecsurf(x,y,z,<span class="string">'MosaicPicture'</span>,[ <span class="keyword">...</span>
                           <span class="string">'SecGridPoints'</span>],50);

figure
surf(x_3d,y_3d,z_3d);
xlabel(<span class="string">'Dimension One'</span>);
ylabel(<span class="string">'Dimension Tow'</span>);
colormap(hot(256));
camlight(-40,30)
lighting <span class="string">phong</span>;
shading <span class="string">interp</span>
title(sprintf([<span class="string">'Demonstration 4:\nAdding Extra Grid '</span> <span class="keyword">...</span>
               <span class="string">'Points Using Mosaic Grid Points Option.\n '</span> <span class="keyword">...</span>
               <span class="string">'Adding Extra Grid Points To The Second '</span> <span class="keyword">...</span>
               <span class="string">'Dimension Hence That A 3D Surface Has\n'</span> <span class="keyword">...</span>
               <span class="string">'To Have At Least Tow Grid Points For Each Of'</span> <span class="keyword">...</span>
               <span class="string">' The First And Second Dimensions.'</span>]));


 [x_3d,y_3d,z_3d] = vecsurf(x,y,z,<span class="string">'MosaicPicture'</span>,[
         <span class="string">'SecGridPoints'</span>],50,<span class="string">'SecBoun'</span>,[-0.5,0.5]);

figure
surf(x_3d,y_3d,z_3d);
xlabel(<span class="string">'Dimension One'</span>);
ylabel(<span class="string">'Dimension Tow'</span>);
colormap(winter);
camlight(-40,30)
shading <span class="string">interp</span>
title(sprintf([<span class="string">'Demonstration 5:\nAdding A value Boundry '</span> <span class="keyword">...</span>
               <span class="string">'To The Extra Grid Points\n'</span> <span class="keyword">...</span>
               <span class="string">' Using Mosaic Grid Points Value Boundry -\n'</span> <span class="keyword">...</span>
               <span class="string">'Value Boundry: [-0.5,0.5]'</span>]));

 pause;
</pre><img src="vecsurf_testmain3_files/vecsurf_testmain_06.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_07.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_08.png" alt="" hspace="5" vspace="5"> <h2>Function Option - Saving Time With Larg Calculations Using Representational Three Dimensional Surface Option<a name="4"></a></h2><p>- Using Progress Option To See Function Progress - Using Smooth Option To Create A Smooth Surface</p><pre class="codeinput">clear

x = linspace(-3,3,1000000);

f = exp(-(x.^2));
X = x(1,1:1000000);
Y(1,1:1000000) = 0;
X(1,1000001:2000000) = 0;
Y(1,1000001:2000000) = x(1,1:1000000);
Z(1,1:1000000) = f;
Z(1,1000001:2000000) = f;

X = X';
Y = Y';
Z = Z';

figure
plot3(X,Y,Z,<span class="string">'.m'</span>);
grid <span class="string">on</span>
title(<span class="string">'Actual Data Points In 3D Space For Demonstration 6/7'</span>);

[x_3d,y_3d,z_3d] = vecsurf(X,Y,Z,<span class="string">'Representation'</span>,[ <span class="keyword">...</span>
                           [60,60]],<span class="string">'Progress'</span>);

figure
surf(x_3d,y_3d,z_3d);
colormap <span class="string">cool</span>
title(sprintf([<span class="string">'Demonstration 6:\nSaving Time With Larg'</span> <span class="keyword">...</span>
               <span class="string">' Calculations Using Representational\n'</span> <span class="keyword">...</span>
               <span class="string">'Three Dimensional Surface Option. Using'</span> <span class="keyword">...</span>
               <span class="string">' Progress Option To See Function Progress.\n'</span> <span class="keyword">...</span><span class="comment">.</span>
               <span class="string">'For This Demonstration I Used 1000000'</span> <span class="keyword">...</span>
               <span class="string">' Sameples Of Data.'</span>]));
shading <span class="string">interp</span>
lighting <span class="string">phong</span>
light
material <span class="string">shiny</span>

[x_3d,y_3d,z_3d] = vecsurf(X,Y,Z,<span class="string">'Representation'</span>,[ <span class="keyword">...</span>
                 [60,60]],<span class="string">'Progress'</span>,<span class="string">'Smooth'</span>,[2,2,1]);

figure
surf(x_3d,y_3d,z_3d);
colormap <span class="string">cool</span>
title(sprintf([<span class="string">'Demonstration 7:\nWhen Representational'</span> <span class="keyword">...</span>
               <span class="string">' Three Dimensional Surface Option\n'</span> <span class="keyword">...</span>
               <span class="string">'Is Used, Smooth Option Can Be Used To'</span> <span class="keyword">...</span>
               <span class="string">' Smoothness The Surface.'</span>]));

shading <span class="string">interp</span>
lighting <span class="string">phong</span>
light
material <span class="string">shiny</span>
pause;
</pre><img src="vecsurf_testmain3_files/vecsurf_testmain_09.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_10.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_11.png" alt="" hspace="5" vspace="5"> <h2>Function
Option - Zoom In On Specific Parts Of The 3D Surface Without Losing
Detail Using Representational Three Dimensional Surface Grid Value
Boundry<a name="5"></a></h2><p>- Using Plot Option To Make The Function Plot The Result</p><pre class="codeinput">clear

n1 = 15;
n2 = 15;
theta = rand(n1,1)*pi/2;
r = rand(1,n2);

x = cos(theta)*r;
y = sin(theta)*r;
x=x(:);
y=y(:);

X = [[0 0 1 1]';x;x;1-x;1-x];
Y = [[0 1 0 1]';y;1-y;y;1-y];

Z = sin(4*X+5*Y).*cos(7*(X-Y))+exp(X+Y);

vecsurf(X,Y,Z,<span class="string">'Representation'</span>,[60,60],<span class="string">'Plot'</span>,[ <span class="keyword">...</span>
        1],<span class="string">'Smooth'</span>,[2,2,1]);

title(sprintf([<span class="string">'Demonstration 8:\nUsing Plot Option'</span> <span class="keyword">...</span>
               <span class="string">' To Make The Function Plot The Result.'</span>]));
colormap(hot(256))
camlight <span class="string">right</span>
lighting <span class="string">phong</span>

vecsurf(X,Y,Z,<span class="string">'Representation'</span>,[60,60],<span class="string">'Plot'</span>,1,[ <span class="keyword">...</span>
       <span class="string">'Smooth'</span>],[2,2,2],<span class="string">'RepOneBoun'</span>,[0.2,0.7],[ <span class="keyword">...</span>
       <span class="string">'RepSecBoun'</span>],[0.2,0.7]);

title(sprintf([<span class="string">'Demonstration 9:\n Zoom In On'</span> <span class="keyword">...</span>
               <span class="string">' Specific Parts Of The 3D Surface'</span> <span class="keyword">...</span>
               <span class="string">'Without Losing Detail\n'</span> <span class="keyword">...</span>
               <span class="string">'Using Representational Three Dimensional'</span> <span class="keyword">...</span>
               <span class="string">' Surface Grid Value Boundry.'</span>]));
colormap(hot(256))
camlight <span class="string">right</span>
lighting <span class="string">phong</span>
pause;
</pre><img src="vecsurf_testmain3_files/vecsurf_testmain_12.png" alt="" hspace="5" vspace="5"> <img src="vecsurf_testmain3_files/vecsurf_testmain_13.png" alt="" hspace="5" vspace="5"> <p class="footer"><br>
      Published with MATLAB® 7.12<br></p></div><!--
##### SOURCE BEGIN #####
%% Basic Function Purpose - Organizing Vector Data In A 3D Surface Form

x = 1:50;
y = 1:50;
z = sin(x) + cos(y);

x = x';
y = y';
z = z';

figure
plot3(x,y,z,'.b');
grid on
title('Actual Data Points In 3D Space For Demonstration 1/2');

[x_3d,y_3d,z_3d] = vecsurf(x,y,z);

figure
surf(x_3d,y_3d,z_3d)
colormap(jet(256))
camlight right
lighting phong
title(sprintf(['Demonstration 1:\nBasic Function Purpose - '...
              'Organizing Vector Data In A 3D Surface Form']));
pause;

%% Function Option - Comeplete Missing Data In The 3D Surface Using The Mosaic Picture Option

x = 1:50;
y = 1:50;
z = sin(x) + cos(y);

x = x';
y = y';
z = z';

[x_3d,y_3d,z_3d] = vecsurf(x,y,z,'MosaicPicture');

figure
surf(x_3d,y_3d,z_3d)
colormap(jet(256))
camlight right
lighting phong
title(sprintf(['Demonstration 2:\nComeplete Missing' ...
               'Data In The 3D\n' ...
               'Surface Using The Mosaic Picture Option']));

clear 

x =-8:0.5:8;
y =x';
[xi,yi] = meshgrid(x,y);
r = sqrt(xi.^2+yi.^2)+eps;
zi= sin(r)./r;

X(1,1:33) = xi(1:33,17);
Y(1,1:33) = yi(1:33,17);
Z(1,1:33) = zi(1:33,17);

X(1,34:66) = xi(17,1:33);
Y(1,34:66) = yi(17,1:33);
Z(1,34:66) = zi(17,1:33);

x = X';
y = Y';
z = Z';

figure
plot3(x,y,z,'.r');
title('Actual Data Points In 3D Space For Demonstration 3');
grid on

[x_3d,y_3d,z_3d] = vecsurf(x,y,z,'MosaicPicture');

figure
surf(x_3d,y_3d,z_3d);
colormap(hsv(256))
camlight right
lighting phong
title(sprintf(['Demonstration 3:\nComeplete' ...
               'Missing Data In The 3D\n' ...
               'Surface Using The Mosaic Picture Option']));
pause;
%% Function Option - 1)Adding Extra Grid Points Using Mosaic Grid Points Option
% -  2)Adding A value Boundry To The Extra Grid Points
% -  Using Mosaic Grid Points Value Boundry

clear

x = linspace(-1.5,1.5,50);
y(1,1:50) = 0;
z = exp(-(x.^2));

x = x';
y = y';
z = z';

figure
plot3(x,y,z,'.g');
grid on
title('Actual Data Points In 3D Space For Demonstration 4/5');
xlabel('Dimension One');
ylabel('Dimension Tow');

[x_3d,y_3d,z_3d] = vecsurf(x,y,z,'MosaicPicture',[ ...
                           'SecGridPoints'],50);

figure
surf(x_3d,y_3d,z_3d);
xlabel('Dimension One');
ylabel('Dimension Tow');
colormap(hot(256));
camlight(-40,30)
lighting phong;
shading interp
title(sprintf(['Demonstration 4:\nAdding Extra Grid ' ...
               'Points Using Mosaic Grid Points Option.\n ' ...
               'Adding Extra Grid Points To The Second ' ...
               'Dimension Hence That A 3D Surface Has\n' ...
               'To Have At Least Tow Grid Points For Each Of' ...
               ' The First And Second Dimensions.']));
           

 [x_3d,y_3d,z_3d] = vecsurf(x,y,z,'MosaicPicture',[
         'SecGridPoints'],50,'SecBoun',[-0.5,0.5]);

figure
surf(x_3d,y_3d,z_3d);
xlabel('Dimension One');
ylabel('Dimension Tow');
colormap(winter);
camlight(-40,30)
shading interp
title(sprintf(['Demonstration 5:\nAdding A value Boundry ' ...
               'To The Extra Grid Points\n' ...
               ' Using Mosaic Grid Points Value Boundry -\n' ...
               'Value Boundry: [-0.5,0.5]']));

 pause;
 %% Function Option - Saving Time With Larg Calculations Using Representational Three Dimensional Surface Option
 % - Using Progress Option To See Function Progress 
 % - Using Smooth Option To Create A Smooth Surface
 
clear

x = linspace(-3,3,1000000);

f = exp(-(x.^2));
X = x(1,1:1000000);
Y(1,1:1000000) = 0;
X(1,1000001:2000000) = 0;
Y(1,1000001:2000000) = x(1,1:1000000);
Z(1,1:1000000) = f;
Z(1,1000001:2000000) = f;

X = X';
Y = Y';
Z = Z';

figure
plot3(X,Y,Z,'.m');
grid on
title('Actual Data Points In 3D Space For Demonstration 6/7');

[x_3d,y_3d,z_3d] = vecsurf(X,Y,Z,'Representation',[ ...
                           [60,60]],'Progress');

figure
surf(x_3d,y_3d,z_3d);
colormap cool
title(sprintf(['Demonstration 6:\nSaving Time With Larg' ...
               ' Calculations Using Representational\n' ...
               'Three Dimensional Surface Option. Using' ... 
               ' Progress Option To See Function Progress.\n' ....
               'For This Demonstration I Used 1000000' ...
               ' Sameples Of Data.']));
shading interp
lighting phong
light
material shiny

[x_3d,y_3d,z_3d] = vecsurf(X,Y,Z,'Representation',[ ...
                 [60,60]],'Progress','Smooth',[2,2,1]);

figure
surf(x_3d,y_3d,z_3d);
colormap cool
title(sprintf(['Demonstration 7:\nWhen Representational' ...
               ' Three Dimensional Surface Option\n' ...
               'Is Used, Smooth Option Can Be Used To' ...
               ' Smoothness The Surface.']));

shading interp
lighting phong
light
material shiny
pause;
%% Function Option - Zoom In On Specific Parts Of The 3D Surface Without Losing Detail Using Representational Three Dimensional Surface Grid Value Boundry
% - Using Plot Option To Make The Function Plot The Result

clear

n1 = 15;
n2 = 15;
theta = rand(n1,1)*pi/2;
r = rand(1,n2);

x = cos(theta)*r;
y = sin(theta)*r;
x=x(:);
y=y(:);

X = [[0 0 1 1]';x;x;1-x;1-x];
Y = [[0 1 0 1]';y;1-y;y;1-y];

Z = sin(4*X+5*Y).*cos(7*(X-Y))+exp(X+Y);

vecsurf(X,Y,Z,'Representation',[60,60],'Plot',[ ...
        1],'Smooth',[2,2,1]);

title(sprintf(['Demonstration 8:\nUsing Plot Option' ...
               ' To Make The Function Plot The Result.']));
colormap(hot(256))
camlight right
lighting phong

vecsurf(X,Y,Z,'Representation',[60,60],'Plot',1,[ ...
       'Smooth'],[2,2,2],'RepOneBoun',[0.2,0.7],[ ...
       'RepSecBoun'],[0.2,0.7]);

title(sprintf(['Demonstration 9:\n Zoom In On' ...
               ' Specific Parts Of The 3D Surface' ...
               'Without Losing Detail\n' ...
               'Using Representational Three Dimensional' ...
               ' Surface Grid Value Boundry.']));
colormap(hot(256))
camlight right
lighting phong
pause;
##### SOURCE END #####
--></body></html>

Contact us