Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 3

Task 3.3 Detail: Controlling line styles and colors, and axis scales.

Summary of new tools and commands.

Task: In a script, create time ranging from 0 to 365 with an interval of 5 (units of days). Evaluate y=exp(-.01*time).*cos(2*pi*time/31).^2; Create a graph of y vs time. Control the extent of the time axis to be 365 days. Also, create a graph plotting the log of y vs time, again controlling the size of the time axis.

The plot command has a large number of options, which you can find by help plot or doc plot. Many of these options can be invoked by adding additional arguments to the command.

The line style and color are indicated by passing a character string as the third argument to the command; plot(x,y,'b-') requests a plot with a solid blue line.

The predefined colors are b (blue), g (green), r (red), k (black), w (white), c (cyan), m (magenta), or y (yellow). We will see later how to create other colors.

Line styles are - (solid), : (dotted), -. (dash-dot), or -- (dashed).

It is possible to plot symbols at the points (x,y). Some choices are . (point), o (circle), + (plus) or x (cross). See help plot for more choices.

It is possible to combine these options. For example, plot(x,y,'r+:') would draw a red dotted line through the points (x,y) and put red + symbols at the points.

How would you put lines and symbols on a plot with different colors? Unfortunately, plot(x,y,'b-r+') will not put a blue line with red pluses. The next task (3.4) may provide a hint of how to do this.

MATLAB tries hard to put useful scales on the plots that are created. Most of the time, it makes good choices. However, we sometimes want to have specific choices of the axis scales to control the extent of an axis (so similar plots have the same scales) or to exclude some part of a plot.

The function xlim([a b]) will set the range of values on the x axis from a to b. Notice that xlim wants a vector of length 2 with the first value being the minimum of x and the second being the maximum of x.

  SS3_3a

An alternative method would be to create a variable Xrange=[0 10]; and use xlim(Xrange). This method is a good idea if you have a number of similar plots to create and you want all of them to have the same scale on the x axis. It is also easy to change the axis on all of your plots by changing the values of one variable.

The function ylim([c d]) works in a similar fashion for the y axis.

In some cases, the values being plotted have a large range. In these circumstances, it is useful to plot the common logarithm of the values. loglog(x,y) will plot the values on a plot with both axes defined by powers of 10. semilogx(x,y) will make a similar plot with the x axis scaled in powers of 10 but the y axis having a linear scale. semilogy(x,y) will make a similar plot with the y axis scaled in powers of 10 but the x axis having a linear scale.

  SS3_3b

Script to complete this task:

%  task33.m
  time3=0:5:365;
  y3=exp(-.01*time3).*cos(2*pi*time3/31);
  figure
  plot(time3,y3)
  xlim([0 365])
  title('Monthly Change')
  xlabel('time (days)')
  ylabel('function')
  figure
  semilogy(time3,y3)  %  this command issues a warning. Do you see why?
  xlim([0 365])
  title('Monthly Change')
  xlabel('time (days)')
  ylabel('log function')

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 3


email: J. Klinck