Graphics Example a final product:

Similar documents
INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

Classes 7-8 (4 hours). Graphics in Matlab.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

INC151 Electrical Engineering Software Practice. MATLAB Graphics. Dr.Wanchak Lenwari :Control System and Instrumentation Engineering, KMUTT 1

Mechanical Engineering Department Second Year (2015)

CSE 123. Plots in MATLAB

Basic Graphs. Dmitry Adamskiy 16 November 2011

Overview. Lecture 13: Graphics and Visualisation. Graphics & Visualisation 2D plotting. Graphics and visualisation of data in Matlab

Logical Subscripting: This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression.

Lab of COMP 406 Introduction of Matlab (II) Graphics and Visualization

Graphics in MATLAB. Responsible teacher: Anatoliy Malyarenko. November 10, Abstract. Basic Plotting Commands

Dr Richard Greenaway

FF505/FY505 Computational Science. MATLAB Graphics. Marco Chiarandini

GUI Alternatives. Syntax. Description. MATLAB Function Reference plot. 2-D line plot

PROGRAMMING WITH MATLAB WEEK 6

This module aims to introduce Precalculus high school students to the basic capabilities of Matlab by using functions. Matlab will be used in

Dr. Iyad Jafar. Adapted from the publisher slides

Introduction to MATLAB: Graphics

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial Vb

MATLAB Functions and Graphics

TOPIC 6 Computer application for drawing 2D Graph

Matlab Tutorial 1: Working with variables, arrays, and plotting

Graphics and plotting techniques

Computer Programming in MATLAB

2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command...

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development

QUICK INTRODUCTION TO MATLAB PART I

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

1 Introduction to Matlab

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

GRAPHICS AND VISUALISATION WITH MATLAB

Computing Fundamentals Plotting

MATLAB Introduction to MATLAB Programming

STAT 391 Handout 1 Making Plots with Matlab Mar 26, 2006

Plotting - Practice session

What is Matlab? A software environment for interactive numerical computations

Additional Plot Types and Plot Formatting

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

fplot Syntax Description Examples Plot Symbolic Expression Plot symbolic expression or function fplot(f) fplot(f,[xmin xmax])

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

1. Register an account on: using your Oxford address

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

Introduction to Matlab

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

Lecture 6: Plotting in MATLAB

Math Sciences Computing Center. University ofwashington. September, Fundamentals Making Plots Printing and Saving Graphs...

Part #6. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Purpose of the lecture MATLAB MATLAB

Plotting x-y (2D) and x, y, z (3D) graphs

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

MATLAB SUMMARY FOR MATH2070/2970

Math 7 Elementary Linear Algebra PLOTS and ROTATIONS

ENGG1811 Computing for Engineers Week 11 Part C Matlab: 2D and 3D plots

PART 1 PROGRAMMING WITH MATHLAB

