1D Plotting

From ComputingForScientists

Jump to: navigation, search

Contents

  1. References
  2. Notes
    1. Plotting array elements
    2. Annotation
    3. Line Color
    4. Marker Style
    5. Axis Numbering
  3. Problems
    1. Scalar Time Series Plots I
    2. Scalar Time Series Plots II

1. References

2. Notes

2.1. Plotting array elements

In MATLAB, line plots are created using the plot function.

% Create an array to plot
A = [1.0,4.0,16.0,32.0];
clf; % Clear the plot window
plot(A)
From raw.githubusercontent.com on May 19 2019 09:24:15.

The plot(A) command caused a plot of the values of A to be shown with the values in A shown on the y-axis. The x-axis values were assumed to be [1,2,3,4]. The above commands are equivalent to

A = [1.0,4.0,16.0,32.0];
x = [1,2,3,4];
clf; % Clear the plot.
plot(x,A);
From raw.githubusercontent.com on May 19 2019 09:24:15.

To change the x-axis values, modify the first array that is passed to the plot function.

A = [1.0,4.0,16.0,32.0];
x = [10,20,30,40]; 
clf; % Clear the plot window
plot(x,A);
From raw.githubusercontent.com on May 19 2019 09:24:15.

2.2. Annotation

To add a grid, write grid on after the plot command. To add a axis labels and a title, use xlabel, ylabel, and title

A = [1.0,4.0,16.0,32.0];
x = [1,2,3,4];
clf;
plot(x,A);
grid on;
xlabel('Time [seconds]');
ylabel('Height [meters]');
title('Experiment 1 results');
From raw.githubusercontent.com on May 19 2019 09:24:15.

2.3. Line Color

A style argument may be specified when calling the plot function. This set of commands will create a red line.

clf;
A = [1.0,4.0,16.0,32.0];
x = [1,2,3,4];
% Can be one of r,g,b,c,m,y,k,w
style = 'r'; 
plot(x,A,style);
From raw.githubusercontent.com on May 19 2019 09:24:15.

Other colors may be used by specifying a set of r,g,b values. This set of commands will create a gray line.

clf; 
A = [1.0,4.0,16.0,32.0];
x = [1,2,3,4];
plot(x,A,'Color',[0.5,0.5,0.5]);
From raw.githubusercontent.com on May 19 2019 09:24:15.

2.4. Marker Style

Marker styles are one of .,o,x,+,*,s,d,v,^,<,>,p,h

A = [1.0,4.0,16.0,64.0];
x = [1,2,3,4];
% Can be one of
% .,o,x,+,*,s,d,v,^,<,>,p,h
style = '*';
clf; 
plot(x,A,style);
From raw.githubusercontent.com on May 19 2019 09:24:15.

2.5. Axis Numbering

In this example, note that by default MATLAB chose to label the values in 0.5 increments. This is not a good default - all of the x-values are integers. The following example shows how to modify the values that are labeled.

To modify the x-position labels, use YTick instead of XTick.

A = [1.0,4.0,16.0,32.0];
x = [1,2,3,4];
clf;
plot(x,A);
% Label x-position labels
% at 1, 2, 3, and 4.
set(gca,'XTick',[1:4]);
grid on;
xlabel('Time [seconds]');
ylabel('Height [meters]');
title('Experiment 1 results');
From raw.githubusercontent.com on May 19 2019 09:24:15.

3. Problems

3.1. Scalar Time Series Plots I

Create a the plot shown below. The plot must have a grid, labels, x symbols of size 20, and a green line of width 3.

Turn in your program and a print-out of the plot (you can print both the program and the plot from within MATLAB using File-Print.)

3.2. Scalar Time Series Plots II

The following program computes population for two different scenarios.

  • An initial population of 100 and a growth rate of 1%/yr.
  • An initial population of 1000 and a decay rate of 10%/yr.
Pa(1) = 100;
for i = 2:40
    Pa(i) = Pa(i-1) + 0.05*Pa(i-1);
end

Pb(1) = 1000;
for i = 2:40
    Pb(i) = Pb(i-1) - 0.10*Pb(i-1);
end

Plot both population scenarios and draw a vertical line at the year in which the populations are nearest each other.

The plot should contain a legend and axis labels on the plot.

Personal tools