plot function overwrites axes

조회 수: 22 (최근 30일)
Peter Dittrich
Peter Dittrich 2023년 2월 6일
댓글: Walter Roberson 2023년 2월 7일
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!
Voss
Voss 2023년 2월 7일
You're welcome!
This page lists the low-level graphics functions:

댓글을 달려면 로그인하십시오.

추가 답변 (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