SGN Introduction to Matlab

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

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

PROGRAMMING WITH MATLAB WEEK 6

Introduction to MATLAB

Introduction to MATLAB

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

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

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

Outline. Case Study. Colormaps. Announcements. Syllabus Case Study Task 1: Animation. Each frame in animation contains. field. map.

Name: Math Analytic Geometry and Calculus III - Spring Matlab Project - due on Wednesday, March 30

Chapter 11. Above: Principal contraction rates calculated from GPS velocities. Visualized using MATLAB.

Introduction to Programming in MATLAB

3D plot of a surface in Matlab

1 >> Lecture 4 2 >> 3 >> -- Graphics 4 >> Zheng-Liang Lu 184 / 243

Mechanical Engineering Department Second Year (2015)

EXERCISES Introduction to MATLAB: Graphics

Introduction to Matlab

MATH 2221A Mathematics Laboratory II

AMS 27L LAB #2 Winter 2009

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

GRAPHICS AND VISUALISATION WITH MATLAB

Scientific Functions Complex Numbers

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

Graphics Example a final product:

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

CSE 123. Plots in MATLAB

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

EOSC 473/573 Matlab Tutorial R. Pawlowicz with changes by M. Halverson

Plotting - Practice session

INTERNATIONAL EDITION. MATLAB for Engineers. Third Edition. Holly Moore

Introduction to Matlab for Engineers

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

NENS 230 Assignment 4: Data Visualization

DATA PLOTTING WITH MATLAB

8.1 Simple Color Specifications

Master Class: Large Scale Data Analysis and Visualization Daniel Armyr Application Engineer

Matlab Concise Notes

Graphics and plotting techniques

More on Plots. Dmitry Adamskiy 30 Nov 2011

Data Analysis in Geophysics ESCI Class 17. Bob Smalley. More Matlab.

Lab 6: Graphical Methods

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

Computing Fundamentals Plotting

QUICK INTRODUCTION TO MATLAB PART I

Introduction to MATLAB: Graphics

FF505/FY505 Computational Science. MATLAB Graphics. Marco Chiarandini

Dr. Iyad Jafar. Adapted from the publisher slides

TOPIC 6 Computer application for drawing 2D Graph

MATLAB 7 3-D Visualization

A MATLAB Exercise Book. Ludmila I. Kuncheva and Cameron C. Gray

By Edward Grossman Edited and Updated by Mark Turner

Plotting using Matlab. Vytautas Astromskas

MAT 343 Laboratory 4 Plotting and computer animation in MATLAB

For each question, indicate whether the statement is true or false by circling T or F, respectively.

interpolation, color, & light Outline HW I Announcements HW II--due today, 5PM HW III on the web later today

APPM 2460 PLOTTING IN MATLAB

Multimedia Signals and Systems Virtual Reality and VRML

Basic MATLAB Intro III

Volume Rendering. Computer Animation and Visualisation Lecture 9. Taku Komura. Institute for Perception, Action & Behaviour School of Informatics

Introduction to Matlab. WIAA Technical Workshop #2 10/20/2015

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Lecture 7. MATLAB and Numerical Analysis (4)

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

Lecture 6: Plotting in MATLAB

fc-graphics4mesh Matlab toolbox, User s Guide

Spring 2010 Instructor: Michele Merler.

Introduction to MATLAB LAB 1

MATLAB basic guide to create 2D and 3D Plots. Part I Introduction

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

MATLAB 7 3-D Visualization

MATLAB Functions and Graphics

Lecture 3 for Math 398 Section 952: Graphics in Matlab

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

Section 4.4: Parabolas

Previously... contour or image rendering in 2D

Warm-Up Exercises. Find the x-intercept and y-intercept 1. 3x 5y = 15 ANSWER 5; y = 2x + 7 ANSWER ; 7

Simulation and visualization of simple leapfrog advection scheme. ATMO 558 Term Project Koichi Sakaguchi

VISUALIZING THE 3D POLAR POWER PATTERNS AND EXCITATIONS OF PLANAR ARRAYS WITH MATLAB

