graphics and plotting techniques · graphics and plotting techniques . 1. introduction . the...

15
Davies: Computer Vision, 5 th edition, online materials Matlab Tutorial 5 1 CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018 Graphics and plotting techniques 1. Introduction The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions. Section 2 describes what can be achieved using the plot and subplot functions, and concentrates particularly on showing how several graphs can be plotted in the same figure space. Section 3 introduces the line function and describes how plots can be labelled and formatted. Section 4 goes on to explain some of the intricacies of Matlab coding, while Section 5 summarises the various plotting commands and options. 2. Plotting In Matlab, plotting 2-D functions is most easily achieved by using the plot function, which has very wide capability. Basically, it is written as plot(x, y), where y is taken to be a function of x. Figure 1 shows a simple example given by: >> x = 0:0.1:8; >> y = 8*exp(-1.1*x); >> figure >> plot(x, y) Figure 1 Alternatively, if x and y are defined as independent vectors, they must have the same length: >> x = 0:1:8; >> y = [ 2 3 7 2 6 3 5 4 1 ]; >> figure >> plot(x, y) The result is shown in Figure 2.

Upload: others

Post on 08-Jan-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 1

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

Graphics and plotting techniques 1. Introduction The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions. Section 2 describes what can be achieved using the plot and subplot functions, and concentrates particularly on showing how several graphs can be plotted in the same figure space. Section 3 introduces the line function and describes how plots can be labelled and formatted. Section 4 goes on to explain some of the intricacies of Matlab coding, while Section 5 summarises the various plotting commands and options. 2. Plotting In Matlab, plotting 2-D functions is most easily achieved by using the plot function, which has very wide capability. Basically, it is written as plot(x, y), where y is taken to be a function of x. Figure 1 shows a simple example given by: >> x = 0:0.1:8; >> y = 8*exp(-1.1*x); >> figure >> plot(x, y)

Figure 1 Alternatively, if x and y are defined as independent vectors, they must have the same length: >> x = 0:1:8; >> y = [ 2 3 7 2 6 3 5 4 1 ]; >> figure >> plot(x, y) The result is shown in Figure 2.

Page 2: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 2

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

Figure 2 Complete script for Figure 2. For a detailed explanation of the various functions used in this and other scripts, see Section 3. x = linspace(0,8,9); y = [2 3 7 2 6 3 5 4 1]; figure plot(x,y,'r--','LineWidth',1.2) pbaspect([4 3 1]) axis([0 8 0 8]) set(gca, 'fontsize',14) set(gca, 'XTick',[0 2 4 6 8]) set(gca, 'YTick',[0 2 4 6 8]) set(gca, 'LineWidth',1.0) grid on

In the following example, the total number of data points is 7, and the resulting graph is a 6-line approximation to the cosine curve. >> theta = 0:15:90; >> y = cos(theta*pi/180); >> figure >> plot(theta, y)

Figure 3 In fact, it is usually more transparent to use the linspace operator as the basis for plotting operations: this has arguments representing the start x1 and end xn values and

Page 3: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 3

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

the total number of points n created within the range x1 to xn; note that n includes the start and end points, and is therefore not equal to the number of intervals between the start and end points. In fact, identically the same graph (Figure 3) can be generated using the linspace operator: >> theta = linspace(0,90,7); >> y = cos(theta*pi/180); >> figure >> plot(theta, y) If the argument n is omitted, the function defaults to 100. Interestingly, this is often a good compromise value for plotting functions. Note that to create a new figure, the command figure must appear before the plot function is applied, as indicated in the above examples. A useful facility is that of being able to include several graphs in the same plot area, merely by including several sets of arguments in the plot brackets: >> x = linspace(0,8); >> y1 = 8*exp(-1.1*x); >> y2 = x; >> y3 = 1.5*y1 .* y2; >> figure >> plot(x, y1, x, y2, x, y3)

Figure 4 Note how the complete script for Figure 4 includes not only the plotted functions but also information on the plotting colours, plotting characteristics, axes, tick marks and instructions to include a grid in the graphics box.

Page 4: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 4

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

