Tim
Tim

Reputation: 99428

How do the MATLAB functions plot and line differ?

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

Upvotes: 13

Views: 15408

Answers (2)

gnovice
gnovice

Reputation: 125864

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.

Upvotes: 18

High Performance Mark
High Performance Mark

Reputation: 78324

plot() is used to create a graphic, usually a line graph of some sort. line() creates a lin object which may appear in, say, a graphic. No they're not doing the same thing. I'd usually use plot for creating a graphic, line for adding lines to an existing graphic.

If this doesn't answer your question, have a look at the documentation which covers these matters in great detail.

Upvotes: 2

Related Questions