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.

Slides:



Advertisements
Similar presentations
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Advertisements

Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Lecture (4) Plotting & Programming (1) Eng. Osama Talaat 1.
2D Plots 1 ENGR 1181 MATLAB 12.
Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
Matlab Graphics S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: 2D Graphics.
1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 4.
MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:
EGR106 Week 6 MATLAB FILES Two Dimensional Plots Multiple Plots
Vectors and Plotting Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Week 3 17 November Outline Graphics – Basic plotting – Editing plots from GUI – Editing plots from m-file – Advanced plotting commands.
EGR106 Week 4 Two Dimensional Plots Multiple Plots Plot Formatting Homework.
Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.
Lecture 9 Plotting in 2-D Plotting in 2-D, Plotting Multiple Curves, Plotting with Figures, Plot Settings, Scaling, Legends © 2007 Daniel Valentine. All.
Plotting. Basic Plotting Two vectors of x and y values needed. Vectors need to be of the same length, but not necessarily of the same geometry. For example,
An introduction to Plotting in MATLAB Rikard Johansson Department of Biomedical Engineering (IMT) Linköping University
MATLAB Fundamentals.
Programming a GUI Hanan sedaghat pisheh. For calling GUI, we need a function with no inputs or outputs First We create a m.file m file has the same name.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
PLOTS AND FIGURES DAVID COOPER SUMMER Plots One of the primary uses for MATLAB is to be able to create publication quality figures from you data.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()
A short intermission about function handles and cell arrays A MATLAB function may be referenced by a handle. >> sphere(100)
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
MATLAB and SimulinkLecture 51 To days Outline Linear Equations ODE Integration Handle Graphics. Exercises on this days topics.
Introduction to Matlab’s Graphical User Interface (GUI) Type “guide” “Guide” creates interface on a Figure window and code in an M-file. Some hidden code.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Introduction to MATLAB Damon Tomlin February 22, 2008 Lecture 3: Data Visualization & User Interfaces.
HandleGraphics Intro UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB – 2D Plots 1 Go to the class drive: ---> MatLab - Graphing ---> Seed Files.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Plot (x-values, y-values) >> x = linspace (-3, 3, 20); >> x = linspace (-3, 3, 20); >> y = 2*x – 1; >> y = 2*x – 1; >> plot (x, y) >> plot (x, y)
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
EGR 115 Introduction to Computing for Engineers 2D Plotting – Part I Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to Engineering MATLAB – 9 Plotting - 2 Agenda Formatting plots.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
E177, Rules of Interruptibility of Callbacks March 4, Copyright , Andy Packard. This work is licensed.
Figures & Axes, Printing & Saving. Outline Announcements –Homework I on web, due Wed. 5PM by –No lecture on Fri. 10/26, rescheduled to Wed. 10/31.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Lab 2 : potting to Matlab Networks.
More Matlab Graphics and GUI Graphics subplots some useful commands 3D graphics GUI GUI controls The callback property Other essential properties.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
MAE 421 Matlab review Prof. Mark Glauser Created by: David Marr.
Figures & Axes, Printing & Saving. Outline Announcements –Homework I on web, due Wed. 5PM by e- mail A word on HWI and cookies What happens when you plot.
EGR 106 – Week 5 – 2-D Plots Question : Why do engineers use plots? Answer : To analyze, visualize, and present data. Matlab has many useful plotting options.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Creating a Plot The plot function can take one or more inputs; e.g. plot (x, y) or plot (y) Note that in the two-argument version, the vectors x and.
3D-Graphs A 3D surface is defined as: z = f(x, y)
Course Information Do not rely on color to distinguish curves (due to black-white printout). Use different line styles to distinguish curves (solid, dashed,
Lecture 25: Exploring data
Matrices and Arrays.
Net 222: Communications and networks fundamentals (Practical Part)
Net 222: Communications and networks fundamentals (Practical Part)
Net 222: Communications and networks fundamentals (Practical Part)
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files) Double click this icon
E177, Graphics Objects in Matlab February 26, me
MATLAB How to use (using M-files)
Introduction to MATLAB Plotting LAB 3
Plotting Signals in MATLAB
How to Use MATLAB A Brief Introduction.
Introduction to Matlab
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

Graphics Intro UC Berkeley Fall 2007, E7 Copyright , Andy Packard and Roberto Horowitz. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

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

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

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.

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

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

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)])

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) 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.

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)

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 …

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 …

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

Is there 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).

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

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([ ]); >> [get(A1,’Xlim’) get(A1,’YLim’)]

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.

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

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+’)

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)

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]))

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.

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 … …

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

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

axes Objects Constructor –axes Properties –CameraPosition –CurrentPoint –NextPlot –XTick, XTickLabel, XScale, XLim, XGrid, XDir –YTick, YTickLabel, … –ZTick, ZTickLabel, … –…–… Events/Callbacks –ButtonDownFcn –DeleteFcn

uicontrol Objects Constructor –uicontrol Properties –Style checkbox, pushbutton, edit, text, frame, popupmenu, listbox, radiobutton, slider, togglebutton –ToolTipString –Value –Enable Events/Callbacks –Callback –DeleteFcn –ButtonDownFcn

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

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.

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);

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.