Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the MATLAB functions plot and line differ?

Tags:

plot

matlab

line

What's the difference between the functions plot and line in MATLAB? Are they doing the same thing?

like image 336
Tim Avatar asked Mar 15 '10 12:03

Tim


People also ask

How do you plot different lines in Matlab?

Plot Multiple Lines By default, MATLAB clears the figure before each plotting command. Use the figure command to open a new figure window. You can plot multiple lines using the hold on command. Until you use hold off or close the window, all plots appear in the current figure window.

How do you plot a function in Matlab?

plot( X , Y ) creates a 2-D line plot of the data in Y versus the corresponding values in X . To plot a set of coordinates connected by line segments, specify X and Y as vectors of the same length. To plot multiple sets of coordinates on the same set of axes, specify at least one of X or Y as a matrix.


1 Answers

The functions plot and line do nearly the same thing, but plot is a high-level function that may have more interaction with other graphics objects. A brief summary of high-level and low-level functions can be found here. High-level functions like plot are likely internally calling primitive functions like line to create their graphics, but they can also modify or interact with properties of their parent axes or figure. From the documentation for line:

Unlike the plot function, the line function does not call newplot before plotting and does not respect the value of the NextPlot property for the figure or axes. It simply adds the line to the current axes without deleting other graphics objects or resetting axes properties. However, some axes properties, such as the axis limits, can update to accommodate the line.

For example, if you call the line function:

line('XData', x, 'YData', y, 'ZData', z, 'Color', 'r');

MATLAB draws a red line in the current axes using the specified data values. If there is no axes, MATLAB creates one. If there is no figure window in which to create the axes, MATLAB creates it as well.

If you call the line function a second time, MATLAB draws the second line in the current axes without erasing the first line. This behavior is different from high-level functions like plot that delete graphics objects and reset all axes properties (except Position and Units). You can change the behavior of high-level functions by using the hold command or by changing the setting of the axes NextPlot property.

The plot and line functions also differently affect automatic line coloring, as show here.

like image 150
gnovice Avatar answered Sep 20 '22 09:09

gnovice