3D Plotting: An Introduction to Creating Surfaces and Meshes in Matlab - Prof. David Smith, Study notes of Computer Science

An introduction to 3d plotting in matlab, covering the basics of constructing plots, choosing different plot types, and the anatomy of a 3d plot. It also explores various topics such as plotting in 3d, using the meshgrid() function, 3d mesh plots, surface plots, shading and light sources, contour plots, and using color to add a fourth dimension. The document emphasizes the power of matlab for 3d plotting and its applications in engineering.

Typology: Study notes

Pre 2010

Uploaded on 08/04/2009

koofers-user-cng
koofers-user-cng 🇺🇸

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
CS1371
Introduction to Computing
for Engineers
3D plotting
2
Three Dimensional Plotting
Learning Objectives
Understand the
anatomy of a 3D plot
Basics of
constructing plots in
3D
How to choose
different plot types
for best effects
Topics
Plotting in 3D: major
differences
The meshgrid() function
to create plaids
3D mesh plots
3D surface plots
shading and light sources
Contour plots
Use of color to add a 4th
dimension
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download 3D Plotting: An Introduction to Creating Surfaces and Meshes in Matlab - Prof. David Smith and more Study notes Computer Science in PDF only on Docsity!

CS

Introduction to Computing

for Engineers

3D plotting

Three Dimensional Plotting

Learning Objectives

– Understand the

anatomy of a 3D plot

– Basics of

constructing plots in

3D

– How to choose

different plot types

for best effects

Topics

– Plotting in 3D: major

differences

– The meshgrid() function

to create plaids

– 3D mesh plots

– 3D surface plots

– shading and light sources

– Contour plots

– Use of color to add a 4th

dimension

Background

• Plotting in 3D is where Matlab’s power really becomes

apparent!

• Matlab defines a number of different kinds of 3D plots

but you will probably find 3 or 4 to be the most useful:

– x,y,z 3D line plot (plot3( ))

– mesh plot

– surface plot

– contour plot

– combo surface/mesh with contour

• The surface plotting can be applied to create realistic 3D

objects by defining and plotting their exterior surfaces!

• We can only touch lightly on this vast area of Matlab…

Anatomy of a 3D Plot

• There are MANY options for plotting in 3D but we will

consider only the basics:

– Plotting a curve in 3D: plot3(x,y,z)

– Plotting a surface, z=f(x,y), in 3D

– There are also several other plotting topics that we

will not cover or discuss in class (but you might find

interesting to explore on your own):

• ribbon plots

• quiver plots (showing vectors)

• volume plots

• advanced colormap use

3D Line Plot (2)

• These examples may be a little more useful…

clf turns=40.pi; theta=linspace(0,turns,4000); x=cos(theta).(turns-theta)./turns; y=sin(theta).*(turns-theta)./turns; z=theta./turns; plot3(x,y,z) grid on text(0.5,0.5,0.75,'Here is a piece of text!');

Here is a piece of text!

theta=0:0.1:10.*pi; plot3(sin(theta),cos(theta),theta) grid on

0

1

  • 1

0

1

0

10

20

30

40

3D Surface Plots

• It is often desirable to plot functions of the form: z=f(x,y)

– for each (x,y), we can compute a value for z

– this defines a surface in 3D space

• If we can define (x,y) at regular intervals, Matlab provides

powerful ways to plot the resulting function as a grid or

surface in 3D.

• There are tools in Matlab to handle the situation with

(x,y) defined at irregular intervals, but we won't consider

them in this course.

• We will look into how colors can be employed to add the

equivalent of a 4th dimension…

Defining the (x,y) Values

• We need a way to create the range of (x,y) values needed to

compute f(x,y)

0

2

0

2

0

5

10

y-axis x-axis

z-axis

x =-3 -2 -1 0 1 2 3 y =-3 -2 -1 0 1 2 3 z=f(x,y)

This won't work because we need all

values of y for each value of x and vice

versa to evaluate function over entire

region shown

xx =-3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3

yy =-3 -3 -3 -3 -3 -3 - -2 -2 -2 -2 -2 -2 - -1 -1 -1 -1 -1 -1 - 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 z=f(xx,yy)

This works : corresponding elements of xx & yy contain the

x and y coordinates to be used for f(x,y) at that point

NOTE :

xx varies along ROWS while yy varies

along COLUMNS

The meshgrid() Function

• Matlab provides a function to compute these arrays:

x=-3.5:3.5; y=-2.5:2.5; [xx,yy]=meshgrid(x,y) xx = -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3. -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3. -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3. -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3. -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3. -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3. yy = -2.5 -2.5 -2.5 -2.5 -2.5 -2.5 -2.5 -2. -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1. -0.5 -0.5 -0.5 -0.5 -0.5 -0.5 -0.5 -0. 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0. 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1. 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.

NOTE #1 :

xx varies along ROWS while yy varies

along COLUMNS

NOTE #2 :

For any (i, j), the value in xx is the x

coordinate, while the value in yy is the

y coordinate:

xx(3,1) = -3.

yy(3,1) = -0.

So (x,y) = (-3.5, -0.5)

NOTE #4 :

We can use array math to efficiently

compute the z values when z=f(x,y)

NOTE #3 :

Matlab calls these arrays "plaids"

Key Concepts for z=f(x,y) Plots

• 3D plotting introduces several key concepts:

– Meshes versus surfaces

– Hidden line removal

– Pedestals and contours

– Color maps and pseudo-coloring

– Viewpoints and camera control (advanced!)

– Shading and lighting (advanced)

• The following figures demonstrate these concepts, but

