Difference between revisions of "Matlab Plotting"

From Sutherland_wiki
Jump to: navigation, search
m
(X-Y Line Plots)
Line 4: Line 4:
  
 
== X-Y Line Plots ==
 
== X-Y Line Plots ==
 +
Perhaps the most common plots we create as engineers are x-y line plots that show the functional dependence of one variable (y) on another (x).  In MATLAB, an x-y plot is very simple to make.  For example, if we want to plot <math>y=\cos(x)</math> on the interval (-&pi;,&pi;) we can do this simply as
 +
<source lang="matlab">
 +
  x = linspace(-pi,pi);  % create the x vector
 +
  plot(x,cos(x));        % plot cos(x) versus x.
 +
</source>
 +
  
 
=== Basics ===
 
=== Basics ===
 +
The basic syntax for plotting a function is the following
 +
<source lang="matlab">
 +
  plot( x, y );      % simplest way to plot a single function
 +
  plot( x, y, 'abc'); % controls the line style
 +
</source>
 +
Here '''a''' represents the color to use when plotting this function, '''b''' represents the type of symbol to use (dots, squares, etc), and '''c''' represents the style of the line to use (solid, dashed, etc). The following three tables describe what to insert for '''a''', '''b''' and '''c''' to control plot styles.
 +
 +
{| border="1" cellpadding="5" cellspacing="0" align="center" style="text-align:left"
 +
|+ '''Line & Symbol Colors'''
 +
|-
 +
! Symbol
 +
| <tt>b</tt> || <tt>g</tt> || <tt>r</tt>
 +
| <tt>c</tt> || <tt>m</tt> || <tt>y</tt>
 +
| <tt>k</tt> || <tt>w</tt> ||
 +
|-
 +
! Color
 +
| style="color:blue"  | Blue  || style="color:green"  | Green  || style="color:red"    | Red
 +
| style="color:cyan"  | Cyan  || style="color:magenta" | Magenta || style="color:yellow" | Yellow
 +
| style="color:black" | Black || style="color:white" bgcolor="black" | white
 +
| Default color
 +
|}
 +
 +
 +
{| border="1" cellpadding="5" cellspacing="0" align="center" style="text-align:left"
 +
|+ '''Line Markers'''
 +
|-
 +
! Symbol
 +
| <tt>.</tt> || <tt>o</tt> || <tt>x</tt> || <tt>+</tt> || <tt>*</tt>
 +
| <tt>s</tt> || <tt>d</tt> || <tt>^</tt> || <tt>v</tt> || <tt><</tt>
 +
| <tt>></tt> || <tt>p</tt> || <tt>h</tt> ||
 +
|-
 +
! Description
 +
| Point || Circle || <tt>x</tt> || plus || star
 +
| Square || Diamond || Triangle (up) || Triangle (down) || Triangle (left)
 +
| Triangle (right) || Pentagon || Hexagon || No Symbol
 +
|}
 +
 +
 +
{| border="1" cellpadding="5" cellspacing="0" align="center" style="text-align:left"
 +
|+ '''Line Styles'''
 +
|-
 +
! Symbol
 +
| <tt>-</tt> || <tt>:</tt> || <tt>.-</tt> || <tt>--</tt> ||
 +
|-
 +
! Description
 +
| Solid || Dotted || Dot-Dash || Dashed || No line
 +
|}
 +
 +
The following are some examples of creating plots
 +
<source lang="matlab">
 +
  x = linspace(-pi,pi);    % create 100 points between -pi and pi
 +
  y = sin(x) .* cos(x);    % evaluate a function at each point
 +
  plot(x,y,'rs:');        % plot a red dotted line with squares
 +
  figure; plot(x,y,'g--'); % plot a green dashed line
 +
  figure; plot(x,y,'ko');  % plot black circles (no line)
 +
</source>
 +
Note that the '''figure''' command creates a new plotting window.  If you leave it out, the next plot will be overwritten.
 +
 +
=== Title and Axis Labels ===
 +
Any time you create a plot, you must also label it!  Axis labels (with units where appropriate) are critical.  To create a title and label axes a plot in MATLAB, we use the following:
 +
<source lang="matlab">
 +
  xlabel('label text');
 +
  ylabel('label text');
 +
  title('title text');
 +
</source>
 +
 +
{| border="1" cellpadding="5" cellspacing="0" align="center" style="text-align:center"
 +
|+ '''Special Characters'''
 +
|-
 +
! Text
 +
| \Lambda || \Xi  || \Pi    || \Sigma  || \Theta  || \Psi || \Omega
 +
| \alpha  || \beta || \gamma || \delta || \epsilon || \eta || \theta
 +
|-
 +
! Symbol
 +
| &Lambda; || &Xi;  || &Pi;    || &Sigma; || &Theta;  || &Psi; || &Omega;
 +
|| &alpha; || &beta; || &gamma; || &delta  || &epsilon; || &eta; || &theta;
 +
|}
 +
 +
Example:
 +
<source lang="matlab">
 +
  x=linspace(-pi,pi);
 +
  plot(x,cos(x),'r--');
 +
  xlabel('\theta');
 +
  ylabel('cos(\theta)');
 +
  title('A plot of the function cos(\theta) as a function of \theta');
 +
</source>
  
=== Adding Axis Labels ===
 
  
 
=== Multiple Lines on a Graph ===
 
=== Multiple Lines on a Graph ===
 +
 +
