lecture 13

22
1 Lecture 13: Handle Graphics, 3D plots Table of Contents Handle Graphics ................................................................................................................. 1 Graphics - IV: 3-D Plots ...................................................................................................... 5 plot3 ................................................................................................................................. 7 Mesh and Surface Plots ........................................................................................................ 9 Plots with Special Graphics ................................................................................................. 13 Polar coordinates grid in the xy plane ................................................................................... 18 View ............................................................................................................................... 19 Rotate View ..................................................................................................................... 21 Graphic Handles, 3D plots, Mesh and Surface Plots are discussed. Handle Graphics %For controlling the graphic appearance or for creating animations etc. %Using Handle Graphics one can get handles of graphic objects and change %the properties of the objects. %get(handle) gets a list of all property names and their current values. %get(handle,'PropertyName') gets the current value of the sought property. %set(handle,'PropertyName','PropertyValue') Any property can be changed %using this. clf; t = linspace(0,2*pi,100); y = t.*sin(t); hg1 = plot(t,y) get(hg1) hg1 = 175.01 DisplayName: '' Annotation: [1x1 hg.Annotation] Color: [0 0 1] LineStyle: '-' LineWidth: 0.5 Marker: 'none' MarkerSize: 6 MarkerEdgeColor: 'auto' MarkerFaceColor: 'none' XData: [1x100 double] YData: [1x100 double] ZData: [1x0 double]

Upload: anshu-kumar

Post on 01-Feb-2016

218 views

Category:

Documents


0 download

DESCRIPTION

lpl

TRANSCRIPT

Page 1: Lecture 13

1

Lecture 13: Handle Graphics, 3D plots

Table of ContentsHandle Graphics ................................................................................................................. 1Graphics - IV: 3-D Plots ...................................................................................................... 5plot3 ................................................................................................................................. 7Mesh and Surface Plots ........................................................................................................ 9Plots with Special Graphics ................................................................................................. 13Polar coordinates grid in the xy plane ................................................................................... 18View ............................................................................................................................... 19Rotate View ..................................................................................................................... 21

Graphic Handles, 3D plots, Mesh and Surface Plots are discussed.

Handle Graphics%For controlling the graphic appearance or for creating animations etc.%Using Handle Graphics one can get handles of graphic objects and change%the properties of the objects.

%get(handle) gets a list of all property names and their current values.%get(handle,'PropertyName') gets the current value of the sought property.

%set(handle,'PropertyName','PropertyValue') Any property can be changed%using this.

clf;t = linspace(0,2*pi,100);y = t.*sin(t);hg1 = plot(t,y)get(hg1)

hg1 =

175.01

DisplayName: '' Annotation: [1x1 hg.Annotation] Color: [0 0 1] LineStyle: '-' LineWidth: 0.5 Marker: 'none' MarkerSize: 6 MarkerEdgeColor: 'auto' MarkerFaceColor: 'none' XData: [1x100 double] YData: [1x100 double] ZData: [1x0 double]

Page 2: Lecture 13

Lecture 13: Handle Graphics, 3D plots

2

BeingDeleted: 'off'

ButtonDownFcn: [] Children: [0x1 double] Clipping: 'on' CreateFcn: [] DeleteFcn: [] BusyAction: 'queue' HandleVisibility: 'on' HitTest: 'on' Interruptible: 'on' Selected: 'off' SelectionHighlight: 'on' Tag: '' Type: 'line' UIContextMenu: [] UserData: [] Visible: 'on' Parent: 174.01 XDataMode: 'manual' XDataSource: '' YDataSource: '' ZDataSource: ''

set(hg1,'color','r')set(hg1,'linewidth',2,'linestyle','--')

Page 3: Lecture 13

Lecture 13: Handle Graphics, 3D plots

3

set(hg1,'marker','o')

%change the values of some y-coordinates by changing data pointsyvec = get(hg1,'ydata');yvec(15:20) = 0; yvec(40:45)=0;set(hg1,'ydata',yvec);

Page 4: Lecture 13

Lecture 13: Handle Graphics, 3D plots

4

• plotyy plot

x = 1:0.2:10;y1 = exp(-x).*sin(x);y2 = exp(x);Dy = plotyy(x,y1,x,y2);hy1 = get(Dy(1),'ylabel');hy2 = get(Dy(2),'ylabel');set(hy1,'string','e^-x sin(x)');set(hy2,'string','e^x');title('\bf Example of a Plotyy Plot');

Page 5: Lecture 13

Lecture 13: Handle Graphics, 3D plots

5

Graphics - IV: 3-D Plots%Extensive functions and tools for visualizing 3-D data.%There are several 3-D analogs of special 2-D plotting functions.%eg. plot3, stem3, bar3, pie3, contour3, etc.

help graph3d

Three dimensional graphs. Elementary 3-D plots. plot3 - Plot lines and points in 3-D space. mesh - 3-D mesh surface. surf - 3-D colored surface. fill3 - Filled 3-D polygons. Color control. colormap - Color look-up table. caxis - Pseudocolor axis scaling. shading - Color shading mode. hidden - Mesh hidden line removal mode. brighten - Brighten or darken color map. colordef - Set color defaults. graymon - Set graphics defaults for gray-scale monitors. cmpermute - Rearrange colors in colormap. cmunique - Eliminate unneeded colors in colormap of indexed image.

Page 6: Lecture 13

Lecture 13: Handle Graphics, 3D plots

6

imapprox - Approximate indexed image by one with fewer colors. Lighting. surfl - 3-D shaded surface with lighting. lighting - Lighting mode. material - Material reflectance mode. specular - Specular reflectance. diffuse - Diffuse reflectance. surfnorm - Surface normals. Color maps. hsv - Hue-saturation-value color map. hot - Black-red-yellow-white color map. gray - Linear gray-scale color map. bone - Gray-scale with tinge of blue color map. copper - Linear copper-tone color map. pink - Pastel shades of pink color map. white - All white color map. flag - Alternating red, white, blue, and black color map. lines - Color map with the line colors. colorcube - Enhanced color-cube color map. vga - Windows colormap for 16 colors. jet - Variant of HSV. prism - Prism color map. cool - Shades of cyan and magenta color map. autumn - Shades of red and yellow color map. spring - Shades of magenta and yellow color map. winter - Shades of blue and green color map. summer - Shades of green and yellow color map. Transparency. alpha - Transparency (alpha) mode. alphamap - Transparency (alpha) look-up table. alim - Transparency (alpha) scaling Axis control. axis - Control axis scaling and appearance. zoom - Zoom in and out on a 2-D plot. grid - Grid lines. box - Axis box. hold - Hold current graph. axes - Create axes in arbitrary positions. subplot - Create axes in tiled positions. daspect - Data aspect ratio. pbaspect - Plot box aspect ratio. xlim - X limits. ylim - Y limits. zlim - Z limits. Viewpoint control. view - 3-D graph viewpoint specification. viewmtx - View transformation matrix. rotate3d - Interactively rotate view of 3-D plot.

Page 7: Lecture 13

Lecture 13: Handle Graphics, 3D plots

7

Camera control. campos - Camera position. camtarget - Camera target. camva - Camera view angle. camup - Camera up vector. camproj - Camera projection. High level camera control. camorbit - Orbit camera. campan - Pan camera. camdolly - Dolly camera. camzoom - Zoom camera. camroll - Roll camera. camlookat - Move camera and target to view specified objects. cameratoolbar - Interactively manipulate camera. High level light control. camlight - Creates or sets position of a light. lightangle - Spherical position of a light. Graph annotation. title - Graph title. xlabel - X-axis label. ylabel - Y-axis label. zlabel - Z-axis label. text - Text annotation. gtext - Mouse placement of text. plotedit - Experimental graph editing and annotation tools. Hardcopy and printing. print - Print graph or Simulink system; or save graph to MATLAB file. printopt - Printer defaults. orient - Set paper orientation. See also GRAPH2D, SPECGRAPH.

plot3%syntax: plot3(x,y,z, 'style option', 'PropertyName','propertyvalue')%plots in 3-D are annotated in the same way as in 2-D plots.%eg. xlabel, ylabel, zlabel, title, text, grid, etc.

t = 0:0.1:10;x = exp(-0.2*t).*cos(2*t);y = exp(-0.2*t).*sin(2*t);

subplot(1,2,1)plot(x,y);title('\bf 2-D line plot');xlabel('\bf x')ylabel('\bf y')grid on

Page 8: Lecture 13

Lecture 13: Handle Graphics, 3D plots

8

subplot(1,2,2)plot3(x,y,t);title('\bf 3-D line plot');xlabel('\bf x')ylabel('\bf y')zlabel('\bf t')grid on

Page 9: Lecture 13

Lecture 13: Handle Graphics, 3D plots

9

Mesh and Surface Plots%Surface, mesh and contour plots are convenient ways to represent data that%is a function of two independent variables, i.e., z = f(x,y).

%To plot any functions of 2 indep. variables, one needs to create 3 arrays%of equal size.

%Three steps are involved: first generate a grid of (x,y) coordinates, then%evaluate the function at each grid point and finally create a 3D plot.

%creates an (X,Y) mesh gridu = linspace(-3,3,50);[X Y] = meshgrid(u,u);

%mesh plotZ = X.*Y.*(X.^2 - Y.^2)./(X.^2 + Y.^2);mesh(X,Y,Z)

%mesh and coontour plot (draws a contour plot beneath the mesh)figure(2)meshc(X,Y,Z); xlabel('x'); ylabel('y');zlabel('z');

%mesh curtain plot (draws a curtain around the mesh)figure(3)

Page 10: Lecture 13

