Wednesday, October 3, 2007

Matlab - Basic Plotting

To plot one function in one graph (line plot)

t = 0 : pi/100 : 2*pi; %create a vector of values in the range [0, 2pi] in increments of pi/100

y = sin(t); %use this vector to evaluate the sine function over that range
plot(t,y)
grid on % Turn on grid lines for this plot

To plot several functions in one graph (several lines plot)

y = sin(t);
y2 = sin(t-0.25);
y3 = sin(t-0.5);
plot(t,y,t,y2,t,y3)

with lines type specified

t = 0:pi/100:2*pi;
y = sin(t);
y2 = sin(t-0.25);
y3 = sin(t-0.5);
plot(t,y,'-',t,y2,'--',t,y3,':')

Colors, Line Styles, and Markers

The basic plotting functions accepts character-string arguments that specify various line styles, marker symbols, and colors for each vector plotted. In the general form,

plot(x,y,'linestyle_marker_color')

linestyle_marker_color is a character string (delineated by single quotation marks) constructed from

  1. A line style (e.g., dashed, dotted, etc.)
  2. A marker type (e.g., x, *, o, etc.)
  3. A predefined color specifier (c, m, y, k, r, g, b, w)
For example,

plot(x,y,':squarey')%plots a yellow dotted line and places square markers at each data point.

If you specify a marker type, but not a line style, MATLAB draws only the marker.
The specification can consist of one or none of each specifier in any order.

For example, the string'go--' defines a dashed line with circular markers, both colored green.

You can also specify the size of the marker and, for markers that are closed shapes, you can specify separately the colors of the edges and the face.

x = -pi:pi/10:pi; % x is from -pi to pi, with increments pi/10
y = tan(sin(x)) - sin(tan(x)); % definition of y
plot(x,y,'--rs',... % plot the graph y = f(x), a red dashed line with squared markers
'LineWidth',2,... % a line with width of two points
'MarkerEdgeColor','k',... % the edge of the marker colored BLACK
'MarkerFaceColor','g',... % the facr of the marker colored green
'MarkerSize',10) % the size of the marker set to 10 points

Note: the three dots '...' tells Matlab that something more will be added in the next line.
W.o. them, Matlab will interpret the commend to be ended right when the line ends.

Adding plot to an existing graph

semilogx(1:100,'+') % screate a semilogarithmic plot
hold all % hold plot and cycle line colors
plot(1:3:300,1:100,'--') % then add a linear plot
hold off % stop the holding process
grid on % Turn on grid lines for this plot

Plotting only the data points

For example, given two vectors

x = 0:pi/15:4*pi; % x from 1 to 4pi, with increments pi/15.
y = exp(2*cos(x)); % y is a function in x
plot(x,y, 'r+') % call plot with only a color and a marker specifier, here a red plus sigh at each data point.

Plotting Markers and Lines

x = 0:pi/15:4*pi;
y = exp(2*cos(x)); % x and y as above
plot(x,y,'-r',...
% data as a red, solid line
x,y,'ok') % adds circular markers with black edges at each data point



Some options:
'a' - none
'b' = blue
'c' = cyan
'd' = looks like {}
'e' - none
'f' - none
'g'
'h' = star
'k' = black
'l' - none
'm' = pink/purple
'n' - none
'o' = circle
'p' = star
'q' - none
'r' = red
's' = square
't' - none
'u' - none
'v' - looks like gradient notation
'w' = white
'y' = yellow
'z' - none

'^' = upward triangle
'*' = snowflake
':' = tiny dashed
'<' = rightward triangle '>' = leftward triangle
'.' = tinny colored dot

No comments: