plotting – 3-dimensional. 3d plots versus 2d plots 3-dimensional plots, in contrast to...

39
Plotting – 3- Dimensional

Upload: evan-fowler

Post on 28-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Plotting – 3-Dimensional

Page 2: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

3D Plots versus 2D Plots

3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis).

We plot both 2D and 3D charts on a flat surface (screen, paper, etc.). In 3D charts, the third dimension gives the visual impression of depth.

Page 3: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

One dimensional data

One dimensional data (a single vector) can be plotted either in 2D or 3D.

Page 4: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

One dimensional data

cont= {'Asia', 'Europe', 'Africa', 'N.America', 'S.America'};

pop= [3332; 696; 694; 437; 307];

ti= 'World population 1992';

pie(pop, cont);

title(ti);

figure;

pie3(pop, cont);

title(ti);

Page 5: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

One dimensional data

pop= [3332; 696; 694; 437; 307];

subplot(2,2,1); bar(pop);

subplot(2,2,2); bar3(pop);

subplot(2,2,3); barh(pop);

subplot(2,2,4); bar3h(pop);

Page 6: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Two dimensional data

Two dimensional data (a pair of vectors, typically in the form y=f(x)) can be plotted either in 2D or 3D.

Plotting multiple vector pairs is also possible, such as

y1=f(x), y2=g(x), y3=h(x)

Each pair is usually plotted with a different color for easy discriminitation.

Page 7: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Two dimensional data

x= linspace(0, 6*pi, 200);

y1= sin(x);

y2= cos(x);

y3= 0.5*sin(x).*sin(12*x);

plot(x,y1, x,y2, x,y3);

legend('sin(x)', 'cos(x)', 'sin(x)sin(12x)/2');

figure;

% 3D ribbon plot with 0.4 ribbon thickness

ribbon(x', [y1',y2',y3'], 0.4);

legend('sin(x)', 'cos(x)', 'sin(x)sin(12x)/2');

Page 8: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

3D graph viewpoint

The angle from which an observer sees a 3D plot can be adjusted using the VIEW function.

view(azimuth, elevation) Azimuth is the horizontal rotation, and elevation is

the vertical angle from the x-y plane (both in degrees).

Azimuth revolves about the z-axis, with positive values indicating counter-clockwise rotation of the viewpoint.

Positive values of elevation correspond to moving above the object; negative values move below.

Page 9: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

View command formats

Different ways to use the view command: Most used:

view(azimuth, elevation) Look at the plot from the angle of a point in

cartesian space:view([x, y, z])

Default 3D view (azimuth: -37.5°, elevation: 30°):view(3)

Convert to 2D view (look from the top):view(2)

Page 10: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

View command examples

view(100,15) view([5,5,5])

view(3) view(2)

Page 11: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Three dimensional data

We almost always need to use a 3D chart to represent three dimensional data.

Three dimensional data is often available in one of two formats: Three vectors, each holding x, y, and z

components of points which, when interconnected, form a trajectory (path) in space

Three matrices, each holding x, y, and z components of points which, when interconnected, form a surface in space

Page 12: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Trajectory plots in 3D

Trajectory plots in 3D are based on three vectors. They are generally characterized by one free and

two dependent, or no free and three dependent vectors.

[y, z]= f(x)

or

[x, y, z]= g(t)

Page 13: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Trajectory plots in 3D

z= linspace(0,4,1000);

x= z.*cos(exp(z));

y= z.*sin(exp(z));

plot3(x,y,z,'r')

grid on

Page 14: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Trajectory plots in 3D

t= linspace(0,10,4000);

z= -sin(t);

y= sqrt(1-z.^2).*cos(20*t);

x= sqrt(1-z.^2).*sin(20*t);

comet3(x,y,z)

Page 15: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Surface plots in 3D

Surface plots are based on three matrices. They are generally characterized by two

independent (free) matrices and one dependent matrix.

Z= f(X, Y) The independent matrices generally contain a

rectangular grid of coordinates on the X-Y plane. If we are only interested in the shape, rather than

the coordinates of the surface, then we can build the plot by using only the Z matrix.

Page 16: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Building the X-Y grid (1)

Assume that a surface chart is to be drawn in the range –2x2 and 2y8.

We want data to be plotted for all integer x in the range, and only even numbers in y.

This implies a rectangular grid whose x-coordinates are [-2, -1, 0, 1, 2].

The y-coordinates of this grid are[2, 4, 6, 8].

Page 17: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Building the X-Y grid (2)

We can represent this 4x5= 20-point grid by putting the x-coordinates of every point into a X matrix, and the corresponding y-coordinates into a Y matrix.

The resulting matrices would then look like this:

21012

21012

21012

21012

X

88888

66666

44444

22222

Y

Page 18: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Building the X-Y grid (3)

A function named MESHGRID is very useful in automatically creating the X and Y matrices out of x and y vectors of the grid.

x= [-2 -1 0 1 2];

y= [2 4 6 8];

[X,Y]= meshgrid(x, y);

Page 19: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Building the Z matrix

Once the X-Y grid is ready, building the actual surface coordinates into a matrix is a matter of writing an equation of X and Y:

Z= 0.4 - 0.4*X.^2 - 0.1*Y.^2 + 1.2*Y;

surf(X,Y,Z)

Page 20: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Surface plots

We often want a large number of points in the grid for seeing a better rendered surface.

When x- and y-axis grids are identical, we do not need to create separate x and y vectors.

% Egg carrier

u= linspace(-10, 10, 50);

[X,Y]= meshgrid(u, u);

Z= sin(X) + sin(Y);

surf(X, Y, Z)

Page 21: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Surface plot types

Contour (contour lines where the surface crosses certain z-levels)

Surf (color-filled surface patches) Mesh (displays only colored lines between Z data

points) Special shapes (spheres, cylinders, fills, etc.)

Page 22: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

PEAKS function

PEAKS is a function of two variables, obtained by translating and scaling Gaussian distributions.

It creates a nice looking surface, which is useful for demonstrating 3D plots such as MESH, SURF, CONTOUR, etc.

For the surface charts on tnhe following slides, we will assume X, Y and Z matrices generated by the following code:

u= linspace(-3, 3, 50);[X, Y]= meshgrid(u, u);Z= peaks(X, Y);

Page 23: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

2D CONTOUR plots

Contour plots are probably the only chart types that can successfully represent three dimensional data in 2D.

They project the cutting points of particular z-levels onto a flat surface.

The plot on the right top is a default contour plot, while the one at the bottom is a filled contour of 20 levels.contour(X, Y, Z); figure;contourf(X, Y, Z, 20);

Page 24: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

3D CONTOUR plots

3D contour plots display contour data in their actual intersection surfaces in space.

The plot on the right top is a default 3D contour plot, while the one at the bottom is a 3D contour plot of 20 levels.contour3(X, Y, Z); figure;contour3(X, Y, Z, 20); grid off

Page 25: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

SURF plot - faceted

This is the default SURF plot. Actual data points are at places where the x and y lines intersect.

surf(X, Y, Z);

shading('faceted');

Page 26: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

SURF plot - flat

In this SURF plot, the lines in x and y directions are removed to show only colored rectangles.

surf(X, Y, Z);

shading('flat');

Page 27: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

SURF plot - interpolated

In this SURF plot, the lines in x and y directions are removed, and additionally data is interpolated to display continuous colors.

surf(X, Y, Z);

shading(‘interp');

Page 28: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

SURFC plot

In the SURFC plot, we have the SURF plot with additional contour lines projected onto the X-Y plane.

surfc(X, Y, Z);

Page 29: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

SURFL plot

In the SURFL plot, we have the SURF plot with highlights from a light source.

surfl(X, Y, Z);

Page 30: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

MESH plot

MESH plots give the colored parametric mesh defined by the given matrices.

mesh(X,Y,Z);

Page 31: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

MESH plot – HIDDEN OFF

MESH plots by default hide the shapes behind any drawn mesh area. To display those, you can turn the hidden mode OFF.

mesh(X,Y,Z);

hidden off

Page 32: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

MESHC plot

MESHC plots, in addition to the mesh plot, the contour lines projected to the X-Y plane.

meshc(X,Y,Z);

Page 33: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

MESHZ plot

MESHZ plots give the colored parametric mesh defined by the given matrices. The small plot is with hidden mode turned off.

meshz(X,Y,Z);

Page 34: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

WATERFALL plot

WATERFALL is similar to MESHZ, but without any lines drawn in the column direction.

waterfall(X,Y,Z);

Page 35: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Special types: FILL and FILL3% Five arm star, with 72° between armsfor k=1:10 a= (k-1)*36; if mod(k,2)==1 % arm x(k)= sin(a/180*pi); y(k)= cos(a/180*pi); else % trough x(k)= sin(a/180*pi)*0.382; y(k)= cos(a/180*pi)*0.382; endendsubplot(3,1,1)fill(x,y,'y'); axis('square')subplot(3,1,2)% Set color to the point’s y heightfill(x,y,y); axis('square')z=y*0.5;subplot(3,1,3)fill3(x,y,z,y); axis('square')

Page 36: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Special types: Sphere, etc.

There are ready functions for some known 3D shapes: SPHERE ELLIPSOID CYLINDER

[x,y,z]= ellipsoid(1,1,1,4,6,3);

surfl(x,y,z);

colormap copper

axis equal

Page 37: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Advanced formatting: Handles

For more advanced formatting of charts, we can obtain a handle for the plot while creating it, and modify plot properties through handle operations.

All chart creation commands return handles when we assign the command to a variable. Examples:

h_plot= surf(X, Y, Z);

h_title= title(‘Peaks in 3D'); A get command to the handle will list all properties

associated with that item.

get(h_plot);

get(h_title);

Page 38: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Advanced formatting: Handles

You can see the settings associated with an item, and their present values using the GET command:

>> get(h_title)BackgroundColor = noneColor = [0 0 0]EdgeColor = noneEraseMode = normalEditing = offExtent = [0 0 0 0]FontAngle = normalFontName = HelveticaFontSize = [10]FontUnits = pointsFontWeight = normalHorizontalAlignment = centerLineStyle = -LineWidth = [0.5]Margin = [2]Position = [-1.24135 -1.61775 21.2632]Rotation = [0]String = Peaks in 3DUnits = dataInterpreter = texVerticalAlignment = bottom

Page 39: Plotting – 3-Dimensional. 3D Plots versus 2D Plots 3-dimensional plots, in contrast to 2-dimensional ones, has a third axis (often called the z-axis)

Advanced formatting: Handles

The SET command can be used to modify properties of the item associated with the handle:

set(h_plot, 'LineStyle', ':');

set(h_title, 'FontSize', 16, 'FontWeight', 'bold')