you are encouraged to check this out for yourself (using

Matlab’s graphic brings out the real fun in using this

powerful software!).

[X,Y,Z] = sphere(12);

subplot(1,2,1);

mesh(X,Y,Z), title('Figure 26.5a: Opaque');

hidden on;

axis square off;

subplot(1,2,2);

mesh(X,Y,Z),title('Figure 26.5b: Transparent');

hidden off;

axis square off;

Fig 26.5a: Opaque Fig 26.5b: Transparent

Exploring Hidden Line Removal

• This uses an interesting built-in function sphere( )

• Hidden lines:

– ON : shows white inside mesh

– OFF : shows transparent mesh

Let's Explore the mesh( ) Function

• We'll use peaks( ) to create a z=f(x,y) function that is

interesting and shows off the 3D plotting

• Note : you should check help peaks and help mesh and also

the textbook for further details on these functions

y-axis x-axis

z-axis

>> [x,y,z]=peaks(30);

>> mesh(x,y,z)

>> axis tight

>> xlabel('x-axis')

>> ylabel('y-axis')

>> zlabel('z-axis')

Suggestion :

Try using hidden off and

hidden on to see what happens.

Exploring meshc Plots

• meshc( ) adds a contour plot directly below the mesh

– helps visualize the contours

– can locate the peaks and dips

y-axis x-axis

z-axis

>> [x,y,z]=peaks(30);

>> meshc(x,y,z)

>> axis tight

>> xlabel('x-axis')

>> ylabel('y-axis')

>> zlabel('z-axis')

Hint: :

If you just finished the previous

example, you need only type in the

new meshc( ) command.

Let's Explore the surf( ) Function

• So far we have only been able to plot meshes to

represent the surface

– can hide hidden lines to clarify the surface shape

– still appears as a wireframe-like shape

• Matlab provides a function that will fill in the mesh with

facets (surfaces with 3 or 4 corners but not necessarily

plane surfaces)

– we'll see that these can produce very realistic

appearing surfaces in 3D

– can control appearance of mesh

– can change color mapping to reveal other information

– can add lighting

Exploring surf Plots (shading faceted)

• The basic function uses the default shading faceted

and this shows the mesh:

y-axis x-axis

z-axis

>> [x,y,z]=peaks(30);

>> surf(x,y,z)

>> axis tight

>> xlabel('x-axis')

>> ylabel('y-axis')

>> zlabel('z-axis')

Exploring surf Plots (shading flat)

• shading flat will eliminate the mesh and leave the

facets colored with a constant color value

y-axis x-axis

z-axis

>> [x,y,z]=peaks(30);

>> surf(x,y,z)

>> shading flat

>> axis tight

>> xlabel('x-axis')

>> ylabel('y-axis')

>> zlabel('z-axis')

Exploring surfc Plots (shading interp)

• surfc acts much like meshc with a contour plot drawn below the

surface

• shading interp interpolates color over each facet

>> [x,y,z]=peaks(30);

>> surfc(x,y,z)

>> shading interp

>> axis tight

>> xlabel('x-axis')

>> ylabel('y-axis')

>> zlabel('z-axis')

NOTE:

shading interp can take time to

execute and the figure may cause

plotting problems

Using Color as a 4

th

Dimension

• Matlab associates a “colormap” with each figure window

– this is a 3 column array in which columns #1-3 control the Red,

Blue & Green colors (0-1 range)

– each row defines a specific color

– rows are limited by the color display capabilities of the

computer

– these, along with a few fixed colors, are the colors Matlab will

use in the figure window (each figure window has a separate

colormap)

– Matlab predefines a number of useful colormaps

• JET, HSV, GRAY, HOT, COOL, BONE, COPPER, PINK, FLAG,

PRISM

• see help graph3d for more information and other colormaps

• use colormap hsv or colormap(‘hsv’) to change

• colormap default restores the colormap to default values

– use the colorbar command to display the color bar by itself or

alongside a plot (see help)

Using Color as a 4

th

Dimension (2)

• Matlab uses “pseudo-color” to change the color in a mesh or surf

plot

– colors can be based on the z values (default)

– you can specify the color variable in mesh() and surf()

• Use caxis([cmin cmax]) to define the max and min values that

are mapped to the colormap

>> caxis([-5 5]);

>> colorbar

>> caxis([-50 50]);

>> colorbar

Using Color as a 4

th

Dimension (3)

• mesh() and surf() can accept a "color" argument that

defines the color used over the plaid.

y-axis

>> caxis('auto')

>> surf(x,y,z,y)

>> axis tight

>> ylabel ('y-axis')

Here we have used the y

values as the color variable.

Curvature

This shows curvature of the

surface as the color variable.

>> C=del2(z); % compute Laplacian

>> surf(x,y,z,C)

>> axis tight

>> colorbar

Contour Plots

• Matlab provides several functions to draw contours

– contour(): draws simple contour map with N intervals

– contourf(): draws a contour with filled contours

– contour3(): draws a contour map in 3D

0

2

4

6

-3 -2 -1 0 1 2 3

0

1

2

3

x-axis

y-axis

» [x,y,z]=peaks(30);

» contour(x,y,z,10)

» colorbar

» xlabel('x -axis')

» ylabel('y -axis')

0

2

4

6

-3 -2 -1 0 1 2 3

0

1

2

3

x-axis

y-axis

» [x,y,z]=peaks(30);

» contourf(x,y,z,10)

» colorbar

» xlabel('x -axis')

» ylabel('y -axis')

NOTE:

See textbook for

other options.

NOTE:

See textbook for

other options.

Questions?