필터 지우기
필터 지우기

What is the logic in the way plots are handled in Live Scripts?

조회 수: 1 (최근 30일)
Henning Søgaard
Henning Søgaard 2023년 11월 21일
답변: Steven Lord 2023년 11월 21일
Example: I have the following code in a Live Script:
fplot(@sqrt,[0 10]), legend('sqrt')
subplot(2,1,1)
fplot(@sin,[0 2*pi]), legend('sin')
subplot(2,1,2)
fplot(@cos,[0 2*pi]), legend('cos')
fplot(@log,[0.1 10]), legend('log')
subplot(2,1,1)
fplot(@tan,[0 2*pi]), legend('tan')
subplot(2,1,2)
fplot(@cot,[0 2*pi]), ylim([-15 15]), legend('cot')
I expect the code to produce four figures (of which two of them are containing subplots), but I only get two figures (the first one and the last one):
What is the logic behind that?
I know that I can get my four figures by inserting the command figure as I have done in the following code, but why do I need to do something like this?
fplot(@sqrt,[0 10]), legend('sqrt')
figure
subplot(2,1,1)
fplot(@sin,[0 2*pi]), legend('sin')
subplot(2,1,2)
fplot(@cos,[0 2*pi]), legend('cos')
figure
fplot(@log,[0.1 10]), legend('log')
figure
subplot(2,1,1)
fplot(@tan,[0 2*pi]), legend('tan')
subplot(2,1,2)
fplot(@cot,[0 2*pi]), ylim([-15 15]), legend('cot')

채택된 답변

Steven Lord
Steven Lord 2023년 11월 21일
From the documentation for the subplot function: "subplot(m,n,p) divides the current figure into an m-by-n grid and creates axes in the position specified by p. MATLAB® numbers subplot positions by row. The first subplot is the first column of the first row, the second subplot is the second column of the first row, and so on. If axes exist in the specified position, then this command makes the axes the current axes."
So your second subplot(2, 1, 1) command makes that the current axes. It does not, as it sounds like you expected it to, create another figure with an axes on it. To do that you would need to call figure. Alternately if you want the sine and tangent curves to be on the same axes, call hold on before calling fplot the second time on the subplot(2, 1, 1) axes.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by