Introduction to MATLAB LAB 1

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Assignment :1. 1 Arithmetic Operations : Compute the following quantities ) -1. and compare with (1-

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

AMS 27L LAB #2 Winter 2009

Lab #1 Revision to MATLAB

Armstrong Atlantic State University Engineering Studies MATLAB Marina 2D Plotting Primer

Introduction to Matlab

A Guide to Using Some Basic MATLAB Functions

Introduction to Matlab

Chapter 3: Introduction to MATLAB Programming (4 th ed.)

Basic plotting commands Types of plots Customizing plots graphically Specifying color Customizing plots programmatically Exporting figures

PSY8219 : Week 6. Homework 5 Due Today. Homework 6 Due October 8. Readings for Today Attaway Chapter 6, 10, and 12

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

INTRODUCTION TO MATLAB

Introduction to MATLAB

MATLAB Guide to Fibonacci Numbers

Introduction to graphics

workspace list the variables and describe their matrix sizes 4x1 matrix (4 rows, 1 column) x=[3.4, 7, 2.2] 1x3 matrix (1 row, 3 columns)

Math 375 Natalia Vladimirova (many ideas, examples, and excersises are borrowed from Profs. Monika Nitsche, Richard Allen, and Stephen Lau)

Programming 1. Script files. help cd Example:

Programming in Mathematics. Mili I. Shah

Introduction to MATLAB Practical 1

W1005 Intro to CS and Programming in MATLAB. Plo9ng & Visualiza?on. Fall 2014 Instructor: Ilia Vovsha. hgp://

LabVIEW MathScript Quick Reference

Introduction to GNU-Octave

EGR 102 Introduction to Engineering Modeling. Lab 05B Plotting

A very brief Matlab introduction

3D plot of a surface in Matlab

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

Introduction to Matlab

MATLAB Laboratory 09/23/10 Lecture. Chapters 5 and 9: Plotting

Computational Foundations of Cognitive Science. Inverse. Inverse. Inverse Determinant

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Introduction to MATLAB

Basic Plotting. All plotting commands have similar interface: Most commonly used plotting commands include the following.

Lecturer: Keyvan Dehmamy

Getting Started with MATLAB

Chapter 2 (Part 2) MATLAB Basics. dr.dcd.h CS 101 /SJC 5th Edition 1

ECE 201 Matlab Lesson #2 Basic Arithmetic and Plotting. Element-by-Element Arithmetic for Vectors and Matrices

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Transcription:

Basic 2D Graphics 1

Graphics Example a final product: TITLE LEGEND YLABEL TEXT or GTEXT CURVES XLABEL 2

2-D Plotting Specify x-data and/or y-data Specify color, line style and marker symbol (Default values used if not specified) Syntax: Plotting a single line: plot(xdata, ydata, 'color_linestyle_marker') Plotting multiple lines: plot(x1, y1, 'clm1', x2, y2, 'clm2',...) 3

2-D Plotting simplest example Create a Blue Sin Wave x = 0:.1:2*pi; y = sin(x); plot(y) If x axis isn t specified, MATLAB uses consecutive numbers 4

2-D Plotting with x axis: Create a Blue Sine Wave with appropriate x-axis x=linspace(0,2*pi,50); y = sin(x); plot(x,y) Note values on x axis: 5

2-D Plotting multiple lines Create a blue sine wave and green cosine x=linspace(0,2*pi,50); y = sin(x); xx=linspace(0,4*pi,20); yy=cos(xx); plot(x,y,xx,yy) Notes: broken green line due to small number of points x and y are same length xx and yy are same length x and xx can be different 6

Two more ways to draw multiple lines The same syntax as we showed before can be applied when x and y are matrices: First case: x is a vector and y is a matrix Example: we want to plot both sin and cos over the same interval - x=linspace(0,2*pi,50); y = [sin(x)', cos(x)']; plot(x,y) Note: All Columns of y are plotted vs. same x 7

Multiple lines Second case: x is a matrix and y is a matrix Example: we want to plot both sin and cos over the different intervals - x=linspace(0,2*pi,50) ; x=[x,x+2*pi]; y = [sin(x(:,1)),cos(x(:,2))]; plot(x,y) Notes: Each Column of y is plotted vs. the corresponding column of x 8

Adding Additional Plots to a Figure HOLD ON holds the current plot HOLD OFF releases hold on current plot HOLD toggles the hold state x = 0:.1:2*pi; 0:.1:2*pi; y = sin(x); sin(x); plot(x,y) plot(x,y) grid grid on on hold hold on on plot(x,exp(-x),'r:*') 9

Adding a Grid GRID ON creates a grid on the current figure GRID OFF turns off the grid from the current figure GRID toggles the grid state grid on 10

Graph Annotation title('2-d title('2-d Plots') Plots') xlabel('time') xlabel('time') ylabel('sin(t)') ylabel('sin(t)') text(pi/3,sin(pi/3),...... '<--Sin(\pi/3)') '<--Sin(\pi/3)') legend('sine legend('sine Wave', Wave',...... 'Decaying 'Decaying Exponential') Exponential') 11

Some useful symbols: \alpha \beta other Greek letters \infty \div \aleph \neq \copyright 12

Line styles, markers and colors Various Various line line types, types, plot plot symbols symbols and and colors colors may may be be obtained obtained with with PLOT(X,Y,S) PLOT(X,Y,S) where where S S is is a a character character string string made made from from one one element element from from any any or or all all the the following following 3 3 columns: columns: b b blue blue.. point point - - solid solid g g green green o o circle circle : : dotted dotted r r red red x x x-mark x-mark -. -. dashdot dashdot c c cyan cyan + + plus plus -- -- dashed dashed m m magenta magenta * * star star y y yellow yellow s s square square k k black black d d diamond diamond v v triangle triangle (down) (down) ^ ^ triangle triangle (up) (up) < < triangle triangle (left) (left) > > triangle triangle (right) (right) p p pentagram pentagram h h hexagram hexagram 13