Complete script for Figure 4 x = linspace(0,8); y1 = 8*exp(-1.1*x); y2 = x; y3 = 1.5*y1 .* y2; figure plot(x,y1,'r--',x,y2,'b-.',x,y3,'k-','LineWidth',1.2) pbaspect([4 3 1]) axis([0 8 0 8]) set(gca, 'fontsize',14) set(gca, 'XTick',[0 2 4 6 8]) set(gca, 'YTick',[0 2 4 6 8]) set(gca, 'LineWidth',1.0) grid on

An alternative is to make use of the hold on function: this has been incorporated into the following code, generating exactly the same figure: >> x = linspace(0,8); >> y1 = 8*exp(-1.1*x); >> y2 = x; >> y3 = 1.5*y1 .* y2; >> figure >> plot(x, y1) >> hold on >> plot(x, y2) >> plot(x, y3) After a hold on operation, a new plot is added without deleting any old ones, so previous plots in the current graphics window are retained. After a hold off operation, a new plot replaces any old ones, so previous plots in the current graphics window are eliminated. Note that once asserted, a hold state continues until a hold off operation is applied. Whereas it is useful to be able to show multiple graphs 'on top of each other' in the same plot space, it is often preferable to display them separately in an array of subplots, as in Figure 5. This is achieved with the following set of commands, in which we define several sets of axes and arrange them in a regular array:

Page 5: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 5

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

>> x = linspace(0,8); >> y1 = 8*exp(-1.1*x); >> y2 = x; >> y3 = 1.5*y1 .* y2; >> figure >> axes1 = subplot(2,2,1); >> plot(axes1, x, y1) >> axes2 = subplot(2,2,2); >> plot(axes2, x, y2) >> axes3 = subplot(2,2,3); >> plot(axes3, x, y3) >> axes4 = subplot(2,2,4); >> plot(axes4, x, y1, x, y2, x, y3)

Figure 5 Complete script for Figure 5 x = linspace(0,8); y1 = 8*exp(-1.1*x); y2 = x; y3 = 1.5*y1 .* y2; figure set(gca, 'fontsize',28) set(gca, 'LineWidth',1.5) axes1 = subplot(2,2,1); plot(axes1,x,y1,'r--','LineWidth',1.0) axis([0 8 0 8]); pbaspect([4 3 1]); grid on axes2 = subplot(2,2,2); plot(axes2,x,y2,'b-.','LineWidth',1.0) axis([0 8 0 8]); pbaspect([4 3 1]); grid on axes3 = subplot(2,2,3); plot(axes3,x,y3,'k-','LineWidth',1.0) axis([0 8 0 8]); pbaspect([4 3 1]); grid on axes4 = subplot(2,2,4); plot(axes4,x,y1,'r--',x,y2,'b-.',x,y3,'k-','LineWidth',1.0) axis([0 8 0 8]); pbaspect([4 3 1]); grid on

Page 6: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 6

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

When using the subplot function, the three arguments appearing in the brackets are respectively the number of rows of subplots, the number of column subplots and the number in the sequence (in which we number along the first row and then along the other rows in turn). So far, we have only considered graphs in which y is plotted as a function of x—which is a strategy that only works for single-valued functions. However, it is easy to generalise it to other types of function. This is achieved by using parametric forms for such functions. For example, Figure 6 shows that a circle is easily generated by taking angle theta relative to its centre and employing sine and cosine functions: >> theta = linspace(0,360); >> x = cos(theta); >> y = sin(theta); >> figure >> plot(x, y)

Figure 6 Complete script for Figure 6 theta = linspace(0,360); x = cos(theta*pi/180); y = sin(theta*pi/180); figure plot(x,y,'r--','LineWidth',1.2) axis square axis([-1 1 -1 1]) set(gca, 'fontsize',14) set(gca, 'LineWidth',1.0) grid on

3. Labelling and formatting graphs Clearly, it can be confusing for several functions to be plotted in the same 2-D space, so it is useful for them to be distinguished by various line styles and colors, and also to have the capability for adding discrete markers to represent specific points. All this can be achieved by including line and marker specifications alongside the plot arguments. In fact, we can generalise the previous plot instruction to the following:

