Download presentation
Presentation is loading. Please wait.
Published byJob Dawson Modified over 9 years ago
1
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
2
The interactive tools >> x = -5:0.05:5; % [-5 -4.95 …….4.95 5] >> y = sin(x.^2); >> plot(x,y)
5
none
7
The axes is contained within the figure. A figure may have more than one axes.
10
The plot is contained within the axes. An axes may have more than one plot.
11
>> y2= 1./(1+exp(-x)); >> hold Current plot held >> plot(x,y2,'color','k','lineStyle','-.')
12
>> y2= 1./(1+exp(-x)); >> hold Current plot held >> plot(x,y2,'color','k','lineStyle','-.') Property-name, property value pair
13
>> y2= 1./(1+exp(-x)); >> hold Current plot held >> plot(x,y2,'color','k','lineStyle','-.') Property-name, property value pair
15
dash-dot line -. dotted line : dashed line -- solid line (default) - Line Style Specifier six-pointed star (hexagram)h five-pointed star (pentagram)p triangle pointing left< triangle pointing right> triangle pointing downwardv triangle pointing upward^ diamondd squares crossx point. asterisk* circleo Plus sign+ Marker typeSpecifier whitew blackk yellowy magentam cyanc blueb greeng Redr ColorSpecifier
16
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:’);
17
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:’); Marker Color LineStyle There are more alternative formats Use MATLAB help. Consult the property inspector.
18
Specialized 2D plots Bars Histograpms Stair Stem Scatter Error bars Area Pie charts contour
19
Specialized 3D plots plot3 mesh surf Use MATLAB help and demos.
20
The programmer’s point of view: Graphic Objects Handles Properties
21
Graphic Object (figure, axes, text, line etc.) handle
22
Graphic Object (figure, axes, text, line etc.) handle properties
23
Many objects may exist simultaneously
24
We use the handles to pick an object and manipulate it.
25
An object may have more than one handle
26
Objects may contain other objects
27
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
28
>> 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
29
Objects The plot The axes The figure ? Handles p is the plots handle ?
30
>>set(p,'color','r') >>set(p,'lineWidth',4) handle
31
>>set(p,'color','r') >>set(p,'lineWidth',4) property names
32
>>set(p,'color','r') >>set(p,'lineWidth',4) How would I know what properties does the plot have? property values
33
>> 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: ''
34
>> p_parent=get(p,'parent') p_parent = 157.0059 handle
35
property name >> p_parent=get(p,'parent') p_parent = 157.0059
36
property value >> p_parent=get(p,'parent') p_parent = 157.0059
37
>> p_parent=get(p,'parent') p_parent = 157.0059 p_parent is a handle. Of what? >> get(p_parent,'type') ans = axes
38
>> get(p_parent,'children') ??? ans = 158.0063 >> p ans = 158.0063
39
>> 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
40
>> 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')
41
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)
42
Alternatively: >> findobj('color','b') ans = 204.0061
43
Who is the title’s parent? Who are the title’s children? Who is the axes` parent? Who is the figure’ parent?
44
Graph 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.
45
Graph Objects Phylogeny parent
46
Graph Objects Phylogeny children
47
…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
48
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
49
handle right_handle = handle
51
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
52
x=linspace(0,3,500); plot(x,1./(x-1).^2+3./(x-2).^2) grid on %Change zooming ylim([0 50])
53
>>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')
54
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)
55
MATLAB has an text interpreter named T E X very similar to LAT E X. 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)}
56
A little more elaborated example: The most useful string modifiers are: \it changes text to italics \rmchanges text to normal ^superscript _underscript
57
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
58
>>x=[10,20,30]; >>y=[5 3 10 2 7 10 3 7 10 10 2 2]; >>bar(x,y) >>title('Vertical bar graph');
59
>>bar(x,y,'stacked')
60
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
61
Y(:,1)=randn(10000,1); Y(:,2)=randn(10000,1)+10; hist(Y,20) title('histogram with 20 bins');
62
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
63
>> [n,x]=hist(Y,20) >> freqN=n./repmat(sum(n),length(n),1) % freqN=n/10000 >> bar (x,freqN)
64
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
65
>> x=-2:0.1:2; >> y=normpdf([-2:0.1:2],0,1); >> stairs(x,y) >> title('Stair graph')
66
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)]
67
>> x=1:10; >> y=2*x+5; >> noise=rand(1,1)*x.^2; >> errorbar(x,y,noise,'ok:') >> xlim([0 11]) >> title('Errorbars')
68
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
69
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)
70
>> x=[10 1 3 0.5 7] >> outline=x>5; % = [1 0 0 0 1] >> pie(x,outline) >> title('Pie chart')
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.