Documentation Center

  • Trial Software
  • Product Updates


Retain current graph when adding new graphs


hold on
hold off
hold all


The hold function controls whether MATLAB® clears the current graph when you make subsequent calls to plotting functions (the default), or adds a new graph to the current graph.

hold on retains the current graph and adds another graph to it. MATLAB adjusts the axes limits, tick marks, and tick labels as necessary to display the full range of the added graph.

hold off resets hold state to the default behavior, in which MATLAB clears the existing graph and resets axes properties to their defaults before drawing new plots.

hold all holds the graph and the current line color and line style so that subsequent plotting commands do not reset the ColorOrder and LineStyleOrder property values to the beginning of the list. Plotting commands continue cycling through the predefined colors and line styles from where the last graph stopped in the list.

hold reverses the current hold state. If the hold state is currently on, then a hold command sets the state to off. Similarly, if the hold state is currently off, then a hold command sets the state to on.

hold(axes_handle,...) applies the hold to the axes identified by the handle axes_handle. If several axes objects exist in a figure window, each axes has its own hold state. hold also creates an axes if one does not exist.

Test the hold state using the ishold function.


Add Plot to Existing Graph

Plot the sine function. Then, set the hold state to on to retain the current graph. Add a cosine plot.

x = -pi:pi/20:pi;
y1 = sin(x);
y2 = cos(x);

hold on
hold off % reset hold state


hold toggles the NextPlot axes property between the add and replace.

hold on sets the NextPlot property of the current figure and axes to add. hold off sets the NextPlot property of the current axes to replace.


If the range of subsequently added data is much greater than the original data, the original graph can become difficult to see in one axes. In these cases, it is usually better to use two separate axes. See subplot to create multiple axes in one figure.

See Also

| | |

Was this topic helpful?