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; % [ …… ] >> 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)); >> hold Current plot held >> plot(x,y2,'color','k','lineStyle','-.')
>> y2= 1./(1+exp(-x)); >> hold Current plot held >> plot(x,y2,'color','k','lineStyle','-.') Property-name, property value pair
>> y2= 1./(1+exp(-x)); >> hold Current plot held >> plot(x,y2,'color','k','lineStyle','-.') Property-name, property value pair
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
Alternative format: >> x=[ ]; >> y=[ ]; >> p2=plot(x,y,'or:’);
Alternative format: >> x=[ ]; >> y=[ ]; >> p2=plot(x,y,'or:’); Marker Color LineStyle There are more alternative formats Use MATLAB help. Consult the property inspector.
Specialized 2D plots Bars Histograpms Stair Stem Scatter Error bars Area Pie charts contour
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 = A handle
Objects The plot The axes The figure ? Handles p 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: DisplayName: '' XDataMode: 'manual' XDataSource: '' YDataSource: '' ZDataSource: ''
>> p_parent=get(p,'parent') p_parent = handle
property name >> p_parent=get(p,'parent') p_parent =
property value >> p_parent=get(p,'parent') p_parent =
>> p_parent=get(p,'parent') p_parent = p_parent is a handle. Of what? >> get(p_parent,'type') ans = axes
>> get(p_parent,'children') ??? ans = >> p ans =
>> set(p_parent,'xcolor','b'); >> set(p_parent,'ycolor','b'); What about the title? >> title=get(p_parent,'title') title = >> 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 = >> 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 = >> blue=lines(1) blue = >> set(blue,'lineWidth',4)
Alternatively: >> findobj('color','b') ans =
Who is the title’s parent? Who are the title’s children? Who is the axes` parent? Who is the figure’ parent?
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.
Graph Objects Phylogeny parent
Graph 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 = [ ]; >> b = a; >> b(3)=9 b = >> a a = >> 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 zooming ylim([0 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 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)}
A little more elaborated example: The most useful string modifiers are: \it changes text to italics \rmchanges 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
>>x=[10,20,30]; >>y=[ ]; >>bar(x,y) >>title('Vertical bar graph');
>>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');
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)
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')
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)]
>> 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=[ ] >> outline=x>5; % = [ ] >> pie(x,outline) >> title('Pie chart')