Hi,
I want to make a 3x1 subplot (1 column and 3 row) for 3 trigonometric graphs
y = sin(x)
y= cos(x)
y = tan(x)
for x = 1:0.1:50
What code to do this? Thank you.

답변 (2개)

Stephan
Stephan 2019년 10월 24일

0 개 추천

A = @(x) sin(x);
subplot(3,1,1)
fplot(A,[1 50])
B = @(x) cos(x);
subplot(3,1,2)
fplot(B,[1 50])
C = @(x) tan(x);
subplot(3,1,3)
fplot(C,[1 50])

댓글 수: 2

Yung Xi
Yung Xi 2019년 10월 24일
Thank you. What code I use to change title for each graph, axis label, and line color?
Steven Lord
Steven Lord 2019년 10월 24일
The Formatting and Annotation chapter in the documentation describes functions, tools, and techniques you can use to customize the appearance of your plots.

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

dpb
dpb 2026년 10월 31일 17:44
편집: dpb 2026년 11월 1일 17:12

0 개 추천

Another random, seemingly unrelated post brought this thread up again; another couple of ways with more recent tools as comparison...
First with the not-so-newfangled-any-more @doc:tiledlayout
funcs={'sin','cos','tan'}; % the desired functions
rnge=[1 50]; % and plotting range
hTL=tiledlayout(3,1); % create the tile layout in desired format
for f=funcs % iterate over each functions in turn
nexttile % create the new axes to plot into
hF=str2func(f{:}); % and the function to plot as handle
fplot(hF,rnge) % plot it
title(f) % label the plot function
ylabel('Magnitude') % each y axis
ylim;
end
xlabel('X') % one generic for x since all same...
Alternatively, since is a common x-axis, this would be an application for a stacked plot albeit one has to create the data explicitly instead of being able to use the functional form. That might be an enhancement; have to think about that a little...
YLIM=arrayfun(@(h)ylim(h),flip(hTL.Children),'UniformOutput',false);
YTIK=arrayfun(@(h)yticks(h),(hTL.Children),'UniformOutput',false);
figure
hF=cellfun(@str2func,funcs,'UniformOutput',false);
x=linspace(rnge(1),rnge(2),100);
y=array2table(cell2mat(cellfun(@(h)h(x).',hF,'UniformOutput',false)),'VariableNames',funcs);
hSP=stackedplot(y,'XLabel','X');
set(hSP.AxesProperties,{'YLimits'},YLIM) % make ylimits match
Unfortunately, the stacked plot default presentation isn't as pretty as what fplot() does on scaling so to make the two similar the above saved the ylim values from the first plot and applied them to the latter. I couldn't get the YTick values to match; it isn't an exposed property and handle-diving to the underlying axes showed the YLim values of [-1 1] for the top two but the tick labels wouldn't show up if set YTick to [-1 0 1] as in the tiled layout figure. All in all, not a pleasing result with stackedplot.

카테고리

도움말 센터 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

질문:

2019년 10월 24일

편집:

dpb
2026년 11월 1일 17:12

Community Treasure Hunt

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

Start Hunting!

Translated by