Retain current plot when adding new plots (2024)

Retain current plot when adding new plots

collapse all in page

Syntax

hold on

hold off

hold

hold(state)

hold(ax,___)

Description

example

hold on sets the axes hold state to on, which retains plots in the current axes so that new plots added to the axes do not delete existing plots. When the hold state is on, 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. If an axes object does not exist, then the hold command creates one.

example

hold off sets the axes hold state to off so that new plots added to the axes clear existing plots and reset all axes properties. When the hold state is off, 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 option is the default behavior.

hold toggles the hold state between on and off.

hold(state) sets the axes hold state by specifying a state value. You can specify state as "on", "off", a logical value, or an OnOffSwitchState value.

example

hold(ax,___) sets the hold state for the axes specified by ax instead of the current axes. Specify ax before all other arguments.

Examples

collapse all

Add Line Plot to Existing Axes

Open Live Script

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

x = linspace(-pi,pi);y1 = sin(x);plot(x,y1)hold ony2 = cos(x);plot(x,y2)hold off

Retain current plot when adding new plots (1)

When the hold state is off, new plots delete existing plots. New plots start from the beginning of the color order and line style order.

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

Retain current plot when adding new plots (2)

Specify Hold State for Specific Axes

Open Live Script

Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Plot a sine wave plot in each axes.

x = linspace(0,10);y1 = sin(x);y2 = cos(x);tiledlayout(2,1)% Top plotax1 = nexttile;plot(ax1,x,y1)% Bottom plotax2 = nexttile;plot(ax2,x,y2)

Retain current plot when adding new plots (3)

Add a second sine wave to the top axes.

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

Retain current plot when adding new plots (4)

Set the Hold State for Multiple Axes

Open Live Script

Create a 1-by-2 tiled chart layout. Call the nexttile function to create two axes objects and plot into the axes.

t = tiledlayout(1,2);ax1 = nexttile;ax2 = nexttile;plot(ax1,[0 1 0 1])scatter(ax2,rand(1,10),rand(1,10),'filled')

Retain current plot when adding new plots (5)

Set the hold state for both of the axes to 'on'. Then display additional data in each plot.

hold([ax1 ax2],'on')plot(ax1,[.5 .2 .5 .2])scatter(ax2,rand(1,10),rand(1,10),'filled')

Retain current plot when adding new plots (6)

Input Arguments

collapse all

stateAxes hold state
"on" | "off" | true or 1 | false or 0 | OnOffSwitchState value

Axes hold state, specified one of these values:

  • "on" or "off" — A value of "on" sets the hold state to on, and "off" sets the state to off. You can also specify the character vectors 'on' or 'off'.

  • Numeric or logical 1 (true) or 0 (false) — A value of 1 or true sets the hold state to on, and 0 or false sets the state to off. (since R2024a)

  • A matlab.lang.OnOffSwitchState value — A value of matlab.lang.OnOffSwitchState.on sets the hold state to on, and matlab.lang.OnOffSwitchState.off sets the state to off. (since R2024a)

axTarget axes
axes | array of axes

Target axes, specified as one of the following:

  • Any type of axes object: an Axes, PolarAxes, or GeographicAxes object.

  • An array of axes objects that belong to the same class. To determine the class, use the class function.

If you do not specify the axes, then hold sets the hold state for the current axes.

Tips

  • Use the ishold function to testthe hold state.

Algorithms

The hold function sets these properties:

  • NextPlot axes property - Sets this property of the associated Axes, PolarAxes, or GeographicAxes object to either 'add' or 'replace'.

  • NextPlot figure property - Sets this property of the Figure object to 'add'.

Version History

Introduced before R2006a

expand all

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

See Also

Functions

  • ishold | figure | tiledlayout | nexttile | axes | cla | newplot

Properties

  • Axes Properties | PolarAxes Properties

Classes

  • matlab.lang.OnOffSwitchState