Page 7: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 7

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

>> plot(x, y1, linespec1, x, y2, linespec2, x, y3, linespec3) This may be implemented using the following simple notation that will be described below: >> figure >> plot(x, y1, 'r--', x, y2, 'g:', x, y3, 'b-') Importantly, the line specification is conveniently expressed in a few characters such as '--ro', where '--' means that the line is dashed, 'r' means that the color is red, and 'o' means that the marker is to be a circle. Note that: • the whole of the specification is expressed in single quotes which include an

optional line specification L, an optional marker specification M, and an optional color specification C

• L, M and C can be written in any order • if only L is included, only a line is drawn • if only M is included, only markers are drawn • if both L and M are included, both a line and markers are drawn, with the same

color • if neither L nor M is included, a default line is drawn without any markers. The highly compact representation described above is made possible by use of a cleverly chosen set of codes, including the following:

line style code marker code dashed ‐‐ point . dotted : circle o dash dot ‐. square s solid (default) ‐ diamond d pentagram (5-point star) p color code hexagram (6-point star) h red r plus + green g cross x blue b asterisk * cyan c N-pointing triangle ^ magenta m S-pointing triangle v yellow y E-pointing triangle > white w W-pointing triangle < black k

(Note that, to avoid confusion with blue, black is represented by its last letter.) To provide further guidance, most of the codes listed above were used to generate Figure 7 and are included in its Matlab script. There are a few additional refinements to the above description. For example, to avoid all the data points appearing as markers, marker indices can be specified by including the following additional information in the line specification:

Page 8: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 8

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

'MarkerIndices', 1:n:length(y): this ensures that only every nth data point will be a marker. Markers such as squares and circles can have different boundary (edge) and fill (face) colors. These can be assigned using the following name–value pair extensions to the line specification: 'MarkerEdgeColor', 'g' and 'MarkerFaceColor', 'b'. The widths of lines can be specified by using the 'LineWidth' parameter, which is set using point values, with a default value of 0.5: note that this parameter also modifies the boundary widths of markers. Finally, marker size can also be set using the name–value pair, 'MarkerSize', m, where m is proportional to the area of the marker: it has a default value of 6.