We often want to place multiple lines on a single plot.  For example, if we want to plot the functions
 +
<center><math>
 +
\begin{align}
 +
  f_1(x) &= \cos(x) \\
 +
  f_2(x) &= \sin(x) \\
 +
  f_3(x) &= \tan(x)
 +
\end{align}
 +
</math></center>
 +
on the interval (-&pi;,&pi;) we could do the following
 +
<source lang="matlab">
 +
  x = linspace(-pi,pi);
 +
  f1 = cos(x);
 +
  f2 = sin(x);
 +
  f3 = tan(x);
 +
  plot(x,f1, x,f2, x,f3);
 +
</source>
 +
Here MATLAB automatically selects the colors for the lines.  If we want more control over line styles, we can specify them ourselves:
 +
<source lang="matlab">
 +
  plot(x,f1,'k-', x, f2, 'r:', x, f3, 'b-.');
 +
</source>
 +
 +
==== The <tt>hold on</tt> command ====
 +
 +
<source lang="matlab">
 +
  x = linspace(-pi,pi);
 +
  f1 = cos(x);
 +
  f2 = sin(x);
 +
  f3 = tan(x);
 +
  plot(x,f1,'k-');
 +
  hold on;
 +
  plot(x,f2,'r:');
 +
  plot(x,f3,'b-.');
 +
  hold off;
 +
</source>
 +
 +
 
==== Adding a Legend ====
 
==== Adding a Legend ====
  
Line 22: Line 150:
 
* Adding a grid
 
* Adding a grid
 
* Changing axis ranges
 
* Changing axis ranges
 
 
  
 
== Contour & Surface Plots ==
 
== Contour & Surface Plots ==

Revision as of 15:40, 23 August 2008


Warn.jpg
This page is a stub and needs to be expanded.
If you can provide information or finish this page you're welcome to do so and then remove this message afterwards.

X-Y Line Plots

Perhaps the most common plots we create as engineers are x-y line plots that show the functional dependence of one variable (y) on another (x). In MATLAB, an x-y plot is very simple to make. For example, if we want to plot y=\cos(x) on the interval (-π,π) we can do this simply as

   x = linspace(-pi,pi);  % create the x vector
   plot(x,cos(x));        % plot cos(x) versus x.


Basics

The basic syntax for plotting a function is the following

   plot( x, y );       % simplest way to plot a single function
   plot( x, y, 'abc'); % controls the line style

Here a represents the color to use when plotting this function, b represents the type of symbol to use (dots, squares, etc), and c represents the style of the line to use (solid, dashed, etc). The following three tables describe what to insert for a, b and c to control plot styles.

Line & Symbol Colors
Symbol b g r c m y k w
Color Blue Green Red Cyan Magenta Yellow Black white Default color


Line Markers
Symbol . o x + * s d ^ v < > p h
Description Point Circle x plus star Square Diamond Triangle (up) Triangle (down) Triangle (left) Triangle (right) Pentagon Hexagon No Symbol


Line Styles
Symbol - : .- --
Description Solid Dotted Dot-Dash Dashed No line

The following are some examples of creating plots

   x = linspace(-pi,pi);    % create 100 points between -pi and pi
   y = sin(x) .* cos(x);    % evaluate a function at each point
   plot(x,y,'rs:');         % plot a red dotted line with squares
   figure; plot(x,y,'g--'); % plot a green dashed line
   figure; plot(x,y,'ko');  % plot black circles (no line)

Note that the figure command creates a new plotting window. If you leave it out, the next plot will be overwritten.

Title and Axis Labels

Any time you create a plot, you must also label it! Axis labels (with units where appropriate) are critical. To create a title and label axes a plot in MATLAB, we use the following:

  xlabel('label text');
  ylabel('label text');
  title('title text');
Special Characters
Text \Lambda \Xi \Pi \Sigma \Theta \Psi \Omega \alpha \beta \gamma \delta \epsilon \eta \theta
Symbol Λ Ξ Π Σ Θ Ψ Ω α β γ &delta ε η θ

Example:

   x=linspace(-pi,pi);
   plot(x,cos(x),'r--');
   xlabel('\theta');
   ylabel('cos(\theta)');
   title('A plot of the function cos(\theta) as a function of \theta');


Multiple Lines on a Graph

We often want to place multiple lines on a single plot. For example, if we want to plot the functions


 \begin{align}
   f_1(x) &= \cos(x) \\
   f_2(x) &= \sin(x) \\
   f_3(x) &= \tan(x)
 \end{align}

on the interval (-π,π) we could do the following

   x = linspace(-pi,pi);
   f1 = cos(x);
   f2 = sin(x);
   f3 = tan(x);
   plot(x,f1, x,f2, x,f3);

Here MATLAB automatically selects the colors for the lines. If we want more control over line styles, we can specify them ourselves:

   plot(x,f1,'k-', x, f2, 'r:', x, f3, 'b-.');

The hold on command

   x = linspace(-pi,pi);
   f1 = cos(x);
   f2 = sin(x);
   f3 = tan(x);
   plot(x,f1,'k-');
   hold on;
   plot(x,f2,'r:');
   plot(x,f3,'b-.');
   hold off;


Adding a Legend

Log-Scale Plots

  • semilogx
  • semilogy
  • loglog

Subplots

Fine-Tuning

  • Adding a grid
  • Changing axis ranges

Contour & Surface Plots

Histograms & Bar Graphs