AMath 483/583 Lecture 28 June 1, Notes: Notes: Python scripting for Fortran codes. Python scripting for Fortran codes.

The misc3d Package. October 22, 2007

Graphics for VEs. Ruth Aylett

Introduction to Simulink

Matlab Notes for Calculus 3. Lia Vas

Computer Science 426 Midterm 3/11/04, 1:30PM-2:50PM

8 Piecewise Polynomial Interpolation

Introduction to MATLAB

Complex Dynamic Systems

Bibliographic References

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

MATLAB. The Language of Technical Computing. MATLAB Graphics Reference Version 5.1. Computation. Visualization. Programming

Matlab Plots in Microsoft Word

Direction Fields; Euler s Method

MAT 275 Laboratory 3 Numerical Solutions by Euler and Improved Euler Methods (scalar equations)

Homework #2. Hidden Surfaces, Projections, Shading and Texture, Ray Tracing, and Parametric Curves

MATH36032 Problem Solving by Computer. Publishable Quality Graphics

Topic 2b Building Geometries into Data Arrays

m-files Matlab offers this feature through m-files, which are ascii text files containing a set of Matlab commands.

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

Graphics for VEs. Ruth Aylett

FDP on Electronic Design Tools - Computing with MATLAB 13/12/2017. A hands-on training session on. Computing with MATLAB

Transcription:

SGN-84007 Introduction to Matlab Lecture 4: Data Visualization Heikki Huttunen Alessandro Foi October 10, 2016

Outline Basics: figure, axes, handles, properties; Plotting univariate and multivariate data; Shading, Lighting, Alpha-blending, Transparency; Continuous drawing, Animations.

Handles and Properties Any figure as well as any of the figure s contents are univocally identified by numerical handles: figure_handle=figure; axis_handle=subplot(2,1,2); plot_handle=plot(randn(1e5,1),randn(1e5,1),. ); Handles allow to inspect and modify any property of figures, axes, and various graphical entities.

Handles and Properties Handles of the current figure and axis are given by the commands: >> gcf ans = 1 >> gca ans = 1.731507568359375e+002 The figure handle typically coincides with the figure number shown in the title of the figure window.

Handles and Properties Available properties and their current values are accessed using the get command: >> get(axis_handle) ActivePositionProperty = position ALim = [0 1] ALimMode = auto AmbientLightColor = [1 1 1] Box = on CameraPosition = [-2.5 0 17.3205] CameraPositionMode = auto CameraTarget = [-2.5 0 0] CameraTargetMode = auto CameraUpVector = [0 1 0] CameraUpVectorMode = auto CameraViewAngle = [6.60861] CameraViewAngleMode = auto CLim = [0 1] CLimMode = auto Color = [1 1 1]...

Handles and Properties Available properties and their current values are accessed using the get command: >> get(plot_handle) DisplayName: Annotation: [1x1 hg.annotation] Color: [0 0 1] LineStyle: none LineWidth: 0.500000000000000 Marker:. MarkerSize: 6 MarkerEdgeColor: auto MarkerFaceColor: none XData: [1x100000 double] YData: [1x100000 double] ZData: [1x0 double] BeingDeleted: off ButtonDownFcn: [] Children: [0x1 double] Clipping: on...

Handles and Properties Values of properties can be modified using the set command. >> set(plot_handle, MarkerEdgeColor,[1 0 0], MarkerSize,2)

Plotting data Matlab boasts a plethora of functions for plotting data: plot, plot3, mesh, surf, surfl, scatter, scatter3, stem, bar, bar3, imagesc, patch, etc. They can be roughly divided in two classes: plots of univariate data (e.g., 1-D plots in 2-D, or plots of curves in 2-D or 3-D) each data sample is linked with previous and next sample; data typically represented as a vector. plots of bivariate data (e.g., meshes, surfaces). each data sample is linked with its neighbours ; data is typically represented as a matrix. Axes are always 3-D, even when everything is 2-D.

