Plots in Matlab

From LiteratePrograms
Jump to: navigation, search

A lot of new users ask about how to plot the graph of a function in MATLAB, here are some simple examples.


[edit] Classic plot

result of the simple plot

If you want to draw the plot of a function, you first need to define your function. MATLAB provide anonymous function definition to do that.

We will first try something with:


<<define an anonymous function>>=

notice the 'dot' before the operators, it's because we want to perform a pointwise application of our function.

now we have to define about the domain on which we want to plot f, let's define it through its bounds, and define the resolution of our plot:

<<domain and resolution>>=
x_bounds     = [-3, 3];
x_resolution = .1;
x_domain     = (x_bounds(1):x_resolution:x_bounds(2))'

Then we can apply it by simply calling f on |x_domain|:

<<apply f>>=
y = f(x_domain);

we just have to plot it:

<<plot the result>>=

we decided to open a figure (we can add options to choose color, menu, etc) before really plotting the graph.

The plot function has options too, as in:


which is easier to see on a projector.

now the whole code for downloading purpose:


define an anonymous function

domain and resolution

apply f

plot the result

[edit] Plot with legend

Following a question on the MATLAB newsgroup, here is a standard way to build custom legends.

[edit] Generation of data

I first generate fake datasets to be used in my example.

  • The first one is a 2-dimensional exponential of Brownian motion
  • The second one is a 2-dimensional process whose increasings are uniformly distributed
<<fake datasets>>=
data1 = struct('date', (1:100)', 'value', 100*exp(cumsum(randn(100,2))/10), 'names', {{ 'line 1', 'line 2'}}, 'title', 'dataset 1')
data2 = struct('date', (1:100)', 'value', 100*cumsum(rand(100,2)*2-1+.1), 'names', {{ 'line 1', 'line 2'}}, 'title', 'dataset 2')

My dataset have a structured shape (read more on structured datasets in MATLAB), like this :

data1 = 
    date: [100x1 double]
   value: [100x2 double]
   names: {'line 1'  'line 2'}
   title: 'dataset 1'

This is quite useful to have such container which always includes :

  • values
  • corresponding dates
  • names of the columns (variables)
  • name of the dataset (i.e. title)

[edit] Plot and legend

result of the legend positionning

As you can see, the plot is quite standard but I demand an output which gives me handles on plotted lines.

Then, I can ask to the |legend| MATLAB function to only address those lines.

Notice the way I generate the legend which is a nice use of cellfun...


fake datasets
%% plot
% notice the handle I get back from the plots
h1 = plot(, data1.value, 'linewidth',2)
hold on
h2 = plot(, data2.value, ':', 'linewidth',2)
hold off
%% Legend
legend(gca,[h1;h2], cellfun(@(x,y)([x ' ' y]), ...
        {data1.names{:}, data2.names{:}}', ...
        cellstr( [repmat(data1.title, length(data1.names),1); ...
                  repmat(data2.title,length(data2.names),1)]), ...
        'uniformoutput',false), 'Location',  'NorthWest' )

[edit] External links

Download code