motivation the interactive tools: drawing commands interactive figure manipulation the programmer...
TRANSCRIPT
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
The interactive tools
>> x = -5:0.05:5; % [-5 -4.95 …….4.95 5]>> y = sin(x.^2);>> plot(x,y)
none
The axes is contained within the figure.
A figure may have more than one axes.
The plot is contained within the axes.
An axes may have more than one plot.
>> y2= 1./(1+exp(-x));>> holdCurrent plot held>> plot(x,y2,'color','k','lineStyle','-.')
>> y2= 1./(1+exp(-x));>> holdCurrent plot held>> plot(x,y2,'color','k','lineStyle','-.')
Property-name, property value pair
>> y2= 1./(1+exp(-x));>> holdCurrent plot held>> plot(x,y2,'color','k','lineStyle','-.')
Property-name, property value pair
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
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:’);
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.
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
Specialized 3D plots
• plot3
• mesh
• surf
Use MATLAB help and demos.
The programmer’s point of view:
Graphic Objects
Handles
Properties
Graphic Object(figure, axes, text, line etc.)
handle
Graphic Object(figure, axes, text, line etc.)
handle
properties
Many objects may exist simultaneously
We use the handles to pick an object and manipulate it.
An object may have more than one handle
Objects may contain other objects
• 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
>>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
Objects The plot The axes The figure?
Handlesp is the plots handle??
>>set(p,'color','r')>>set(p,'lineWidth',4)
handle
>>set(p,'color','r')>>set(p,'lineWidth',4)
property names
>>set(p,'color','r')>>set(p,'lineWidth',4)
How would I know what properties does the plot have?
property values
>> 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: ''
>>p_parent=get(p,'parent')
p_parent=
157.0059
handle
property name
>>p_parent=get(p,'parent')
p_parent=
157.0059
property value
>>p_parent=get(p,'parent')
p_parent=
157.0059
>>p_parent=get(p,'parent')
p_parent=
157.0059
p_parent is a handle.
Of what?
>> get(p_parent,'type')
ans =
axes
>> get(p_parent,'children')???ans =
158.0063>> pans = 158.0063
>>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(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')
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)
Alternatively:
>>findobj('color','b')
ans=
204.0061
Who is the title’s parent?
Who are the title’s children?
Who is the axes` parent?
Who is the figure’ parent?
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.
Graph Objects PhylogenyGraph Objects Phylogeny
parent
Graph Objects PhylogenyGraph Objects Phylogeny
children
…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
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
handle
right_handle = handle
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
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
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')
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)
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)}
A little more elaborated example:
The most useful string modifiers are:
\it changes text to italics
\rm changes text to normal
^ superscript
_ underscript
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
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');
10 20 300
5
10
15
20
25
30
>>bar(x,y,'stacked')
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
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
• 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
>> [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
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
>> 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
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)]
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')
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
• 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)
>> 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