Introduction to M ATLAB 2 Graphics Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment

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

MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
CSE 123 Plots in MATLAB. Easiest way to plot Syntax: ezplot(fun) ezplot(fun,[min,max]) ezplot(fun2) ezplot(fun2,[xmin,xmax,ymin,ymax]) ezplot(fun) plots.
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Plotting Selim Aksoy Bilkent University Department of Computer Engineering
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.
Matrix Manipulation and 2D Plotting
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Chapter 11 Above: Principal contraction rates calculated from GPS velocities. Visualized using MATLAB.
MATLAB’s extensive, device-independent plotting capabilities are one of its most powerful features. They make it very easy to plot any data at any time.
M AT L AB Graphics. Basic Plotting Commands figure : creates a new figure window plot(x) : plots line graph of x vs index number of array plot(x,y) :
Graphics in MATLAB Week 15 – 5/5/09 Kate Musgrave
EGR106 Week 6 MATLAB FILES Two Dimensional Plots Multiple Plots
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.
 1999 BG Mobasseri1 8/29/2015 MATLAB GRAPHICS - PART II ADVANCED PLOTTING June 4, ‘99.
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.
CMPS 1371 Introduction to Computing for Engineers PLOTTING.
MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called.
A short intermission about function handles and cell arrays A MATLAB function may be referenced by a handle. >> sphere(100)
Matlab tutorial course Lesson 5: Loading and writing data, producing visual output
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 Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
MA/CS 3751 Fall 2002 Lecture 24. MA/CS 3752 ginput ginput is a Matlab function which takes one argument input: number of points to select in the image.
COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing.
MATLAB and SimulinkLecture 51 To days Outline Linear Equations ODE Integration Handle Graphics. Exercises on this days topics.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 9”
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Outline Announcements –Homework I due Wed. 5PM by Subject: CIS 402 Homework 1 Plain text Not allowed to use errorbar Try to do plotCI without looking.
Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.
Introduction to MATLAB Session 5 Simopekka Vänskä, THL 2010.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Figures & Axes, Printing & Saving. Outline Announcements –Homework I due Wed. 5PM by –No lecture on Fri. 10/26, rescheduled to Wed. 10/31 at 8AM.
Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab.
Recap Plots with More than one Line Plots of Complex Arrays Line, Color and Mark Style Axis Scaling and Annotating Plots Subplots Polar Plots Logarithmic.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
June 14, ‘99 COLORS IN MATLAB.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
GRAPHICS AND VISUALISATION WITH MATLAB UNIVERSITY OF SHEFFIELD CiCS DEPARTMENT Deniz Savas & Mike Griffiths May 2015.
Introduction to Engineering MATLAB – 9 Plotting - 2 Agenda Formatting plots.
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.
More Matlab Graphics and GUI Graphics subplots some useful commands 3D graphics GUI GUI controls The callback property Other essential properties.
An Introduction to Programming in Matlab Emily Blumenthal
Beginning Programming for Engineers Animation. Special matrix functions >> clear >> ones(2,3) >> zeros(3,5) >> ones(2) >> zeros(3) >> eye(4) >> magic(4)
EEE 242 Computer Tools for Electrical Engineering
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.
Reading and Writing Image Files
John Federici NJIT Physics Department
Inserting and Working with Images
Plotting Chapter 5.
3D-Graphs A 3D surface is defined as: z = f(x, y)
Lecture 25.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Ch3 Graphics Overview of Plotting Editing Plots
Lecture 25: Exploring data
Two-Dimensional Plots
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files) Double click this icon
Tutorial 2 SEG7550 Introduction to MATLAB II
Plotting Multiple Graphs In The Same Plot
MATLAB How to use (using M-files)
Yang-Ming University, Taipei, Taiwan
Presentation transcript:

Introduction to M ATLAB 2 Graphics Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment

GRAPHICS Basic Plotting –Linetypes, markers, colours, etc Subplots – creating multiple axes on a single figure Annotating figures – titles, labels, legends Editing figure properties Other plotting functions

Basic Plotting Commands figure : creates a new figure window plot(x) : plots line graph of x vs index number of array plot(x,y) : plots line graph of x vs y plot(x,y,'r--') : plots x vs y with linetype specified in string : 'r' = red, 'g'=green, etc for a limited set of basic colours.