Figure 7 Complete script for Figure 7 x = linspace(0,8,9); y = [2 3 7 2 6 3 5 4 1]; figure grid on; box on hold on plot(x,y*1.1,'r.','MarkerSize',15,'LineWidth',1.2) plot(x,y,'-go','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.9,'bs','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.8,'--cd','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.7,'mp','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.6,'-.yh','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.5,'kv','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.4,':r<','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.3,'g>','MarkerSize',5,'LineWidth',1.2) plot(x,y*0.2,'ws','MarkerSize',8,'LineWidth',1.0,'MarkerFaceColor','w') pbaspect([4 3 1]) axis([0 8 0 8]) set(gca, 'fontsize',14) set(gca, 'XTick',[0 2 4 6 8]) set(gca, 'YTick',[0 2 4 6 8]) set(gca, 'LineWidth',1.0)

Interestingly, color can be specified more accurately using RGB triplet values: these must each be in the range [0, 1]. The table below gives the equivalent RGB triplet values for the above eight common colors:

Page 9: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 9

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

color code RGB triplet red r [1 0 0] green g [0 1 0] blue b [0 0 1] cyan c [0 1 1] magenta m [1 0 1] yellow y [1 1 0] white w [1 1 1] black k [0 0 0]

Plots may be augmented by including titles, labels along the x and y axes, and similarly for subplots: title('2-D line plot') xlabel('x-axis') ylabel('y-axis') title(axes1, '2-D line subplot1') xlabel(axes1, 'subplot1 x-axis') ylabel(axes1, 'subplot1 y-axis') Many possibilities exist for formatting titles and labels. In particular, the following are important. Font size, font weight and font color can be modified using the usual name–value pair arguments, e.g., xlabel('x-axis', 'FontSize', 12, 'FontWeight', 'bold', 'Color', 'g'). Here, FontSize specifies the font size in points, with 11 points being the default for labels and 10 points being the default for axes. In addition, 'FontName' specifies the name of the font (e.g., 'Courier'), which defaults to Helvetica. Labelling can include Greek letters and other characters generated by using TeX markup—e.g., \pi, \leq, \theta, and so on. Note that superscripts and subscripts can be included using the TeX '^' and '_' notation, accompanied by curly brackets if more than one character is to be inserted (^{ } and _{ }). TeX markup also generalises the inclusion of font weights (\bf (bold), \rm (normal), \it (italic)), font sizes (\fontsize{8} ...), font colors using both standard colors (\color{magenta}, ...), and RGB triplets (\color[rgb]{0.1, 0.2, 0.3}, ...). Note that variable names can be converted to strings using the num2str function. This is frequently useful, e.g., for labelling subplots automatically, as in Figure 8: >> x = linspace(0,8); >> figure >> for i = 1:4 >> axesi = subplot(2,2,i); >> y = exp(-i/2*x); >> plot(axesi, x, y) >> title( axesi, [ 'scale factor ' num2str(i/2) ]) >> % the square brackets act to concatenate the two strings >> end

Page 10: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 10

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

Figure 8

Figure 9

Figure 10 Notice that unnecessary space between the four subplots can be eliminated by using the get function to access the subplot position vector (this has four elements [left, bottom, width, height]), adjusting the left and bottom values, and finally applying the new position using the set function: the result is shown in Figure 9; the complete Matlab script appears below. Additionally, if (as sometimes happens in practice) the axes and title options are not needed, further space can be removed as shown in Figure 10: note that the axis off option eliminates the axes, tick marks and labels. In Figure 10 the position adjustments were set at ±0.04, ±0.03—exactly double those for Figure 9.

Page 11: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 11

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

Complete script for Figure 9 x = linspace(0,8); figure set(gca, 'fontsize',28) for i = 1:4 axesi = subplot(2,2,i); y = exp(-i/2*x); p = get(axesi, 'position'); if i==1 || i==3, p(1) = p(1) + 0.02; end if i==2 || i==4, p(1) = p(1) - 0.02; end if i==1 || i==2, p(2) = p(2) - 0.015; end if i==3 || i==4, p(2) = p(2) + 0.015; end set(axesi, 'position', p) plot(axesi,x,y,'-k','LineWidth',1.0) set(gca, 'XTick',([0 2 4 6 8])) set(gca, 'YTick',([0 0.2 0.4 0.6 0.8 1])) title(axesi,['scale factor ' num2str(i/2) ]) end

One other function that is often useful is the line function, which is aimed at joining single pairs of points. As such it could be regarded as replacing the plot function, which is aimed at joining sequences of points. However, it can be useful for simplicity: for example, it can be used to draw a line right across a set of existing plots without deleting other objects or changing the axis settings. A simple example is: >> x = [1 8]; >> y = [3 7]; >> line(x, y, '--dg') >> % here the line end points are (1,3) and (8,7) More elaborate examples are shown in Figures 11 and 12, together with their associated Matlab scripts. Interestingly, more relevant lines and curves often need to be plotted later so that they can be seen to partially overwrite earlier plots—which can be important to the appearance of the final figure, as happens in Figures 11 and 12.

Page 12: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 12

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

Figure 11. This figure shows how plots, lines, axes, scales and labelling can be put together to form a vital illustration. It is essentially the same as Figure 14.14 in Davies 2017 (5th edition, Elsevier). Complete script for Figure 11 % sigmoid function % first, generate the functions x = linspace(-6,6); ye = exp(-x); lengthx = length(x); for i = 1:lengthx ys(i) = 1/(1 + ye(i)); end figure % next, include the axes and tick marks axis([-6 6 0 1]); hold on pbaspect([5 3 1]) grid on; box on set(gca, 'LineWidth',1.0) set(gca, 'XTick',[-6 -4 -2 0 2 4 6]) set(gca, 'YTick',[0 0.2 0.4 0.6 0.8 1]) set(gca, 'fontsize',10) % add 1 colored line and 2 black lines line([-6,6],[0.5,0.5],'color','k','linewidth',1.0) line([0,0],[0,1],'color','k','linewidth',1.0) line([-2,2],[0,1],'color','b','linewidth',1.0) % plot 1 colored curve plot(x,ys,'r','linewidth',1.0) % include the title with the right weight and position set(gca, 'TitleFontWeight','normal') thandle = title('Logistic sigmoid function'); p = get(thandle,'Position'); p(2) = p(2) + 0.01; set(thandle,'Position',p,'fontsize',12) % label the x and y axes Labelsize = 12; xlabel('\itv','fontsize',labelsize) text(-7.5,0.507,'\sigma({\itv})','fontsize',labelsize) % (to avoid label orientation problems the ylabel function was avoided)

Page 13: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 13

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

Figure 12. This figure includes many related plots and lines, together with essential labelling. It is essentially the same as Figure 14.13 in Davies 2017 (5th edition, Elsevier). Complete script for Figure 12 % comparison of loss functions % first, generate the functions x = linspace(-3,3); ye = exp(-x); yoffset = 1 - log(2); yl = log(1 + exp(-x*2)) + yoffset; figure % next, include the axes and tick marks axis([-3 3 0 5]); hold on set(gca, 'dataaspectratio',[1 1 1]) grid on; box on set(gca, 'LineWidth',1.0) set(gca, 'XTick',[-3 -2 -1 0 1 2 3]) set(gca, 'YTick',[0 1 2 3 4 5]) set(gca, 'fontsize',11) % add 3 colored lines and 3 black lines line([-3,1],[4,0],'color',[0 0.8 0],'linewidth',1.0) line([-3,0],[6+yoffset,yoffset],'color',[0 0.8 0.8],'linewidth',1.0) line([0,3],[yoffset,yoffset],'color',[0 0.8 0.8],'linewidth',1.0) line([-3,0],[1,1],'color','k','linewidth',1.0) line([0,3],[0,0],'color','k','linewidth',1.0) line([0,0],[1,0],'color','k','linewidth',1.0) % plot 2 colored curves plot(x,ye,'r','LineWidth',1.0) plot(x,yl,'b','LineWidth',1.0) % include the title with the right weight and position set(gca, 'TitleFontWeight','normal') thandle = title('Comparison of loss functions used for boosting'); p = get(thandle,'Position'); p(2) = p(2) + 0.05; set(thandle,'Position',p) % label the x-axis and add labels to the individual plots xlabel('\ityF') text(-0.37,0.65 ,'\itA','fontsize',11) text(-1.45,4.5 ,'\itE','fontsize',11) text(-1.9 ,2.63 ,'\itH','fontsize',11) text(-2.25 ,1.15 ,'\itS','fontsize',11) text(-1.6,3.7,'\itL','fontsize',11) text(-1.58,3.82,'~','fontsize',9)

Page 14: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 14

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

4. Further details of the Matlab scripts At this point we explain further aspects of the Matlab scripts used to generate Figures 2, 4, 5, 6, 7, 9, 10, 11 and 12. First note that in all these cases the linspace function has been used as the basis for plotting operations. Figure 2: 'pbaspect' has the effect of setting the aspect ratio of the figure to a

suitable landscape or other shape; 'set(gca, ...)' has been used to set graphics options, 'gca' being the 'handle'

for the current figure axes; 'XTick' and 'YTick' are used to set tick marks along the two axes; 'Linewidth' has to be set separately for the axes and the plots; 'grid on' is used to add an optional grid framework to the axes. Figure 4: axis([xmin xmax ymin ymax]) is used to set the respective ranges of the x

and y values. Figure 5: the 'subplot' option is useful in arranging the various plotting areas in a

generally appropriate way, regarding size, position, and labelling: however, labels will often be reduced markedly in size, though this can be offset by making the initial sizes significantly larger than usual.

Figure 6: The 'axis square' option makes the current axis frame square, so that (as here) circles can be properly presented.

Figure 7: This figure is based on Figure 2 and is a test case for illustrating the available types of marker: their shapes and colors cover most of those listed on p. 8. Note that the result of the white color ('w') is not always easy to discern.

Figure 9: The position vector of a subplot has four elements, [left, bottom, width, height]. Here, adjusting it allows unnecessary space to be eliminated between the subplots. (Note that the position vector can be applied not only to subplots but also the position of a title or any other graphics object.)

Figure 10: This version of Figure 9 uses 'axis off' to eliminate the axes, tick marks and labels; the title option is also eliminated from Figure 9. The position vector can now be adjusted to eliminate further space between the subplots. (In this case the full script is not shown.)

Figure 11: The 'line' function allows straight lines to be included, joining pairs of (x, y) coordinates, albeit expressing them in terms of [xmin xmax] and [ymin ymax] vectors;

note how a Greek letter can be included in the text: in this case '\sigma(\itv)' adds not only the Greek letter sigma but also an italic v;

'TitleFontWeight' was set to 'normal' so that the default bold option was inactivated.

Figure 12: In this case, the 'dataaspectratio' option was used to make the grid boxes exactly square;

'TitleFontWeight' was again set to 'normal'; RGB triplet values were used to create dark green [0 0.8 0] and dark cyan

[0 0.8 0.8], to help these colors stand out in the figure; extra care was needed when producing the L label; ideally, this would be

done using the Latex labelling facility, '$\tilde{L}$', 'Interpreter', 'latex', but making the desired font appear proved problematic.

Page 15: Graphics and plotting techniques · Graphics and plotting techniques . 1. Introduction . The purpose of this tutorial is to outline the basics of graphics and plotting in two dimensions

Davies: Computer Vision, 5th edition, online materials Matlab Tutorial 5 15

CVL Matlab5bi 09 October 2018, 16:21 © E. R. Davies 2018

5. Summary of plotting commands and options We now return to the subject of axes and examine in more detail how they are defined. The first example shows how axes can be limited to given ranges of x and y values; then follow further instances of axes-related commands and other commands which are needed to complete the plotting scenario. Most of these commands do not require a semicolon to follow them to suppress the display of variables, an important exception being the get function. • axis( [ ... ] ) the four arguments in the square brackets represent the respective ranges

of x- and y-values, viz., [xmin xmax ymin ymax] • axis auto returns the axis scaling to its default where the best axis limits are

computed automatically • axis square makes the current axis frame square, so that (for example) a circle will

appear like a circle • axis equal makes the scales of both axes equal • axis tight returns the minimum axes that can represent all the relevant data • axis normal returns axis to the default settings • axis on turns on the axes, showing labelling and tick marks • axis off turns off the axes, tick marks and labels • hold on arranges that a new plot is added without deleting any old ones, so

previous plots in the current graphics window are retained • hold off arranges that a new plot replaces any old ones, so previous plots in the

current graphics window are eliminated • grid on adds a grid to a graph • grid off removes the grid from the graph • box on draws a box around the active axes area • box off turns off the current box • pbaspect used to adjust the relative lengths of the x-axis, y-axis, and z-axis (for

2-D plots, the z argument is normally 1, e.g., [1 2 1]) • set used to set a variety graphics options • get used to obtain current settings of relevant graphics variables: unlike set,

get may need to be followed by a semicolon, to suppress printing of irrelevant variables

• gca used to return the graphics handle to the current figure axes • XTick used to set xtick marks along the x-axis,

e.g., set(gca, 'XTick', [ 0 1 2 3 4 5 6 7 8 ]) • YTick used to set ytick marks along y-axis

e.g., set(gca, 'YTick', [ -3 -2 -1 0 1 2 3 ]) • text used to place text on a graph,

e.g., text(2.3, 5.2, '\leftarrow minimum') • gtext allows text to be placed using the mouse cursor button,

e.g., gtext('\leftarrow minimum') • TeX markup to set the Latex interpreter as the default, use the command:

set(0, 'defaultTextInterpreter', 'latex') • figure generates a new (empty) figure window • clf clears the current figure window • close all closes all the open figures (useful to save memory when testing Matlab

programs) • clear clears workspace: eliminates all current variables (useful before starting

work on a new or revised Matlab program) • clc clears command window • dbquit quit debug mode (which is evident from the debug prompt: 'K>>')