Lecture 13: Handle Graphics, 3D plots

10

meshz(X,Y,Z); xlabel('x'); ylabel('y');zlabel('z');

Page 11: Lecture 13

Lecture 13: Handle Graphics, 3D plots

11

Page 12: Lecture 13

Lecture 13: Handle Graphics, 3D plots

12

clear X Y Zx = -3:0.25:3;y = -3:0.25:3;[X,Y] = meshgrid(x,y);Z = 1.8.^(-1.5*sqrt(X.^2 + Y.^2).*cos(0.5*X).*sin(X));%surface plotfigure(1)surf(X,Y,Z); xlabel('x'); ylabel('y');zlabel('z');

%surface and contour plot (draws a contour plot beneath the surface)figure(2)surfc(X,Y,Z); xlabel('x'); ylabel('y');zlabel('z');

Page 13: Lecture 13

Lecture 13: Handle Graphics, 3D plots

13

Plots with Special Graphics% * Plot a Sphere%syntax: sphere; Returns x,y,z coordinates of a unit sphere with 20 faces

[X,Y,Z] = sphere(20);surf(X,Y,Z)

Page 14: Lecture 13

Lecture 13: Handle Graphics, 3D plots

14

• Plot a cylinder

%Syntax: [A,B,C] = cylinder(r); It returns A,B,C coordinates of cylinder%with profile r ()t = linspace(0,2*pi,20);r = 1 + sin(t);[X,Y,Z] = cylinder(r);surf(X,Y,Z)axis('square')

Page 15: Lecture 13

Lecture 13: Handle Graphics, 3D plots

15

• 3-d bar plot

%Syntax: bar3(Y); Each element in Y is one bar. Columns are grouped together.Y = [1 6.5 7; 2 6 7; 3 5.5 7; 4 5 7; 3 4 7; 2 3 7; 1 2 7];bar3(Y)

Page 16: Lecture 13

Lecture 13: Handle Graphics, 3D plots

16

• 3-D Pie Plot

%syntax: pie3(X,explode)X = [5 9 14 20];explode = [0 1 0 0];pie3(X,explode)

Page 17: Lecture 13

Lecture 13: Handle Graphics, 3D plots

17

• 3-D Stem plot

%syntax: stem3(X,Y,Z)t = 0:0.2:10;x = t; y = sin(t); z = t.^1.5;stem3(x,y,z,'fill')grid onxlabel('x'); ylabel('y'); zlabel('z');

Page 18: Lecture 13

Lecture 13: Handle Graphics, 3D plots

18

Polar coordinates grid in the xy plane%A 3D plot of a function in which the value of z ( eg. z = r \theta) is%given in polar coordinates.

%To convert polar to cartesian coordinates use pol2cart function.

[th, r] = meshgrid((0:5:360)*pi/180, 0:0.1:2);Z = r.*th;[X,Y] = pol2cart(th,r);surf(X,Y,Z)xlabel('x'); ylabel('y'); zlabel('z');

Page 19: Lecture 13

Lecture 13: Handle Graphics, 3D plots

19

View%Specifies viewing angle of observer; projects 3-D objects on 2-D planes.%Useful for visualizing the perspectives of different geometrical shapes.

%syntax: view(azimuth,elevation)

%azimuth: Angle (in degrees) of rotation about the z-axis measured%CCW from the negative y-axis. It is in xy-plane.%elevation: vertical angle (in degrees) measured positive above the xy-plane.%The default veiwing angles are az = -37.5 degree and el = 30 degree.

t = linspace(0,6*pi,100);x = sqrt(t).*sin(2*t); y = sqrt(t).*cos(2*t); z = 0.5.*t;

subplot(2,2,1); plot3(x,y,z); grid on;

subplot(2,2,2); plot3(x,y,z); view(0,90); %xy (top view)

subplot(2,2,3); plot3(x,y,z); view(0,0); %xz (side view)

subplot(2,2,4); plot3(x,y,z); view(90,0); %yz (side view)

title('Illustration of 3-D plot, View')

Page 20: Lecture 13

Lecture 13: Handle Graphics, 3D plots

20

View(2) %same as view(0,90), shows projection in the xz-plane View(3) %same as view(-37.5,30), showsthe default 3D view

%Draw a filled circle in 2D and view it in 3Dtheta = linspace(0,2*pi,100);x = cos(theta);y = sin(theta);

subplot(1,2,1)fill(x,y,'g'); axis('square');subplot(1,2,2)fill(x,y,'g'); axis('square');view(3)

Page 21: Lecture 13

Lecture 13: Handle Graphics, 3D plots

21

Rotate View%Rotate in 3D button located in the toolbar of the figure window. Use mouse%to rotate the view.

%There is a utility function called rotate3d. Turn it on and rotate the%view using mouserotate3d on

Page 22: Lecture 13

Lecture 13: Handle Graphics, 3D plots

22

Published with MATLAB® R2013b