70
Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles Invoking and manipulating graphics objects

Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Embed Size (px)

Citation preview

Page 1: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Motivation

The interactive tools: Drawing commands Interactive figure manipulation

The programmer point of view: Introduction to objects and their handles Invoking and manipulating graphics objects

Page 2: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

The interactive tools

>> x = -5:0.05:5; % [-5 -4.95 …….4.95 5]>> y = sin(x.^2);>> plot(x,y)

Page 3: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles
Page 4: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles
Page 5: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

none

Page 6: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles
Page 7: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

The axes is contained within the figure.

A figure may have more than one axes.

Page 8: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles
Page 9: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles
Page 10: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

The plot is contained within the axes.

An axes may have more than one plot.

Page 11: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> y2= 1./(1+exp(-x));>> holdCurrent plot held>> plot(x,y2,'color','k','lineStyle','-.')

Page 12: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> y2= 1./(1+exp(-x));>> holdCurrent plot held>> plot(x,y2,'color','k','lineStyle','-.')

Property-name, property value pair

Page 13: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> y2= 1./(1+exp(-x));>> holdCurrent plot held>> plot(x,y2,'color','k','lineStyle','-.')

Property-name, property value pair

Page 14: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles
Page 15: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

dash-dot line-.

dotted line:

dashed line--

solid line (default)

-

Line StyleSpecifier

six-pointed star (hexagram)h

five-pointed star (pentagram)p

triangle pointing left<

triangle pointing right >

triangle pointing downward v

triangle pointing upward^

diamondd

squares

crossx

point.

asterisk*

circleo

Plus sign+

Marker typeSpecifier

whitew

blackk

yellowy

magentam

cyanc

blueb

greeng

Redr

ColorSpecifier

Page 16: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

0 2 4 6 8 10 122

3

4

5

6

7

8

9

10

