plot function overwrites axes
24 views (last 30 days)
Show older comments
When specifying the axes for a plot, I assumed one can create a figure-handle, axes-handle, and then plot to an axes.
Can someone please explain to me, why the plot command seems to overwrite ax1?
% hold(ax1, "on")
before the plot command would solve the problem. Is it really neccessary to hold on the axes, specified in the plot command?
fig1 = figure(1);
ax1 = axes("Parent",fig1);
axis(ax1, [-1 1 -1 1]);
ax1.XAxisLocation = "origin";
ax1.YAxisLocation = "origin";
x = [-1,2,3,4];
y = x.*2;
plot(ax1,x,y,"r-x")
0 Comments
Accepted Answer
Voss
on 6 Feb 2023
Edited: Voss
on 6 Feb 2023
If you haven't called hold on (or otherwise set the 'NextPlot' axes property, which is what hold does), then plot() does other things besides just creating a line (or lines) in an axes, including setting the axes x- and y-limits, deleting existing lines from the axes, and (apparently) setting the XAxisLocation and YAxisLocation. As you point out, using hold(ax1, "on") before the plot command avoids changing those settings. So that's the answer to your first question (Why does plot reset the axes?): plot() without hold on does other stuff.
To answer your second question (Is hold on really necessary?) Not if you don't use plot() at all. In a situation where you set up your axes before creating any lines, it's probably more convenient to use line() instead of plot(). line() is a low-level function that just creates a line and doesn't change any axes properties. (line() doesn't allow the same set of input arguments that plot() allows for specifying line properties, so you have to set them using property/value pairs.)
fig1 = figure(1);
ax1 = axes("Parent",fig1);
axis(ax1, [-1 1 -1 1]);
ax1.XAxisLocation = "origin";
ax1.YAxisLocation = "origin";
x = [-1,2,3,4];
y = x.*2;
line(ax1,x,y,"Color","r","LineStyle","-","Marker","x")
More Answers (1)
Walter Roberson
on 6 Feb 2023
Unless you have "hold on" (or the internal equivalent property) then each time you use a "high-level" graphics call, MATLAB does a cla() on the axes if it already exists.
You either need to hold on or else set the axes properties after you plot()
2 Comments
Walter Roberson
on 7 Feb 2023
It used to be relatively easy to distinguish between "high-level" or not: you looked at the types of the objects returned by plotting routines, and those were always in terms of low-level objects.
In the past, plotting routines used to build everything out of line() objects, surface() objects, patch() objects, text() objects, scatter() objects, and image() objects. Higher level functions included functions such as plot() and surf() and contour(); those would go through code that initialized the axes if NextPlot was 'replace' or 'replace'. patch() was an exception in that it was both high level and low level: if you called it passing in matrices of data then it was high level, but if you only passed in properties using name/value pairs then it was low-level.
These days, there are a lot of specialized graphics objects with dubious justification, and it is not always clear whether they are high-level or low-level.
See Also
Categories
Find more on Specifying Target for Graphics Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!