Combine Multiple Plots
You can combine plots in several ways. Combine plots in the same axes, or create multiple axes in a figure.
Combine Plots in Same Axes
Use thehold on
command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off.
x = linspace(0,10,50); y1 = sin(x); figure plot(x,y1) title('Combine Plots') holdony2 = sin(x/2); plot(x,y2) y3 = 2*sin(x); scatter(x,y3) holdoff
When the hold state is on, the plots cycle through colors and lines styles based on theColorOrder
andLineStyleOrder
properties of the axes. Also, new plots do not reset axes properties, such as the title or axis labels. However, the axes limits and tick values can adjust to accommodate new data.
When the hold state is off, the next new plot clears existing plots and resets axes properties, such as the title.
Create Multiple Axes in Figure Using Subplots
Create multiple axes in a single figure using thesubplot
function, which divides the figure into a grid of subplots.
For example, create two stacked subplots and assign theAxes
objects to the variablesax1
andax2
. Add a plot, title, and axis labels to each subplot. Specify theAxes
objects as the first input arguments to each graphics function to ensure that the function targets the correct axes.
figure ax1 = subplot(2,1,1); x = linspace(0,10,50); y1 = sin(2*x); plot(ax1,x,y1) title(ax1,'Subplot 1') ylabel(ax1,'Values from -1 to 1') ax2 = subplot(2,1,2); y2 = rand(50,1); scatter(ax2,x,y2) title(ax2,'Subplot 2') ylabel(ax2,'Values from 0 to 1')
Add Super Title to Figure with Subplots
When you create a figure with subplots, you might want to add a title that applies to all the subplots. You can create the appearance of a super title by creating the subplots in a panel and adding a title to the panel.
Create a panel inside a figure. Specify a title for the panel and adjust some of the font properties.
f = figure; p = uipanel('Parent',f,'BorderType','none'); p.Title ='My Super Title'; p.TitlePosition ='centertop'; p.FontSize = 12; p.FontWeight ='bold';
Create two subplots in the panel by setting theParent
parameter to the panel object. Title each individual subplot.
subplot(1,2,1,'Parent',p) x = linspace(0,10,50); y1 = sin(2*x); plot(x,y1) title('Subplot 1') subplot(1,2,2,'Parent',p) y2 = rand(50,1); scatter(x,y2) title('Subplot 2')