Alternative format:>> x=[1 2.3 4 10 3 11.3];>> y=[3.2 5 2 7.4 3 9.4];>> p2=plot(x,y,'or:’);

Page 17: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Alternative format:>> x=[1 2.3 4 10 3 11.3];>> y=[3.2 5 2 7.4 3 9.4];>> p2=plot(x,y,'or:’);

0 2 4 6 8 10 122

3

4

5

6

7

8

9

10

Marker Color LineStyle

There are more alternative formatsUse MATLAB help.Consult the property inspector.

Page 18: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Specialized 2D plots

• Bars• Histograpms• Stair• Stem • Scatter• Error bars• Area• Pie charts• contour

10 20 300

2

4

6

8

10Vertical bar graph

-10 0 10 200

1000

2000

3000

4000histogram with 20 bins

-2 0 20

0.1

0.2

0.3

0.4Stair graph

-2 0 2-10

-5

0

5

10stem

0 0.5 10

0.2

0.4

0.6

0.8

1Scatter plot

0 5 10-50

0

50

100Errorbars

0.2 0.4 0.6 0.8-10

-5

0

5Ploting functions with fplot

x

f(x)

cos(30*x)/x

1 2 3 4 50

5

10

15

20

25Area plot

47%

5%14%

2%

33%

Pie chart

Page 19: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Specialized 3D plots

• plot3

• mesh

• surf

Use MATLAB help and demos.

Page 20: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

The programmer’s point of view:

Graphic Objects

Handles

Properties

Page 21: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Graphic Object(figure, axes, text, line etc.)

handle

Page 22: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Graphic Object(figure, axes, text, line etc.)

handle

properties

Page 23: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Many objects may exist simultaneously

Page 24: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

We use the handles to pick an object and manipulate it.

Page 25: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

An object may have more than one handle

Page 26: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Objects may contain other objects

Page 27: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

• Everything inside a figure window is a graphic object

• The figure window itself is a graphic object

• All plots in MATLAB are made of graphic objects

• All graphic objects have properties that control the way they look and behave

• Graphic object: any element as defined by the set of its properties

that constitute part of a figure

Page 28: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>t = -pi:0.05:pi; >>x = cos(3*t)*1./(1+exp(-t)); >>y = sin(3*t)*1./(1+exp(-t));

>>p = plot(x,y)

p=

158.0055

A handle

Page 29: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Objects The plot The axes The figure?

Handlesp is the plots handle??

Page 30: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>set(p,'color','r')>>set(p,'lineWidth',4)

handle

Page 31: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>set(p,'color','r')>>set(p,'lineWidth',4)

property names

Page 32: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>set(p,'color','r')>>set(p,'lineWidth',4)

How would I know what properties does the plot have?

property values

Page 33: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> get(p) Color: [1 0 0] EraseMode: 'normal' LineStyle: '-' LineWidth: 4 Marker: 'none' MarkerSize: 6 MarkerEdgeColor: 'auto' MarkerFaceColor: 'none' XData: [1x126 double] YData: [1x126 double] ZData: [1x0 double] BeingDeleted: 'off' ButtonDownFcn: [] Children: [0x1 double] Clipping: 'on' CreateFcn: [] DeleteFcn: [] BusyAction: 'queue' HandleVisibility: 'on' HitTest: 'on' Interruptible: 'on' Selected: 'off' SelectionHighlight: 'on' Tag: '' Type: 'line' UIContextMenu: [] UserData: [] Visible: 'on' Parent: 157.0059 DisplayName: '' XDataMode: 'manual' XDataSource: '' YDataSource: '' ZDataSource: ''

Page 34: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>p_parent=get(p,'parent')

p_parent=

157.0059

handle

Page 35: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

property name

>>p_parent=get(p,'parent')

p_parent=

157.0059

Page 36: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

property value

>>p_parent=get(p,'parent')

p_parent=

157.0059

Page 37: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>p_parent=get(p,'parent')

p_parent=

157.0059

p_parent is a handle.

Of what?

>> get(p_parent,'type')

ans =

axes

Page 38: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> get(p_parent,'children')???ans =

158.0063>> pans = 158.0063

Page 39: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>set(p_parent,'xcolor','b'); >>set(p_parent,'ycolor','b');

What about the title?

>>title=get(p_parent,'title')title=

163.0059 >>get(title,'type')

ans= text

Page 40: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>>set(p_parent,'xcolor','b'); >>set(p_parent,'ycolor','b');

What about the title?

>>title=get(p_parent,'title')title=

163.0059 >>get(title,'type')

ans= text>> set(title,'string','Spiral')>> set(title,'color','b')

Page 41: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Lets add another line

Ooooops…

>> x1 = -cos(3*t)*1./(1+exp(-t)); >>y1 = -sin(3*t)*1./(1+exp(-t));

>> plot(x1,y1)

>>lines = get(p_parent,'children')lines=

204.0061 158.0063

>>blue=lines(1)blue=

204.0061>> set(blue,'lineWidth',4)

Page 42: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Alternatively:

>>findobj('color','b')

ans=

204.0061

Page 43: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Who is the title’s parent?

Who are the title’s children?

Who is the axes` parent?

Who is the figure’ parent?

Page 44: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Graph Objects PhylogenyGraph Objects Phylogeny

This hierarchy is based on the interdependency of objects. A line can only be plotted inside an Axes. A figure contains Axes and so on.

Page 45: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Graph Objects PhylogenyGraph Objects Phylogeny

parent

Page 46: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Graph Objects PhylogenyGraph Objects Phylogeny

children

Page 47: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

…in the beginning there is only the root object….

Hello, I am the root object

Although I am the root I

always got a 0 as my handle

I know and control many important things… Type

get(0) to see all my properties

Page 48: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

What is the difference between a handle and “simple variable”?

>>a = [1 2 3 4 5]; >>b = a; >>b(3)=9

b=

1 2 9 4 5

>>a

a=

1 2 3 4 5

>>get(title,'String')

ans=

Spiral

>>ttt = title; >>set(ttt,'string','Red wide spiral')

>>get(title,'String')

ans=

Red wide spiral

Page 49: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

handle

right_handle = handle

Page 50: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles
Page 51: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

The subplot command

•Present simultaneously several pieces of information that when displayed on a single plot may confuse the reader

•Display and image and quantification

•Let’s see an example

Page 52: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

x=linspace(0,3,500);plot(x,1./(x-1).^2+3./(x-2).^2)grid on

%Change zoomingylim([0 50])

0 0.5 1 1.5 2 2.5 30

1

2

3

4

5

6

7

8x 10

5

0 0.5 1 1.5 2 2.5 30

5

10

15

20

25

30

35

40

45

50

Page 53: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

0 1 2 30

1

2

3

4

5

6

7

8x 10

5

0 1 2 30

5

10

15

20

25

30

35

40

45

50

>>x=linspace(0,3,500);>>a(1)=subplot(1,2,1);%1 rows, 2 cols use first axis>>plot(x,1./(x-1).^2+3./(x-2).^2)>>a(2)=subplot(1,2,2 );%1 rows, 2 cols use second axis>>plot(x,1./(x-1).^2+3./(x-2).^2)>>ylim([0 50])>>set(a,'XGrid','on','YGrid','on')

Page 54: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

The general synopsis of subplot is subplot (n,m,p) where• n is the number of rows • m is the number of columns• p identifies the specific subplot. Starting from the top left axes and counting across rows

How to use the subplot command

>>subplot(1,2,2)

Page 55: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

MATLAB has an text interpreter named TEX very similar to LATEX. • format mathematical expressions and Greek letters to display

nicely both in the screen and in printed material

• we use a backslash “\” followed by either a symbol identifier, or a string modifier

• We can limit the extent of string formatting by placing the string inside curly braces {…}

• To create the following formatted string

f()=sin()• We write:

{\itf(\tetha)}=sin{\it(\tetha)}

Page 56: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

A little more elaborated example:

The most useful string modifiers are:

\it changes text to italics

\rm changes text to normal

^ superscript

_ underscript

Page 57: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Bar plots

• bar and barh create vertical and horizontal bar graph respectively

• When you use bar(Y) where Y is a matrix, rows are treated as groups.

• Use bar(x,Y) to center the bars at the places specified by x

Page 58: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

10 20 300

1

2

3

4

5

6

7

8

9

10Vertical bar graph

>>x=[10,20,30];>>y=[5 3 10 2 7 10 3 7 10 10 2 2];>>bar(x,y)>>title('Vertical bar graph');

Page 59: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

10 20 300

5

10

15

20

25

30

>>bar(x,y,'stacked')

Page 60: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Histograms• Use hist(X,[bins]) to create an histogram (one

per column of X)

• By default the count of the data is done in 10 bins

• You can change this by passing a second argument to hist– Scalar => tells the number of bins– Vector => tell the center of each bin

Page 61: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Y(:,1)=randn(10000,1);Y(:,2)=randn(10000,1)+10;hist(Y,20)title('histogram with 20 bins');

-4 -2 0 2 4 6 8 10 12 140

500

1000

1500

2000

2500

3000

3500histogram with 20 bins

Value

Co

un

t

Page 62: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

• As we saw hist plots the count (i.e. the number of elements in each category)

• What if we want to plot the frequencies instead?

• Use [n,x]=hist(X,[bins]) – n will hold the # of elements of each bin– x will hold the center value of each bin

Page 63: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> [n,x]=hist(Y,20)

>> freqN=n./repmat(sum(n),length(n),1)

% freqN=n/10000

>> bar (x,freqN)

-4 -2 0 2 4 6 8 10 12 140

0.05

0.1

0.15

0.2

0.25

0.3

0.35

Value

Fre

qu

en

cy

Page 64: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Stair plots

• Commonly used in digital signal processing and statistical analysis

• When we sample data at a given rate, we have no information about what is going on between two consecutive samples

Page 65: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> x=-2:0.1:2;>> y=normpdf([-2:0.1:2],0,1);>> stairs(x,y)>> title('Stair graph')

-2 -1.5 -1 -0.5 0 0.5 1 1.5 20.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4Stair graph

Page 66: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Error bars

• Error bars can be added to plots with the help of the errorbar function.

• The synopsis of this function is errorbar(x,y,l,u,[formatting string])

• x,y,l,u are all vertors of the same size

• draws a marker on the point specified by the x,y and add bars of the size [ y+u(i) y-l(i)]

Page 67: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

0 1 2 3 4 5 6 7 8 9 10 11-80

-60

-40

-20

0

20

40

60

80

100

120Errorbars

>> x=1:10;>> y=2*x+5;>> noise=rand(1,1)*x.^2;>> errorbar(x,y,noise,'ok:')>> xlim([0 11])>> title('Errorbars')

Page 68: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

Pie charts

• among the less recommend though common

• use a lot of graphics to represent very low data density

• Still, if you want to use them it is easily done

Page 69: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

• pie(x,[outline]):– x is a vector with the value of each data– Outline is a vector of 1s and 0s the same size of

x that states which pie slices should be outlined (1) or held together (0)

Page 70: Motivation The interactive tools: Drawing commands Interactive figure manipulation The programmer point of view: Introduction to objects and their handles

>> x=[10 1 3 0.5 7]>> outline=x>5; % = [1 0 0 0 1]>> pie(x,outline)>> title('Pie chart')

47%

5%

14%

2%

33%

Pie chart