Table of Contents
Why Use Multiple Plots in One Figure
In many situations you want to compare several data sets that share the same x values, or you want to show related information together. MATLAB makes it straightforward to place multiple plots into a single figure. You can do this either by drawing several lines on one set of axes, or by combining separate axes inside the same figure. This chapter focuses on plotting multiple data series in one axes and creating additional axes in a controlled way, without going into layout systems that are covered elsewhere.
Plotting Multiple Lines in the Same Axes
The most direct way to show several data sets together is to call plot with multiple pairs of x and y values. MATLAB draws each pair as a separate line on the same axes.
For example, suppose you have a common x vector and three different y vectors.
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(2*x);
plot(x, y1, x, y2, x, y3);
In this single call to plot, MATLAB overlays three lines in one axes. The first pair x, y1 creates the first line, the second pair x, y2 creates the second, and so on. MATLAB automatically uses different colors in the default color order for each line.
You are not limited to two arguments. Any even number of arguments, interpreted as (x1, y1, x2, y2, x3, y3, ...), will produce that many lines in the same axes.
Holding the Current Plot with `hold on` and `hold off`
Sometimes you want more control and prefer to build the figure step by step. The hold command lets you add new plots to the existing axes without erasing what is already drawn.
By default, a new plot call clears the axes and replaces the existing content. To prevent this, use hold on.
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1); % First line
hold on; % Keep existing plot
plot(x, y2); % Second line, added to the same axes
hold off; % Return to default behavior
When hold on is active, new graphics objects are added. When you call hold off, MATLAB restores the original behavior where new plotting commands clear the axes before drawing.
You can also call hold with no arguments to toggle the state.
plot(x, y1);
hold; % Toggle: if it was off it turns on, if on it turns off
plot(x, y2); % May or may not overlay depending on state
It is usually clearer to use hold on and hold off explicitly so that anyone reading your code can see the intended behavior.
Combining Different Plotting Functions in One Axes
hold on allows you to mix different plotting functions in the same axes. For instance, you might want to draw a line and then highlight specific points using markers from another function.
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y); % Continuous line
hold on;
plot(x(1:5:end), y(1:5:end), 'ro'); % Red circles at every 5th point
hold off;
Here the first plot call creates the main curve. With hold on active, the second plot adds red circle markers at selected x values. You could similarly combine line plots with stem plots or area plots, as long as you apply hold on before the second command.
You can also mix plot with other graphics functions, for example yline or xline to show reference lines together with your data.
x = 0:0.1:10;
y = exp(-0.3*x).*sin(2*x);
plot(x, y);
hold on;
yline(0, '--k'); % Horizontal line at y = 0
hold off;Controlling Colors and Styles for Multiple Plots
When you create several lines in one axes, MATLAB assigns colors from its default color order. To distinguish lines clearly, you often want to choose colors and styles yourself.
You can specify line style and color as an additional argument in each plot call. For example:
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'r-'); % Red solid line
hold on;
plot(x, y2, 'b--'); % Blue dashed line
hold off;
The style string 'r-' means red solid line, while 'b--' means blue dashed line. You can also include markers, such as 'g:o' for a green dotted line with circle markers.
If you use one plot call with multiple (x, y) pairs, you can still control each line style individually by inserting style strings between the pairs.
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(2*x);
plot(x, y1, 'r-', x, y2, 'b--', x, y3, 'k:');In this example, the first line is red and solid, the second blue and dashed, and the third black and dotted. This method keeps all lines in a single call while giving you clear visual differences.
Plotting Against a Common x Without Repeating x
When several series share the same x values, MATLAB allows a shorthand where you supply one x argument followed by several y vectors. MATLAB then uses the same x for each y, provided their lengths match.
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(2*x);
plot(x, [y1; y2; y3]);
In this form, [y1; y2; y3] is a matrix where each row is one series. MATLAB interprets each column as a data point, so you get three lines corresponding to the three rows. In order for this to work, all y vectors must have the same length, and their orientation must match the way you form the matrix.
If your y data are column vectors, then stacking them as columns is more natural.
x = (0:0.1:2*pi)';
y1 = sin(x);
y2 = cos(x);
y3 = sin(2*x);
Y = [y1 y2 y3]; % 3 columns
plot(x, Y); % One line per column
Each column in Y becomes a line on the plot that shares the same x.
Adding New Axes to the Same Figure
Sometimes you want two separate sets of axes in the same figure, instead of overlaying everything in one axes. For example, you might want two independent plots that share space in the same window, but do not share the same axis limits or scales.
You can create a new axes object in the same figure at a specific position using the axes function with the 'Position' property. The position is specified as [left bottom width height] in normalized units between 0 and 1.
x = 0:0.1:10;
y1 = sin(x);
y2 = exp(-0.3*x);
figure;
ax1 = axes('Position', [0.1 0.1 0.35 0.8]);
plot(ax1, x, y1);
title(ax1, 'Sine');
ax2 = axes('Position', [0.55 0.1 0.35 0.8]);
plot(ax2, x, y2);
title(ax2, 'Exponential decay');
This code uses one figure with two separate axes side by side. Each plot call specifies which axes to draw in by passing ax1 or ax2 as the first argument. Each axes object has its own limits, labels, and title.
Although other layout methods exist, such as grid layouts, using axes directly gives you precise control over where each axes appears.
Overlaying Axes for Different Scales with `yyaxis`
It is sometimes useful to show two quantities that share a common x axis but have very different ranges of y values. In that case you can use a left and a right y axis in the same axes area. MATLAB provides the yyaxis command for this purpose.
The basic pattern is to switch the active side, then plot your data for each side.
x = 0:0.1:10;
y1 = sin(x); % Small values
y2 = 100*exp(-0.5*x); % Much larger scale
yyaxis left;
plot(x, y1);
ylabel('Sine');
yyaxis right;
plot(x, y2);
ylabel('Exponential');
xlabel('x');
title('Two scales in one plot');The left and right y scales are adjusted separately. This approach still uses a single x axis and keeps related information together while preserving visibility for both series.
Managing Axes and Figure State When Adding Plots
When adding multiple plots in one figure, it is important to be clear about which axes you are drawing into. If you create several axes explicitly and then call plot without specifying an axes, MATLAB uses the current axes. The current axes are typically the last one created or clicked in an interactive session.
To avoid confusion, especially in scripts, it is a good habit to store handles to axes in variables and pass them explicitly to plotting functions.
figure;
ax1 = axes('Position', [0.1 0.1 0.8 0.35]);
ax2 = axes('Position', [0.1 0.55 0.8 0.35]);
x = 0:0.1:10;
plot(ax1, x, sin(x));
plot(ax2, x, cos(x));
Here it is clear that the sine curve goes into ax1 and the cosine curve goes into ax2, regardless of which axes is current.
When you use hold on, it applies to the current axes only. If you have multiple axes in a figure and you want to add more plots to a particular one, either make that axes current by clicking on it in the figure window, or by calling axes(axHandle) before using hold on, or by using plot(axHandle, ...) consistently.