2-D Plotting with various lines etc. x = linspace(0,2*pi,50); y=sin(x); y=sin(x); z=cos(x); z=cos(x); plot(x,y,'b:p',x,z,'c-',x,1.2*z,'m+') 14

The axis command Controls many aspects of the figure: axis([xmin xmax ymin ymax]) V=axis axis auto axis manual axis tight axis fill axis ij axis xy axis equal axis image axis square axis normal axis off axis on Set axis limits on the current plot Return a row vector containing the current axis limits Return axis scaling to automatic defaults Freeze axis scaling so that if hold is on, subsequent plots use the same limits Set the axis limits to the range of the plotted data Set limits and aspect ration to fill the allotted space Matrix mode: vertical axis increases from top to bottom Cartesian mode: vertical axis increases from bottom to top Set aspect ration so that equal tick mark increments on each axis are equal in size Set axis limits appropriate for displaying an image Make the axis box square in size Restore the current axis box to full size Turn off all axis labeling, tick marks and background Turn on all axis labeling, tick marks and background 15

Lets experiment with axis: % plotting plotting a circle: circle: x = linspace(0,2*pi,100); y=sin(x); y=sin(x); z=cos(x); z=cos(x); plot(y,z) plot(y,z) OUCH! How can axis help us? Square Equal 16

Squaring the axis axis square 17

Making the aspect ratio uniform: axis equal 18

Multiple figures: Multiple figure windows can be opened figure command opens a new one To make a window active or current: Click on it with the mouse Or type figure(n) To close a figure window Click on the x in the corner Or type close(n) close all closes all figure windows clf erases the contents of the figure 19

