hold

Retain current plot when adding new plots

hold all will be removed in a future release. Use hold on instead.

Syntax

  • hold on example
  • hold off
  • hold all
  • hold
  • hold(ax,'on') example
  • hold(ax,'off')
  • hold(ax)

Description

example

hold on retains plots in the current axes so that new plots added to the axes do not delete existing plots. New plots use the next colors and line styles based on the ColorOrder and LineStyleOrder properties of the axes. MATLAB® adjusts axes limits, tick marks, and tick labels to display the full range of data.

hold off sets the hold state to off so that new plots added to the axes clear existing plots and reset all axes properties. The next plot added to the axes uses the first color and line style based on the ColorOrder and LineStyleOrder properties of the axes. This is the default behavior.

hold all is the same as hold on. This syntax will be removed in a future release. Use hold on instead.

hold toggles the hold state between on and off.

example

hold(ax,'on') retains plots in the axes specified by ax so that new plots added to the axes do not delete existing plots. New plots use the next colors and line styles based on the ColorOrder and LineStyleOrder properties of the axes. MATLAB adjusts axes limits, tick marks, and tick labels to display the full range of data. You can specify a different hold state for each axes.

hold(ax,'off') sets the hold state to off so that new plots added to the axes clear existing plots and reset all axes properties. The next plot added to the axes uses the first color and line style based on the ColorOrder and LineStyleOrder properties of the axes.

hold(ax) toggles the hold state between on and off.

Examples

expand all

Add Line Plot to Existing Graph

Create a line plot. Then, use hold on to add a second line plot without deleting the existing plot. The new plot uses the next color and line style based on the ColorOrder and LineStyleOrder properties of the axes.

x = linspace(-pi,pi);
y1 = sin(x);
y2 = cos(x);

plot(x,y1)
hold on
plot(x,y2)

Reset the hold state to off so that new plots delete existing plots. New plots start from the beginning of the color order and line style order.

hold off
y3 = sin(2*x);
plot(x,y3)

Specify Hold State for Specific Axes

Create a figure with two subplots and add a line plot to each subplot.

ax1 = subplot(2,1,1);
x = linspace(0,2*pi);
y1 = sin(x);
plot(x,y1)

ax2 = subplot(2,1,2);
y2 = cos(x);
plot(x,y2)

Add a second line plot to the upper subplot.

hold(ax1,'on')
y3 = sin(2*x);
plot(ax1,x,y3)

Reset the hold state to off for the upper subplot so that new plots replace the existing plots, which is the default behavior.

hold(ax1,'off')
y4 = sin(4*x);
plot(ax1,x,y4)

Input Arguments

expand all

ax — Axes objectaxes object

Axes object. Use ax to set the hold state for a specific axes, instead of the current axes.

More About

expand all

Tips

  • Use the ishold function to test the hold state.

  • The hold function toggles the NextPlot property of the axes between 'add' and 'replace'.

  • If an axes does not exist, then the hold command creates one.

See Also

| | | | |

Was this topic helpful?