Download presentation
Presentation is loading. Please wait.
Published byDominic Reeves Modified over 8 years ago
1
Graphics Intro UC Berkeley Fall 2007, E7 Copyright 2004-7, Andy Packard and Roberto Horowitz. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. http://creativecommons.org/licenses/by-sa/2.0/
2
2-dimensional plots: plot You have already seen the command plot to make 2-d plots a = linspace(0,pi,80); b = sin(8*a); plot(a,b); % plot all points, connect with line plot(a,b,’rx’) % plot red ‘x’ markers at each point plot(a,b,’rx’,a,b,’b-’) % red ‘x’, blue line plot(a,b,’go’,a,a.*b,’r--’,a,a.^2,’b’) % 3 plots Optional formatting commands after any (or all) (x,y) pair
3
Formatting: Colors, symbols and line types Color (Point) Marker LineStyle b blue. point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star (none) no line y yellow s square k black d diamond w white v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram
4
hold, hold on, hold off Normally, each time plot is executed, the current plot is destroyed, and the new graph replaces it. Using the command hold on subsequent plots will simply add to whatever is already displayed. The command hold off switches back to the “replace” mode. Executing multiple hold on is no different than executing it once. The command hold off still only needs to be executed once to switch back.
5
axis The axis command controls the limits (horizontal and vertical) of the axes. Several uses are common (see >> help axis ) Direct specification of the limits axis([Xmin Xmax Ymin Ymax]) Retrieve the values (in a 1-by-4 array) of the limits V = axis; Freeze limits at current values. Useful in the hold on mode. axis manual Returns to default mode, where limits are chosen automatically, based on the data being plotted axis auto
6
grid The grid command adds grid lines to the plot. Several uses are common (see >> help grid ) Turn on major grid lines grid on Remove major and minor grid lines grid off Toggle (on→off, off→on) the major grid lines grid Toggle (on→off, off→on) the minor grid lines grid minor
7
title, ylabel, xlabel These commands add text to the plot in pre-specified locations. Although the behavior can be customized, in general –The title command adds specified text above the plot; –The ylabel command adds specified text beside the Y-axis; –The xlabel command adds specified text below the X-axis. Example t = linspace(0,10); a = 9.8; clf plot(t,0.5*a*t.^2) xlabel('Time') ylabel('Position') title(['Position vs Time for constant acceleration = ' num2str(a)])
8
subplot Create multiple axes in a figure with subplot. subplot(N,M,L) Example clf subplot(3,3,1) subplot(3,3,2) subplot(3,3,4) subplot(3,3,6) subplot(3,3,9) 1 2 3 4 5 6 7 8 9 WARNING: Unlike arrays, subplot counts across rows to define where the i th axes is, within in the array of axes Note: subplot simply creates the axes. You must use plot to create a line within the axes.
9
subplot clf x = linspace(0,1,30); y = x; subplot(3,3,1) plot(x,y,'r+') subplot(3,3,2) plot(x,y.^2,'r',x,y.^ 3,'b+') subplot(3,3,4) hold on plot(x,y) plot(x,2*y) subplot(3,3,6) plot(x,y) plot(x,2*y) subplot(3,3,9) plot(x,y,x,2*y)
10
subplot Recall the meaning of subplot(N,M,L) If the figure were subdivided into a N-by-M array, place an axes in the L th location. Therefore clf subplot(3,3,1) subplot(1,2,2) xlabel(’Xexample’) produces …
11
subplot Recall subplot(N,M,L). If L is a vector, then the created axes will span all of the locations referred to by L Therefore clf subplot(3,3,[1 2]) subplot(3,3,[3 6]) subplot(3,3,[8 9]) subplot(3,3,[4 7]) subplot(3,3,5) produces …
12
2-dimensional plots: plot The value of the variable H is –the “handle of the line object” created by plot –The handle is a unique ID (an “address” or “pointer”) of the specific line that was drawn. line objects are children of axes objects axes objects are children of figure objects Plotting data in Matlab actually creates line objects. The handle of the line object is the output argument of plot >> a = linspace(0,2*pi,500); >> b = sin(a); >> H = plot(a,b); figure axes line
13
Is there a @handle class? For some reason, no. The variable returned by plot is simply an object of class double. >> H = plot(a,b); >> class(H), size(H), H So, the variable H is not the line object. The value of H is the address of the line object. We say – the “value of H points to the line object” or just… – “ H points to the object.” Clearing the variable from the workspace does nothing to the line itself. >> whos >> clear H >> whos The variable is gone, but the line object remains (look at the figure).
14
Access the properties via the handle Use get to discover the public properties of the object. >> PubProp = get(H) Fieldnames are the properties, and values are current values. Access specific properties, using the PropertyName. >> get(H,’Type’) >> get(get(H,’Parent’),’Type’) Change the properties with set(Handle,PropertyName,NewValue) >> set(H,’Linewidth’,3); >> set(H,’Marker’,’square’); Delete the object using delete. The variable H whose value is the handle of the object remains, but is now just a scalar double. >> whos % H is there, so is line >> delete(H) % line object disappears >> whos % H is still there >> get(H) % no graphics object % associated with this value
15
What does subplot return? The command plot returns the handle(s) to the line objects it creates. Similarly, the command subplot returns handles to the axes object it creates. >> A1 = subplot(3,3,1) >> A2 = subplot(3,3,2) Be careful with the distinction between axes objects and the axis command. The commands grid, hold, axis simply set certain properties of the current axes object. >> get(A1,’NextPlot’) >> hold on >> get(A1,’NextPlot’) >> [get(A1,’Xlim’) get(A1,’YLim’)] >> axis([-10 10 0 1]); >> [get(A1,’Xlim’) get(A1,’YLim’)]
16
The figure command By itself, the command figure creates a new figure object. The handle of the figure is returned as an argument. >> F1 = figure; >> F2 = figure; >> F3 = figure; The handle of the current figure can be accessed with >> gcf % or >> get(0,’CurrentFigure’) Clicking in a figure makes that the CurrentFigure. Alternatively, if Fhan is a figure handle, then >> figure(Fhan) makes it the current figure.
17
The axes command By itself, the command axes creates a new axes object within the current figure ( subplot is a special case). The handle of the axes is returned as an argument. >> A1 = axes; Properties (position, limits, tick marks, etc) can be accessed and modified with get and set. The handle of the current axes can be accessed with >> gca % or >> get(gcf,’CurrentAxes’) Clicking in a axes makes that the CurrentAxes. Alternatively, if Ahan is a axes handle, then >> axes(Ahan) makes it the current axes
18
Where does plot plot? When the command plot is executed –the plot goes to the current axes of the current figure –If no axes, Matlab creates one in the current Figure –If no figure, then Matlab first creates a Figure object By setting the current axes with the axes command, you can control where the plots go in a complex graphical environment with many figures, and perhaps many axes within each figure. >> axes(Ahan1) >> plot(x,y) >> axes(Ahan4) >> plot(z,w,’ro’,z,q,’g+’)
19
Creating in different workspaces Create a line object, using plot, inside a function. After function exits, the line object still exists, unlike the function workspace. >> a = 1:.01:10; >> b = cos(a); >> mkplot(a,b); So, while a line object is created in a workspace (here the function workspace), the line object is not tied to the workspace. The line object continues to exist, even after the workspace where it was created is destroyed. With the handle, properties can be accessed/modified. >> % Click on Line >> LHan = gco; % Get Current Object >> set(LHan,’Linewidth’,3) function Newy = mkplot(x,y) tmp = 1+y; Newy = tmp.^2; plot(x,Newy)
20
Creating/Changing in different workspaces Passing back the object’s handle makes further access easier >> a = 1:.01:10; >> b = cos(a); >> [y,H] = mkplot2(a,b); >> get(H,’Type’) >> mychange(H) Message: Regardless of what is the “current” workspace when the object is created, the object’s properties are always accessible through its handle. function [Newy,h] = mkplot2(x,y) tmp = 1 + y; Newy = tmp.^2; h = plot(x,Newy) function mychange(LH) cc = get(LH,’Color’); set(LH,’Color’,cc([2:end 1]))
21
Graphical objects.vs. Variables Contrast the attributes of the graphical objects with the attributes of Matlab variables (like double, cell, char, struct and objects of user-defined classes) Graphics Objects: –Are referred to by an address, called the “handle” –Have a get/set interface to access/modify –Are not associated with a workspace Matlab variables: –Are referred to by name –Are referenced directly to access/modify ( double, cell, etc) –Live in a workspace –Cease to exist when the workspace they live in is deleted.
22
Graphical Objects in Matlab Some of the different types of graphical objects in Matlab are axes uicontextmenu uimenu figure uicontrol line patch surface text Matlab session, the Root object child of root children of figure children of axes Handle equals 0 … …
23
Root Object Constructor –There is none, it is created when Matlab is started Properties (try get(0) and set(0) to discover them) –Diary, DiaryFile –Format, FormatSpacing –PointerLocation, PointerWindow –RecursionLimit –CurrentFigure
24
figure Objects Constructor –figure –Automatically constructed when creating an axes, uicontrol… Properties (lots…) –Position –Color –CurrentPoint –HandleVisibility Events/Callbacks –KeyPressFcn –CloseRequestFcn –ResizeFcn –WindowButtonDownFcn –WindowButtonMotionFcn –WindowButtonUpFcn
25
axes Objects Constructor –axes Properties –CameraPosition –CurrentPoint –NextPlot –XTick, XTickLabel, XScale, XLim, XGrid, XDir –YTick, YTickLabel, … –ZTick, ZTickLabel, … –…–… Events/Callbacks –ButtonDownFcn –DeleteFcn
26
uicontrol Objects Constructor –uicontrol Properties –Style checkbox, pushbutton, edit, text, frame, popupmenu, listbox, radiobutton, slider, togglebutton –ToolTipString –Value –Enable Events/Callbacks –Callback –DeleteFcn –ButtonDownFcn
27
Position and Units properties Several objects ( figure, axes, uicontrol ) have properties named ’Position’ and ’Units’. How are these interrelated? If F is the handle of a figure, then get(F,’Position’) is the position (the 1-by-4 array [LLX,LLY,Width,Height] ) relative to the LowerLeft corner of screen, expressed in the units of get(F,’Units’) Note that get(0,’Units’) plays no role
28
Position and Units properties If A is the handle of a axes, then get(A,’Position’) is the position (the 1-by-4 array [LLX,LLY,Width,Height] ) relative to the LowerLeft corner of parent figure, expressed in units of get(A,’Units’) –Note that get(get(A,’Parent’),’Units’) plays no role. If UI is the handle of a uicontrol, then get(UI,’Position’) is the position (the 1-by-4 array [LLX,LLY,Width,Height] ) relative to the LowerLeft corner of parent figure, expressed in units of get(UI,’Units’) –Again, get(get(UI,’Parent’),’Units’) plays no role.
29
hnudge: Horizontal axes nudge function hnudge(arg1,arg2) % hnudge Moves CurrentAxes one pixel to right % hnudge(N) Moves CurrentAxes N pixels to right % hnudge(N,AH) Moves axes AH N pixels to right if nargin==0 AH = gca; N = 1; elseif nargin==1 AH = gca; N = arg1; elseif nargin==2 AH = arg2; N = arg1 end OrigUnits = get(AH,'Units'); set(AH,'Units','Pixels'); OrigPosition = get(AH,'Position'); NewPosition = OrigPosition + [N 0 0 0]; set(AH,'Position',NewPosition); set(AH,'Units',OrigUnits);
30
CurrentPoint properties If F is a figure handle, then get(F,’CurrentPoint’) –is the pointer location [X,Y] relative to the LL corner of screen, expressed in the units of get(F,’Units’) If A is an axes, handle, then get(A,’CurrentPoint’) –is a 2x3 array of the “position” of the pointer, in the coordinates of the axes. –Take line perpendicular to screen, passing through pointer. Here get(A,’Units’) plays no role.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.