Plotting data subplot(2,4,1), plot(randn(1,5),randn(1,5)),axis tight subplot(2,4,2), plot3(randn(1,8),randn(1,8),randn(1,8)),box on subplot(2,4,3), scatter(rand(1,3),rand(1,3)),axis equal, grid on subplot(2,4,4), bar(rand(1,3),rand(1,3)),axis square subplot(2,4,5), bar3(rand(4,3)) subplot(2,4,6), surf(([ -10:10].^2*[ -10:10])),axis tight subplot(2,4,7), mesh(([ -10:10].^2*[ -10:10])),axis tight subplot(2,4,8), patch(rand(4,2),rand(4,2),rand(4,2),rand(1,2,3)),view(30,20)

plot3: curve in 3-D >> help plot3 PLOT3 Plot lines and points in 3-D space. PLOT3(x,y,z), where x, y and z are three vectors of the same length, plots a line in 3-space through the points whose coordinates are the elements of x, y and z. PLOT3(X,Y,Z), where X, Y and Z are three matrices of the same size, plots several lines obtained from the columns of X, Y and Z. >> plot_handle=plot3(sin(linspace(0,4*pi,200)),cos(linspace(0,4*pi,200)),cos(linspace (0,6*pi,200))) >> set(plot_handle, LineWidth,3), box on, camproj( persp ),

Using plot3 for scatterplots in 3-D >> set(plot_handle, LineStyle, none, Marker, o, MarkerFaceColor,[0 1 0], MarkerEdgeColor,[1 0 1], MarkerSize,5, LineWidth,1.5).

surf: default [X, Y]=meshgrid(linspace(-1,1,100),linspace(-1,1,100)); Z = 1 - X.^2 - Y.^2; % parabolic surface surf_handle=surf(x,y,z); % draw surface % create X and Y coordinates

Enable perspective rendering >> camproj( persp ) % switch from orthographic to perspective projection

surf: improve rendering >> shading interp % changes color shading >> lighting phong % better rendering quality

Prepare figure for print output >> set(gca, FontSize,16) % prepare for print output (larger fonts) >> set(gcf, Color,[1 1 1]) % prepare for print output (white background)

Surfaces from irregularly sampled data X=randn(2e2,1); Y=randn(2e2,1); % random sampling points Z = 1 - X.^2 - Y.^2; % parabolic surface sampled at (X,Y) plot_handle=plot3(x,y,z); % draw scatterplot set(plot_handle, Marker,., LineStyle, none, Color,[0 0 0]) set(gca, xlim,[-1 1], ylim,[-1 1], zlim,[-1 1]) % set limits of axes camproj( persp ) % switch from orthographic to perspective projection set(gca, FontSize,16) % prepare for print output (larger fonts) set(gcf, Color,[1 1 1]) % prepare for print output (white background)

TriScatteredInterp: interpolation the easy way >> help TriScatteredInterp TriScatteredInterp Scattered data interpolant TriScatteredInterp is used to perform interpolation on a scattered dataset that resides in 2D/3D space. A scattered data set defined by locations X and corresponding values V can be interpolated using a Delaunay triangulation of X. This produces a surface of the form V = F(X). The surface can be evaluated at any query location QX, using QV = F(QX), where QX lies within the convex hull of X. The interpolant F always goes through the data points specified by the sample. F = TriScatteredInterp(X, V) Creates an interpolant that fits a surface of the form V = F(X) to the scattered data in (X, V). X is a matrix of size mpts -by-ndim, where mpts is the number of points and ndim is the dimension of the space where the points reside, ndim >= 2. V is a column vector that defines the values at X, where the length of V equals mpts. F = TriScatteredInterp(X, Y, V) and F = TriScatteredInterp(X, Y, Z, V) allow the data point locations to be specified in alternative column vector format when working in 2D and 3D.

Surfaces from irregularly sampled data F=TriScatteredInterp(X(:),Y(:),Z(:)); % create interpolant function [Xgrid, Ygrid]=meshgrid(linspace(-1,1,100),linspace(-1,1,100)); and Y coordinates hold on % holds the current plot surf_handle=surf(xgrid,ygrid,f(xgrid,ygrid)); % draw surface shading interp % changes color shading quality % create grids of X