Plot(x,y,…properties) Plot(x,y,cml) – plot with a basic colour-marker- line combination (cml) –‘ro-’, ‘g.-’, ‘cv--‘ –Basic colours : r, g, b, k, y, c, m –Basic markers : ‘o’, ‘v’, ‘^’, ’ ’, ‘.’, ‘p’, ‘+’, ‘*’, ‘x’, ‘s’, ‘h’, ‘none’ –Linetypes : ‘-’, ’--‘, ‘-.’, ‘:’, ‘none’

More detailed plot options can be specified through the use of property:value pairs –Plot(x,y,’o-’,’property’,value) ‘property’ is always a string naming the propery, value may be a number, array, or string, depending on the property type: ‘color’ : [R G B] - color (american spelling!) is specified with a [red green blue] value array, each in range 0-1. [1 0 0] – pure red [ ] – orange

–‘linewidth’ : specified as a single number. Default = 0.5 –‘linestyle’ : any of line type strings - ‘-’,’:’,etc –‘marker’ : any of marker strings – ‘v’, ’o’, etc –‘markersize’ : number, default = 6 –‘markeredgecolor’ : color string ‘r’, ‘g’, or [R G B] value for the line defining edge of marker –‘markerfacecolor’ : color string or [R G B] for inside of marker (can be different from edge) Can add any number of property:value pairs to a plot command: >> Plot(x,y,’prop1’,value1,’prop2’,value2,..)

Property Editor — Axis Tree of objects Edited objects Help for object

GUI property browser & editor Access from the figure’s view menu

Subplots subplot(m,n,p) : create a subplot in an array of axes >> subplot(2,3,1); >> subplot(2,3,4); m n P=1 P=2 P=3 P=4

Adding Text to Figures Basic axis labels and title can be added via convenient functions: >> xlabel('x-axis label text') >> ylabel('y-axis label text') >> title('title text') Legends for line or symbol types are added via the legend function: >> legend('line 1 caption','line 2 caption',…) Legend labels are given in the order lines were plotted

‘Handle’ Graphics MATLAB uses a hierarchical graphics model –Graphics objects organised according to their dependencies: e.g. lines must be plotted on axes, axes must be positioned on figures –Every object has a unique identifier, or handle Handles are returned by the creating function –ax = subplot(3,2,n) –H = plot(x,y) Handles can be used to identify an object in order to inspect (get) or modify (set) its properties at any time

root figure axesUI-controlUI-menuUI-contextmenu linelightimagepatchsurfacerectangletext Object Hierarchy The computer desktop Figure window Objects at any level are considered to be ‘children’ of their ‘parent’ object.

Properties of an object with handle H, can be inspected/modified by: >> value = get(H,'propertyname') >> set(H,'propertyname',value) All property values echoed to screen by: >> get(H) 3 useful functions: –gcf : get current figure – returns handle of current figure –gca : get current axes – returns handle of current axes –gco : get current object – returns handle of current object Can use these directly, instead of the handle

Current object is last created (usually), or last object clicked on with mouse. >> pp = get(gca,'position') pp = >> set(gca,'position',pp+[ ]) The code above first gets the position of the current axes – location of bottom left corner (x 0, y 0 ), width (dx) and height (dy) (in normalised units) – then resets the position so that the axes sit 0.1 units higher on the page and decreases their height by 0.1 units.

Other plotting functions Matlab has many plotting functions – we will use a simple data set representing a surface to try these out. Enter the command peaks z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) *(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) /3*exp(-(x+1).^2 - y.^2) Peaks sets up a regular x and y grid, and defines the function z given above.

>> peaks; Peaks is an example function, useful for demonstrating 3D data, contouring, etc. Figure above is its default output. P=peaks; - return data matrix for replotting…

>> P = peaks; - save output of peaks in P >> contour(P) - contour P using default contour values x and y axes are simply the row and column numbers

>> contour(P,[-9:0.5:9]) – contour with specified contour values.

>> [C,h]=contour(P); - capture Contour value and handle >> clabel(C,h); - clabel function adds labels

>> [x,y,P] = peaks; >> contour(x,y,P,[-9:9]) x and y axes are true values

>> contourf(P,[-9:0.5:9]); - filled contour function >> colorbar - add colour scale

Pseudocolour plots An alternative to contouring – provides a continuous colour-mapped 2D data field pcolor(Z) : plot pseudocolour plot of Z pcolor(X,Y,Z) : plot of Z on grid X,Y shading faceted | flat | interp : set shading option –faceted : show edge lines (default) –flat : don't show edge lines –interp : colour is linearly interpolated to give smooth variation