Subplots SUBPLOT- display multiple axes in the same figure window subplot(#rows, #cols, index) subplot(2,2,1); plot(1:10) subplot(2,2,2) x = 0:.1:2*pi; plot(x,sin(x)) subplot(2,2,3) x = 0:.1:2*pi; plot(x,exp(-x), r ) subplot(2,2,4) plot(peaks) 20

2 separate y axes: plotyy x=linspace(-2*pi,2*pi,100); x=linspace(-2*pi,2*pi,100); y=sin(x); y=sin(x); z=3*cos(x); z=3*cos(x); subplot(2,1,1) subplot(2,1,1) plot plot (x,y,x,z) (x,y,x,z) title('the title('the old old way') way') subplot(2,1,2) subplot(2,1,2) plotyy(x,y,x,z) plotyy(x,y,x,z) title('the title('the new new way') way') 21

Easy plotting ezplot, ezpolar Interpret a symbolic vector fstr='sin(x)/x'; ezplot(fstr,[-15,15]) title(fstr) 22

Advanced 2D Graphics 23

Area plots Simply using area with a vector argument creates a plot with filled areas between the line and the x axis: a=randn(1,20); area(a) hold hold on on plot(a,'*r') 24

Area plots Area plots can be stacked: y = [5 [5 1 2; 2; 8 3 7; 7; 9 6 8; 8; 5 5 5; 5; 4 2 3]; 3]; area(y) area(y) 25

Filled polygons The fill function creates filled polygons: fill(x,y, c ) fills a polygon defined by two column vectors each x(i),y(i) pair defines a vertex When x and y are matrices of the same dimension, separate columns define separate polygons t=(1:2:15)'*pi/8; x=cos(t); x=cos(t); y=sin(t); y=sin(t); fill(x,y,'r') fill(x,y,'r') axis axis square square text(0,0,'stop'); 26

Pie charts pie(a,b) creates a pie chart: a is a vector of values b is an optional logical vector describing the slices to be pulled out a=[.5 a=[.5 1 1.6 1.6 1.2 1.2.8.8 2.1]; 2.1]; pie(a,a==max(a)) pie(a,a==max(a)) 27

Pie charts 3D rendering pie3 takes the same arguments, but renders in 3 dimensions a=[1 a=[1 2 3 4]; 4]; pie3(a, pie3(a, [0 [0 1 0 0 ],{'north', ],{'north', 'south','east','west'}) 28

Bar plots Bar plots can be created in grouped or stacked form, in 2 and 3 dimensions Simple bar plots: x=-2.9:.2:2.9; x=-2.9:.2:2.9; y=exp(-x.^2); y=exp(-x.^2); bar(x,y) bar(x,y) bar3(x,y) bar3(x,y) %3D %3D 29

Horizontal bar plots x=-2.9:.2:2.9; x=-2.9:.2:2.9; y=exp(-x.^2); y=exp(-x.^2); barh(x,y) barh(x,y) bar3h(x,y) bar3h(x,y) %3D %3D 30

Grouped / stacked bar plots: x=-2.9:.2:2.9; x=-2.9:.2:2.9; y1=exp(-x.^2); y1=exp(-x.^2); y2=exp(-x.^4); y2=exp(-x.^4); bar(x,[y1',y2'],'grouped') bar(x,[y1',y2'],'stacked') 31

3-D bar plots: If not grouped or stacked then the default is a 3-D plot: bar3(abs(peaks(5))) bar3(abs(peaks(5)),'stacked') Note: peaks is a sample function of two variables the command peaks(5) gives a 5x5 matrix. 32

Histograms Histograms illustrate the distribution of values in a vector hist ( y ) - uses 10 bins hist ( y, n ) - uses n bins hist ( y, x ) - uses the bins whose center values are specified in the vector x y=randn(5000,1); y=randn(5000,1); hist(y,20) hist(y,20) hist(y,-2.9:0.2:2.9) 33

Stem plots Stem plots are convenient for sampled data stem(x,y, linespecs ) 4 3 a=randn(30,1); a=randn(30,1); stem(a,':') stem(a,':') x x = 0:25; 0:25; y y = [exp(-0.01*x).*cos(x);exp(.05*x).*cos(x)]'; h = stem(x,y); stem(x,y); set(h(1),'markerfacecolor','blue') set(h(2),'markerfacecolor','red','marker','square' ) 4) 3 2 1 0-1 2 1 0-1 -2-2 -3-3 0 5 10 15 20 25 30-4 0 5 10 15 20 25 34

Stair plots Staircase plots draw ZOH (Zero Order Hold) of sampled data Format is stairs(x,y, linespec ) as in the plot command Multiple lines are not allowed a=randn(30,1); a=randn(30,1); stairs(a,'--') stairs(a,'--') 35

Error bars errorbar plots a line together with error bars Format is errorbar(x,y,l,u) nonsymmetric error bars errorbar(x,y,e) symmetric error bars Multiple lines are allowed, using multiple columns for each argument x=linspace(0,2,21); y=erf(x); y=erf(x); e=rand(size(x))/10; errorbar(x,y,e) errorbar(x,y,e) 36

Error bars cont. To plot a bar graph with error bars: Plot bars Hold Use errorbar marking dots only x=linspace(0,2,21); y=erf(x); y=erf(x); e=rand(size(x))/10; bar(x,y, r ); bar(x,y, r ); hold hold on on errorbar(x,y,e,. ) 37

Polar plots polar does simple plotting in polar coordinates Format: polar(ang,r, linespec ) 90 120 0.5 0.4 60 ang=linspace(0,2*pi); ang=linspace(0,2*pi); r=sin(2*ang).*cos(2*ang); r=sin(2*ang).*cos(2*ang); polar(ang,r) polar(ang,r) 0.3 150 30 0.2 0.1 180 0 210 330 240 270 300 38

Plotting complex data Complex numbers can be plotted as vectors compass(z) as arrows emanating from the origin feather(z) arrows on the x axis 150 120 90 5 2.5 60 30 180 0 210 330 z=eig(randn(20)); z=eig(randn(20)); subplot(2,1,1) subplot(2,1,1) compass(z) compass(z) subplot(2,1,2) subplot(2,1,2) feather(z) feather(z) 4 3 240 270 300 2 1 0-1 -2-3 -4-5 0 5 10 15 20 25 39

Plotting angle histograms rose is similar to hist only the histogram is polar and the range is 0 to 2*pi 90 80 120 60 Default is 20 bins 60 v=randn(1000,1)*pi; v=randn(1000,1)*pi; rose(v) rose(v) 150 40 20 30 180 0 210 330 240 270 300 40

Scatter plots: scatter plots circles at data points, where the circle size and color can be specified The areas of each circle is in points^2, where points are the units used to measure font sizes, etc. 1 x=rand(40,1); y=rand(40,1); area=70*(x+y.^2); color=(x.^2+y); scatter(x,y,area,color,'filled') box box on on 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 41

Additional plot-related commands xlim ylim zlim daspect pbaspect text quiver fplot pareto plotmatrix ribbon X-axis limits Y-axis limits Z-axis limits Set and get aspect ratio Set and get plot box aspect ratio Place text on plot Quiver or velocity plot Plot a function Pareto chart Scatter plot matrix Linear plot with 2-D lines as ribbons 42