Draw coarse mesh over finely sampled surface hold on for y=linspace(-1,1,10) x=linspace(-1,1,100); plot_handle=plot3(x,y(ones(1,100)),f(x,y(ones(1,100)))); set(plot_handle, color,[0 0 0]) end

Draw coarse mesh over finely sampled surface for x=linspace(-1,1,10) y=linspace(-1,1,100); plot_handle=plot3(x(ones(1,100)),y,f(x(ones(1,100)),y)); set(plot_handle, color,[0 0 0]) end

surf: uniform surfaces are ambiguous [X, Y]=meshgrid(linspace(-1,1,100),linspace(-1,1,100)); % create X and Y coordinates Z = 1 - X.^2 - Y.^2; % parabolic surface surf_handle=surf(x,y,z); % draw surface set(surf_handle, FaceColor,[1 0.0 0.8], EdgeColor, none ) % set single color and no mesh set(gca, FontSize,16, Color,[1 1 1]) set(gcf, Color,[1 1 1]) camproj( persp ) % switch from orthographic to perspective projection

surf: disambiguation by coarse mesh F=TriScatteredInterp(X(:),Y(:),Z(:)); % create interpolant function % draw coarse mesh hold on x=linspace(-1,1,100); for y=linspace(-1,1,10) plot_handle=plot3(x,y(ones(1,100)),f(x,y(ones(1,100)))); set(plot_handle, color,[0 0 0]) end

surf: disambiguation by coarse mesh % and now along the other direction: y = linspace(-1,1,100); for x = linspace(-1,1,10) plot_handle = plot3( x(ones(1,100)), y, F(x(ones(1,100)),y) ); set(plot_handle, color,[0 0 0]) end

surf: disambiguation by lighting lighting phong % better rendering quality light % adds a light to the scene % camlight % adds a light to the scene

surf: disambiguation by transparency set(surf_handle, FaceAlpha,0.6) % alpha blending % small alpha makes surfaces and patches very transparent % alpha = 1 makes them fully opaque

Alpha blending is only for patches or surfaces % HOW TO DRAW ALPHA -BLENDING PLOTS?? % draw coarse mesh using surf instead of plot to exploit alpha -blending x=linspace(-1,1,100); for y=linspace(-1,1,10) my_handle=surf([x;x],y(ones(2,100)),f([x;x],y(ones(2,100)))); set(my_handle, EdgeColor,[0 0 0], FaceColor, none, EdgeAlpha,0.3) end... and similar for the other direction

Seeing through a transparent patch y=linspace(-1,1,100); for x=linspace(-1,1,10) my_handle=surf(x(ones(2,100)),[y;y],f(x(ones(2,100)),[y;y])); set(my_handle, EdgeColor,[0 0 0], FaceColor, none, EdgeAlpha,0.3) end patch_handle=patch([-1-1 1 1-1],[-1 1 1-1 -1],[-1-1 1 1-1],[0 1 0]); set(patch_handle, facealpha,0.4)

Opaque objects are easier to understand [X, Y]=meshgrid(linspace(-1,1,100),linspace(-1,1,100)); % create X and Y coordinates Z = 1 - X.^2 - Y.^2; % parabolic surface surf_handle=surf(x,y,z); % draw surface set(surf_handle, FaceColor,[1 0.0 0.8], EdgeColor, none ) set(gca, FontSize,16, Color,[1 1 1]), set(gcf, Color,[1 1 1]) camproj( persp ) % switch from orthographic to perspective projection lighting phong % better rendering quality light % adds a light to the scene

Using alpha-blending to battle crowding and to highlight trends in data: Noise

Using alpha-blending to battle crowding and to highlight trends in data: Noise % Noise close all y=randn(1,1e4); % random noise figure subplot(1,2,1) plot(y) % plot noise axis tight % set margins of axis subplot(1,2,2) surf_handle=surf([1:size(y,2);1:size(y,2)],[y;y],zeros(2,size(y,2))); as surface set(surf_handle, EdgeAlpha,0.05) % enable alpha blending view([0 90]) % set view to x-y plane axis tight grid off % disable grid box on % enable box (frame around axis) % plot curve

