Wichita State University Logo

Math 451: Computational Mathematics

2.5 Graphics


2.5.1 Basic Two-Dimensional Plots

The MATLAB library of functions and IDE (Integrated Development Environment) allow one to visualize curves in the Cartesian plane. We can use the MATLAB function sort to sort some uniformly distributed numbers in the interval $[0,10]$ using the MATLAB function rand . We use this increasing sequence of numbers for our abscissa ($x$) values. We can use rand again to obtain our ordinate ($y$) values. The plot function creates simple connect-the-dots style graph.

In [22]:
format short
x = sort(rand(1,10));
y = (1:10) + rand(1,10);
plot(x,y)

       

The first argument to the plot function is an array of $x$-values and the second argument is an array of $y$-values. These arrays must be the same size. The plot function graphs each coordinate pair $\left(x(i),y(i)\right)$ and draws a straight line that connects them.

One can add a third argument to plot ; a line specification . A line specification is a string that controls the line style , marker , and color of the curve created. For example, if we add the string 'r*--' , to our plot function, we see a red splat (asterisk) plotted at each point $\left(x(i),y(i)\right)$, and the points are joined by a red dashed line.

In [21]:
x = sort(rand(1,10));
y = (1:10) + rand(1,10);
plot(x,y,'r*--')

       

If we use the line specification 'vo' , each point $\left(x(i),y(i)\right)$ will be marked with a magenta 'o', and no line will join the markers .

In [20]:
x = sort(rand(1,10));
y = (1:10) + rand(1,10);
plot(x,y,'m+')

       

More than one set of points can be passed to plot.

In [26]:
x = 10*sort(rand(1,10)) - 5;
y = (1:10) + rand(1,10);
z = (-5:5);
w = (10:-1:0) + rand(1,11);
plot(x,y, 'gd-.', z, w, 'b+:')

       

Once may further control the properties of a plot by adding more arguments. To mark the data points with large green squares connected by red dashed lines:

In [27]:
x = 10*sort(rand(1,10)) - 5;
y = (1:10) + rand(1,10);
plot(x,y,'--rs','MarkerSize',20,'MarkerFaceColor','g')

       

2.5.2 More Types of Two Dimensional Plots

We can use the MATLAB function loglog , semilogx , and semilogy to create functions with axes set to a logarithmic scale . You should read the documentation for each of the past links to see examples of MATLAB plots one can create. One may create multiple plots using the MATLAB function figure . Mathworks supplies a variety of functions that can be used to visualize data.

In [29]:
x = 10*sort(rand(1,10)) - 5;
y = exp((1:10) + rand(1,10));
z = exp(-5:5);
w = (10:-1:0) + rand(1,11);
semilogy(x,y, 'r-')
figure
semilogx(z, w, 'bp:')

       

2.5.3 Plot Annotation

The appearance and even existence of the axes of a plot can be controlled by MATLAB functions to format and annotate your graphs. You can specify the appearance of the axes , display a legend , and create titles and labels for your graphs. You can create data distribution plots , geographic plots , contour plots and plot in polar coordinates .

MATLAB contains some animation functions. A more powerful technique creates a series of plots in a loop that are added to a movie file. We will explore some of these techniques in class and in your homework assignment.

Creative Commons Logo - White


Your use of this self-initiated mediated course material is subject to our Creative Commons License .


Creative Commons Attribution-NonCommercial-ShareAlike 4.0

Creative Commons Logo - Black
Attribution
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

Creative Commons Logo - Black
Noncommercial
You may not use the material for commercial purposes.

Creative Commons Logo - Black
Share Alike
You are free to share, copy and redistribute the material in any medium or format. If you adapt, remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.