What's the difference between the functions plot
and line
in MATLAB? Are they doing the same thing?
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.
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.
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, theline
function does not callnewplot
before plotting and does not respect the value of theNextPlot
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With