topic 6 visualisations in matlab - unit...
TRANSCRIPT
CITS2401 Computer Analysis & Visualisation
SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS
Topic 6 Visualisations in
Matlab
Material from MATLAB for Engineers, Moore, Chapters 5,14 Additional material by Peter Kovesi and Wei Liu
The University of Western Australia
Two Dimensional Plots √ The x-y plot is the most commonly used plot by engineers
√ The independent variable is usually called x
√ The dependent variable is usually called y
The University of Western Australia
Consider this x-y data
time, sec Distance, Ft 0 0 2 0.33 4 4.13 6 6.29 8 6.85 10 11.19 12 13.19 14 13.96 16 16.33 18 18.17
Time is the independent variable and distance is the dependent variable
The University of Western Australia
The University of Western Australia
In the default mode the figure window is free floating, and appears on top of the MATLAB desktop.
It is often convenient to “dock” the figure, using the docking arrow
The University of Western Australia
The University of Western Australia
Engineers always add …
√ Title: use command title(‘Your title here’)!
√ X axis label, complete with units: xlabel(‘x-Label’)
√ Y axis label, complete with units: ylabel(‘y-Label’)
√ Often it is useful to add a grid: grid on
These commands apply to the most recently plotted figure.
You can use sprintf to use formatted output. E.g.:!
title(sprintf(’F(x) between %d and %d', x(1),x(20)))!
The University of Western Australia
The University of Western Australia
To add an apostrophe to a title (or other annotation) you must enter the single quote twice – otherwise MATLAB interprets the single apostrophe as the end of the string.
The University of Western Australia
Creating multiple plots
√ MATLAB overwrites the figure window every time you request a new plot
√ To open a new figure window use the figure function – for example figure(2)
√ hold on • Freezes the current plot, so that an additional plot can be
overlaid
√ When you use this approach the additional line is drawn in blue – the default drawing color
The University of Western Australia
The first plot is drawn in blue
To unfreeze the plot use the hold off command
The University of Western Australia
Each set of ordered pairs will produce a new line
You can also create multiple lines on a single graph with one command
The University of Western Australia
The legend function
√ The legend function is used to provide legends for each data set that have been displayed simultaneously on one set of axes via the plot(X1, Y1, X2, Y2, X3, Y3, ...) function.
√ For example:
>> legend('Label 1', 'Label 2', ..., 'Label N');
% Adds a legend for each data set in the plot.
√ Type help legend at the Matlab command prompt to learn more about the many options available with this function.
The University of Western Australia
Creating and switching between figure windows
√ The figure function is used to control different figure (or plot) windows.
>> figure; % Creates a new figure window.
% This becomes the "current" figure.
>> figure(N); % Makes figure N the current figure window.
% This function creates a new figure window
% called N if N does not already exist.
>> delete(N); % Deletes figure N.
The University of Western Australia
Variations √ If you use the plot command with a single matrix, MATLAB
plots the values versus the index number √ Usually this type of data is plotted on a bar graph √ When plotted on an x-y grid, it is often called a line graph
The University of Western Australia
If you want to create multiple plots, all with the same x value you can… √ Use alternating sets of ordered pairs
• plot(x, y1, x, y2, x, y3, x, y4)
√ Or group the y values into a matrix • z=[y1, y2, y3, y4]
• plot(x,z)
The University of Western Australia
The peaks(100) function creates a 100x100 array of values. Since this is a plot of a single variable, we get 100 different line plots
The peaks(100) function creates a 100x100 array of values, and will plot itself .
The University of Western Australia
Plots of Complex Arrays √ If the input to the plot command is a single array of complex
numbers, MATLAB plots the real component on the x-axis and the imaginary component on the y-axis
The University of Western Australia
Line, Color and Mark Style √ You can change the appearance of your plots by
selecting user defined • line styles
• color
• mark styles
√ Try using help plot
for a list of available styles
The University of Western Australia
Available choices Table 5. 2 Line, Mark and Color Options
Line Type Indicator Point Type Indicator Color Indicator
solid - point . blue b
dotted : circle o green g
dash-dot -. x-mark x red r
dashed -- plus + cyan c
star * magenta m
square s yellow y
diamond d black k
triangle down v
triangle up ^
triangle left <
triangle right >
pentagram p
hexagram h
The University of Western Australia
Specify your choices in a string √ For example
√ plot(x,y,':ok') • strings are identified with a tick mark
• if you don’t specify style, a default is used
– line style – none
– mark style – none
– color - blue
The University of Western Australia
dotted line
circles
black
The University of Western Australia
Axis scaling √ MATLAB automatically scales each plot to completely fill the
graph √ If you want to specify a different axis – use the axis command
axis([xmin,xmax,ymin,ymax])
√ Lets change the axes on the graph we just looked at
The University of Western Australia
The University of Western Australia
Subplots
√ The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns
√ subplot(m,n,p)
rows columns location
The University of Western Australia
Other Types of 2-D Plots
√ Polar Plots √ Logarithmic Plots √ Bar Graphs √ Pie Charts √ Histograms √ X-Y graphs with 2 y axes √ Function Plots
The University of Western Australia
Polar Plots
√ Some functions are easier to specify using polar coordinates than by using rectangular coordinates
√ For example the equation of a circle is • y=sin(x) in polar coordinates
The University of Western Australia
Logarithmic Plots √ A logarithmic scale (base 10) is convenient when
• a variable ranges over many orders of magnitude, because the wide range of values can be graphed, without compressing the smaller values.
• data varies exponentially.
√ plot – uses a linear scale on both axes √ semilogy – uses a log10 scale on the y axis √ semilogx – uses a log10 scale on the x axis √ loglog – use a log10 scale on both axes
The University of Western Australia
√ plot – uses a linear scale on both axes √ semilogy – uses a log10 scale on the y axis √ semilogx – uses a log10 scale on the x axis √ loglog – use a log10 scale on both axes
The University of Western Australia
x-y plot – linear on both axes
semilogx – log scale on the x axis
semilogy – log scale on the y axis
loglog – log scale on both axes
The University of Western Australia
The University of Western Australia
Bar Graphs and Pie Charts
√ MATLAB includes a whole family of bar graphs and pie charts • bar(x) – vertical bar graph • barh(x) – horizontal bar graph • bar3(x) – 3-D vertical bar graph • bar3h(x) – 3-D horizontal bar graph • pie(x) – pie chart • pie3(x) – 3-D pie chart
The University of Western Australia
The University of Western Australia
The University of Western Australia
Histograms √ A histogram is a plot
showing the distribution of a set of values
The University of Western Australia
X-Y Graphs with Two Y Axes √ Sometimes it is useful to overlay two x-y plots onto the same
figure. However, if the order of magnitude of the y values are quite different, it may be difficult to see how the data behave.
The University of Western Australia
plotyy: plots two functions with separate axis.
The University of Western Australia
5.3 Function Plots
√ Function plots allow you to use a function as input to a plot command, instead of a set of ordered pairs of x-y values
√ fplot('sin(x)',[-2*pi,2*pi])
function input as a string
range of the independent variable – in this case x
The University of Western Australia
Three Dimensional Plotting
√ Line plots
√ Surface plots
√ Contour plots
√ Cylinder plots
The University of Western Australia
Three Dimensional Line Plots
√ These plots require a set of order triples ( x-y-z values) as input
The University of Western Australia
Just for fun
√ try the comet3 function, which draws the graph in an animation sequence
√ comet3(x,y,z) √ If your animation draws too slowly, add more data points √ For 2-D line graphs use the comet function
The University of Western Australia
Surface Plots √ Represent x-y-z data as a surface
• mesh - meshplot
• surf – surface plot
The University of Western Australia
Using mesh with 3 variables √ If we know the
values of x and y that correspond to our z values, we can plot against those values instead of the index numbers
The University of Western Australia
The University of Western Australia
Shading
√ There are several shading options • shading interp
• shading flat
• faceted flat is the default
√ You can also adjust the color scheme with the color map function
The University of Western Australia
Default shading (faceted)
Shading interp
Shading flat
The University of Western Australia
Colormaps autumn bone hot spring colorcube hsv summer cool pink winter copper prism jet (default) flag white
The University of Western Australia
Contour Plots √ Contour plots use the
same input syntax as mesh and surf plots
√ They create graphs that look like the familiar contour maps used by hikers
The University of Western Australia
To demonstrate these functions we used a more interesting example
• A more complicated surface can be created by calculating the values of z, instead of just defining them
• We’ll need to use the meshgrid function to create 2-D input arrays – which will then be used to create a 2-D result
• [X,Y] = meshgrid(x,y) will return 2 n x m matrices where x is an n-vector and y is an m-vector.!
The University of Western Australia
The University of Western Australia
Example
√ For example, imagine generating two plots one above the other:
>> subplot(2, 1, 1), plot(x1, y1), title('Plot 1');
% The first call to subplot breaks the figure window into
% 2x1 cells, making the top one the active cell.
>> subplot(2, 1, 2), semilogy(x2, y2), title('Plot 2');
% A second call to subplot is needed to plot to the
% second cell.
The University of Western Australia
Another example >> xs = 0 : 0.1 : 10;
>> subplot(2, 1, 1), plot(xs, sin(xs)), xlabel('x'), ylabel('sin(x)');
>> subplot(2, 1, 2), plot(xs, cos(xs)), xlabel('x'), ylabel('cos(x)');
The University of Western Australia
3D line plot example
√ For example, the following commands:
>> t = 0 : 0.1 : 10;
>> x = exp(-0.2*t) .* cos(2*t);
>> y = exp(-0.2*t) .* sin(2*t);
>> plot3(x, y, t), xlabel('x'),
ylabel('y'), zlabel('time');
produces the plot:
The University of Western Australia
Surface plotting in 3D
√ The plot3 function is suitable for displaying two variables that are a function of one independent variable:
x = f1(t) y = f2(t)
√ When you have a single variable that is a function of two independent variables, say:
z = f(x, y)
√ then a surface display is more appropriate.
The University of Western Australia
mesh, surf, and contour functions √ Matlab's mesh, surf, and contour functions are used for displaying this
kind of data:
>> mesh(X, Y, Z) % Creates a mesh or wire-frame plot.
>> surf(X, Y, Z) % Creates a surface plot.
>> contour(X, Y, Z) % Creates a contour plot.
The University of Western Australia
The meshgrid function √ The meshgrid function is extremely useful for generating the X and Y
2D arrays for a 3D plot. >> Xvals = -2 : 1 : 2; % Generate axis vectors.
>> Yvals = -2 : 1 : 2;
>> [X, Y] = meshgrid(Xvals, Yvals) % Make a mesh grid.
X =
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
The University of Western Australia
Meshgrid Example Continues
Y = -2 -2 -2 -2 -2
-1 -1 -1 -1 -1
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
% Now calculate the function values on this mesh.
>> Z = X .* exp(-(X.^2 + Y.^2)); % A 2D Gaussian .* X.
>> mesh(X, Y, Z); % Display the plot as a mesh.
>> surf(X, Y, Z); % Display the plot as a surface.
>> contour(X, Y, Z); % Display the plot as a contour.
The University of Western Australia
Mesh, Surface and Contour Plot
æ By decreasing the increment in the axis vector generation commands, we can produce plots like:
The University of Western Australia
Surface plotting with a single argument √ You can also invoke these surface displaying functions with a
single argument.
√ This single argument is treated as the 2D array of Z values for each point.
√ The X and Y arrays default to a range of [1 .. the number of rows/columns of Z].
√ For example:
>> surf(Z)
The University of Western Australia
Parametric surface representation √ This specification of surfaces by three separate arrays - one for each of the x, y
and z coordinates of each data point is actually a parametric surface representation.
√ A parametric surface is parameterized by two independent variables, i and j, which vary continuously over a rectangle.
√ You can then specify functions x(i,j), y(i,j), and z(i,j) which determine the way in which x, y and z coordinates vary with i and j over the surface.
√ For example, the surface of the earth is parameterized in terms of longitude and latitude.
√ You can draw a map of the earth as a flat rectangle, but at each longitude and latitude we can determine the x, y and z values that correspond to the actual location of that point in space.
The University of Western Australia
2D arrays == flat ?
√ In discrete terms, Matlab represents parametric surfaces in terms of 2D arrays of X, Y, and Z values that correspond to each (i, j) grid point on the surface.
√ Hence the arguments to the mesh, surf, and contour functions are 2D arrays of points.
√ In the plots shown so far, the X and Y 2D arrays are "flat", but there is no need for them to be like that...
The University of Western Australia
A triangular prism æ A very wide variety of shapes can be
represented, e.g. a triangular prism can be represented by: X =
1.0 -0.5 -0.5 1.0
1.0 -0.5 -0.5 1.0
Y = 0.0 0.866 -0.866 0.0 0.0 0.866 -0.866 0.0
Z =
0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
The University of Western Australia
Functions for generating points √ Matlab provides some other handy functions for generating the
points of parametric surfaces, eg. cylinder and sphere...
>> [X, Y, Z] = cylinder(R, N);
% Generates an N sided cylinder. R is an array of
% radius values to be used along the length of
% the cylinder.
>> [X, Y, Z] = cylinder([1 1], 3);
% Produces a 3 sided prism.
>> [X, Y, Z] = cylinder([1 0], 4);
% Produces a 4 sided pyramid.
>> [X, Y, Z] = sphere(N);
% Produces a parametrically defined sphere.
The University of Western Australia
Parametric Surface Plots
The University of Western Australia
Nice looking surfaces
√ For a really nice looking surface use:
>> surfl(X, Y, Z)
>> shading interp
√ The surfl function behaves just like surf but renders a surface according to the current lighting model (the default lighting model is fine for almost every purpose you might have).
√ The shading across the surface is no longer a function of its "height".
√ Instead the shading is a function of the relative angle between the incident light and the surface normal at each point.
The University of Western Australia
Example >> [X Y Z] = cylinder([3 1 3 4 2 2 ],500); >> surfl(X, Y, Z) >> shading interp
The University of Western Australia
Images
√ Another way of viewing a single variable that is a function of two independent variables is to display it as a 2D image.
√ The Z, or height, values of the function is encoded by an intensity value or colour.
√ The human visual system is highly proficient at interpreting this form of display if the data represents intensity.
√ However, the visual system can have difficulty if the data represents some other quantity such as height.
√ Images in standard formats such as gif, tiff, and jpeg are stored so that each pixel value lies in the range [0,255].
The University of Western Australia
Why 0-255 √ The range [0, 255] is chosen so that each pixel can be represented by
8 bits (one byte).
• 0: Black
• 128: Grey
• 255: White
√ Colour images will typically consist of three 2D arrays in the range [0 , 255] for each of the red, green, and blue channels.
√ When you read an image into Matlab, Matlab stores the image as a matrix of numbers - each number lies in the range [0, 255].
√ To save memory, Matlab uses the type uint8 (an unsigned 8 bit integer which can represent the values [0, 255] only) to represent the numbers in the image matrix: