plot function overwrites axes
조회 수: 22 (최근 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")
댓글 수: 0
채택된 답변
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")
추가 답변 (1개)
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
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 Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

