plot function overwrites axes

20 次查看(过去 30 天)
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")

采纳的回答

Voss
Voss 2023-2-6
编辑:Voss 2023-2-6
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")
  2 个评论
Peter Dittrich
Peter Dittrich 2023-2-7
Thank you for your answer. Somehow the "line" command does not appear in the overview of the Matlab Plot types: types-of-matlab-plots.html. Super cool hint!

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-2-6
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 个评论
Peter Dittrich
Peter Dittrich 2023-2-7
Thank you for your answer! Is there a way to find out in the docs which functions are high- and which are low-level and what they do behind the scenes?
Only reading the docs: "plot(ax,___) displays the plot in the target axes." really made me believe, that the plot would be displayed in the target axes and not destroy the target axes if not otherwise specified.
Just in this second I found another solution: Setting
ax1.NextPlot = "add";
before plotting – which is of course the same what hold on does.
Walter Roberson
Walter Roberson 2023-2-7
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by