Plots in Matlab

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

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:

$f(x)=\frac{x+1}{x\cdot\sin(x)+1}$

<<define an anonymous function>>=
f=@(x)((x+1)./(x.*sin(x)+1))


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>>=
figure;
plot(x_domain,y)


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:

plot(x_domain,y,'linewidth',2);


which is easier to see on a projector.

<<example_of_plot_01.m>>=

define an anonymous function

domain and resolution

apply f

plot the result



Plot with legend

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

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)

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...

<<example_of_plot_with_legend_01.m>>=

fake datasets
%% plot
% notice the handle I get back from the plots
figure;
h1 = plot(data1.date, data1.value, 'linewidth',2)
hold on
h2 = plot(data2.date, 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' )