Using alpha-blending to battle crowding and to highlight trends in data: Brownian motion

Using alpha-blending to battle crowding and to highlight trends in data: Brownian motion % Brownian motion close all delta_x=randn(1,5e4); % random shifts in x delta_y=randn(1,5e4); % random shifts in y x=cumsum(delta_x); % path in x y=cumsum(delta_y); % path in y figure subplot(1,2,1) plot(x,y) % plot path axis equal tight % set margin and scale of axis subplot(1,2,2) surf_handle=surf([x;x],[y;y],zeros(2,size(x,2))); % plot path curve as surface set(surf_handle, EdgeAlpha,0.05) % enable alpha blending view([0 90]) % set view to x-y plane axis equal tight % set margin and scale of axis grid off % disable grid box on % enable box (frame around axis)

Continuous drawings and Animations Continuous drawing is usually managed through a timed loop: ploteverynseconds =0.1; % how often to draw? timeold=now; while 1 % HERE GO THE INSTRUCTIONS OF MAIN ALGORITHM if (now -timeold)*(60*60*24)>ploteverynseconds % begin of plotting subroutine % HERE GO THE INSTRUCTIONS FOR PLOTTING FIGURE drawnow pause (0.0001); %% issue drawnow to force drawing %% issue a pause to allow drawing end end timeold=now; %% reset timer % end of plotting subroutine

Continuous drawing example: Gravitation

Vector Fields load vectorfield.mat quiver(x, Y, DX, DY); 2 1.5 1 0.5 0-0.5-1 -1.5 Original vector field data -2-2 -1.5-1 -0.5 0 0.5 1 1.5 2 Vector fields can be plotted using the quiver plotting command. The function takes four arguments: X: The x coordinates of drawn vectors Y: The y coordinates of drawn vectors DX: The (scaled) length of vector along x axis DY: The (scaled) length of vector along y axis Note that the vectors are scaled in length to look better.

Vector Fields Another example, where the vector field is smoothed by fitting a second order model to it. % Fit a quadratic model to the field H = [X(:).^2,Y(:).^2,X(:).*Y(:),X(:),Y(:), X(:).^0]; Z = [DX(:),DY(:)]; c = H \ Z; 2 1.5 1 0.5 Smoothed vector field data % Now, a smoothed field is given by H*c: Z_smooth = H*c; % Reshape to original size and quiver plot DX_smooth = Z_smooth(:,1); DY_smooth = Z_smooth(:,2); DX_smooth = reshape(dx_smooth, size(dx)); DY_smooth = reshape(dy_smooth, size(dy)); quiver(x, Y, DX_smooth, DY_smooth); 0-0.5-1 -1.5-2 -2-1.5-1 -0.5 0 0.5 1 1.5 2

Exporting Plots quiver(x, Y, DX, DY); % Save in Matlab format savefig( quiver_plot.fig ); % Save fig 1 as pdf print -f1 -dpdf quiver_plot.pdf Plots can be easily exported from the figure menu. Alternatively the figure can be stored using savefig or print: savefig: Save to Matlab format *.fig file (allows editing later). print: Save to *.png, *.jpg, *.pdf formats. Note, that pdf is by default stored at A4 size. Most reliable way to prevent this is to manually crop the PDF afterwards (although Figure Properties can often reach the same goal, too).

4D Data Even higher dimensional data may be visualized. slice shows a plot for functions of 3 variables. [x,y,z] = meshgrid(-2:.2:2, -2:.25:2, -2:.16:2); % below is the function v(x,y,z) we wish to plot. v = x.* exp(-x.^2 - y.^2 - z.^2); % Slice takes the data and list of planes shown % (in x, y and z dimensions; e.g, [-2, 0.2] below % define planes z = -2 and z = -0.2). slice(x,y,z,v,[-1.2, 0.8, 2], 2, [-2, -0.2]) % Add also an indicator of the values colorbar();