Topics

  • Combine Multiple Plots

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Retain current plot when adding new plots (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Retain current plot when adding new plots (2024)

FAQs

How to keep a plot in MATLAB? ›

Use hold on to add a second line plot without deleting the existing line plot. The new plot uses the next color and line style based on the ColorOrder and LineStyleOrder properties of the axes. Then reset the hold state to off.

What does "hold on" mean in MATLAB? ›

hold on retains the current plot and certain axes properties so that subsequent graphing commands add to the existing graph. hold off resets axes properties to their defaults before drawing new plots. hold off is the default. hold toggles the hold state between adding to the graph and replacing the graph.

Which command is used to add a legend to a MATLAB plot? ›

Alternatively, you can specify the legend labels using the DisplayName property. Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend.

What is the legend function in MATLAB? ›

legend associates strings with the objects in the Axes in the same order that they are listed in the Axes Children property. By default, the legend annotates the current Axes. MATLAB displays only one legend per Axes. legend positions the legend based on a variety of factors, such as what objects the legend obscures.

How do you continuously update a plot in MATLAB? ›

Update Plot Using Data Linking

Create the variable x to represent the iteration number and y to represent the approximation. Plot the initial values of x and y . Turn on data linking using linkdata on so that the plot updates when the variables change. Then, update x and y in a for loop.

How to save plot data in MATLAB? ›

Save Plots Interactively

For example, create a bar chart. Save the chart to a file by hovering over the export button in the axes toolbar and selecting the first item in the drop-down list. MATLAB displays the Save As dialog box with the file type options.

What is hold on and off in MATLAB plot? ›

In short, hold on sets up the axes to add new objects to the axes while hold off sets up the axes to reset when new objects are added.

When to use hold off? ›

If you hold off doing something, you delay doing it or delay making a decision about it. The hospital staff held off taking Rosenbaum in for an X-ray. If you hold off a challenge in a race or competition, you do not allow someone to pass you.

What is the remain function in MATLAB? ›

Description. r = rem( a , b ) returns the remainder after division of a by b , where a is the dividend and b is the divisor. This function is often called the remainder operation, which can be expressed as r = a - b. *fix(a./b) .

How to give plot title in MATLAB? ›

Create a plot. Then create a title and a subtitle by calling the title function with two character vectors as arguments. Use the 'Color' name-value pair argument to customize the color for both lines of text. Specify two return arguments to store the text objects for the title and subtitle.

How do you plot without a legend in MATLAB? ›

One can suppress a legend entry for a line object h by executing h. HandleVisibility='off' or h. Annotation. LegendInformation.

How to plot points in MATLAB? ›

plot( points , ax ) plots points in the specified axis. plot( points , ax ,Name=Value) specifies options using one or more name-value arguments in addition to any combination of arguments from previous syntaxes. For example, plot(ShowOrientation=true) renders the orientation in the display plot.

What is the limit of legend in MATLAB? ›

Accepted Answer

Legends are currently limited to no more than 50 entries. Usually in plots with more than 50 features, the plot is so cluttered and the legend is so large that it is more advisable to select just a few key items to display in the legend.

How to manipulate legend in MATLAB? ›

To add a legend title, set the String property of the legend text object. To change the title appearance, such as the font style or color, set legend text properties. For a list, see Text Properties. plot(rand(3)); lgd = legend('line 1','line 2','line 3'); lgd.

Why do we use grid on in MATLAB? ›

grid on displays the major grid lines for the current axes returned by the gca command. Major grid lines extend from each tick mark. grid off removes all grid lines from the current axes or chart. grid toggles the visibility of the major grid lines.

How do you keep a variable in MATLAB? ›

To save variables to a MATLAB script, click the Save Workspace button or select the Save As option, and in the Save As window, set the Save as type option to MATLAB Script. Variables that cannot be saved to a script are saved to a MAT-file with the same name as that of the script.

How do I save figure properties in MATLAB? ›

saveas( fig , filename ) saves the figure or Simulink® block diagram specified by fig to file filename . Specify the file name as a character vector or string that includes a file extension, for example, 'myplot. jpg' .

How do you save a plot in MATLAB in high resolution? ›

To save a figure as an image at a specific resolution, call the exportgraphics function, and specify the 'Resolution' name-value pair argument. By default, images are saved at 150 dots per inch (DPI). For example, create a bar chart and get the current figure. Then save the figure as a 300-DPI PNG file.

How to plot a set of data in MATLAB? ›

Plotting Data Using MATLAB
  1. Step 1: Opening the Program. ...
  2. Step 2: Creating a Script File. ...
  3. Step 3: Beginning a Script File. ...
  4. Step 4: Creating or Importing Data. ...
  5. Step 5: Creating the Plot. ...
  6. Step 6: Adding Data Markers. ...
  7. Step 7: Adjusting the Marker Size. ...
  8. Step 8: Changing the Plot Color.

Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6044

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.