>> pcolor(P)>> shading flat >> shading interp Data points are at vertices of grid, colour of facet indicates mean value of surrounding vertices. Colours are selected by interpolating data range into a colormap

>> pcolor(P);shading flat >> hold on >> contour(P,[1:9],'k') >> contour(P,[-9:-1],'k--') >> contour(P,[0 0],'k','linewidth',2) >> colorbar

colormaps Surfaces are coloured by scaling the data range to the current colormap. A colormap applies to a whole figure. Several predefined colormaps exist ('jet' (the default), 'warm','cool','copper','bone','hsv'). Select one with >> colormap mapname >> colormap('mapname') The current colormap can be retrieved with >> map=colormap

>> colormap cool

>> caxis([0 8]) - caxis function sets the [min max] limits of >> colorbar the colour scale. Need to refresh colourbar

Colormaps are simply 3-column matrices of arbitrary length (default = 64 rows). Each row contains the [RED GREEN BLUE] components of the colour required, specified on a 0  1 scale. e.g. >> mymap =[ ] >> colormap(mymap)

Specialized Plotting Routines

Specialized Plotting Routines (Continued)

Load jun07_all_5km_means.mat Variables: mlat, mlon- latitude & longitude of measurements mT- mean air temperature msst- mean sea surface temperature mu, mv- mean wind components to east (x) and north (y) mws- mean wind speed Plot the different variables to get a feel for range of values and relationships between them. Plot positions of the points - geographical locations

>> load jun07_all_5km_means.mat >> who Your variables are: mT mlat mlon msst mu mv >> plot(mlon,mlat,'o‘)

>> plot(mlon,mlat,'o') >> hold on >> quiver(mlon,mlat,mu,mv)

Randomly distributed data can’t be used with contour, pcolor, surf, etc…data needs to be on a regular grid. Use meshgrid to generate arrays of x and y on a regular grid. [XX,YY] = meshgrid([-125.2:0.05:-124],[39.9:0.05:40.8]); Use griddata to interpolate mu and mv onto the XX,YY grid … use system help to check usage X-values requiredY-values required X and Y grids

>> [XX,YY] = meshgrid([-125.2:0.05:-124],[39.9:0.05:40.8]); >> gmws = griddata(mlon,mlat,mws,XX,YY); >> pcolor(XX,YY,gmws); shading flat; >> hbar = colorbar; >> hold on >> h1 = plot(mlon,mlat,'ko');

>> gu=griddata(mlon,mlat,mu,XX,YY); >> gv=griddata(mlon,mlat,mv,XX,YY); >> quiver(XX,YY,gu,gv,'k-'); >> set(h1,'markeredgecolor','r','markersize',5)

>> set(gca,'linewidth',2,'fontweight','bold') >> xlabel('Longitude'); ylabel('latitude') >> set(hbar,'linewidth',2,'fontweight','bold') >> set(get(hbar,'xlabel'),'string','(m s^{-1})','fontweight','bold') >> xlabel('Longitude'); ylabel('latitude') >> title('CW96 : June 07 : 30m wind field') >> load mendocinopatch.mat >> patch(mendocinopatch(:,1),mendocinopatch(:,2),[ ])

Printing From file menu: –‘print’ to send to a printer –Export to export to a file (gif, png, jpg, eps,…) Better to use command line > print filename.EXT -dEXT File-type extension ‘png’, ‘ps’, ‘.eps’ appropriate to file type you want Instruction to print engine to select file type (same format as a linux print destination)

> print myfigure.png -dpng > print myfigure.png –dpng –r300 -rNNN option specifies print resolution to NNN dpi

Common print file types –.ps- postscript (full page) –.eps- encapsulated postscript (embeddable, infinitely scalable vector graphic – best for publication) –.png- portable network graphic – best bitmap image option –.jpg- JPEG image NEVER USE THIS!

. png.jpg JPEG format is designed for photographic images with continuously varying tone. Hard edges suffer compression artefacts – distortions – ugly, and can make text impossible to read

.fig files > hgsave(gcf,’filename.fig’) Saves the current figure to a.fig file – this is a complete description of the figure as a set of matlab handle-graphics objects. Can reload the figure to make edits later > hgload(‘filename.fig’)