fplot with two y-axis

조회 수: 3 (최근 30일)
Angel Cuadras
Angel Cuadras 2024년 9월 20일
편집: dpb 2024년 9월 20일
I would like to fplot two curves Sp and St. Sp in the left Y-axis and the ST in the right Y-Axis. I can get the figure with two axis, I can plot the two curves independently but not together in a single graph. Is it possible?
I am using this code
figure (10)
hold on
fplot(Sp,[0.001 1000],"black")
yyaxis left
ylabel("S_{p}")
ylim ([0.000 0.04])
hold on
fplot(St,[0.001 1000],"--")
yyaxis right
ylabel("S_{t} (JK^{-1})")
ylim ([0.000 0.004])
xscale log
box on
xlabel("R_{2}/R_{1}")
hold off
Thank you very much

채택된 답변

Star Strider
Star Strider 2024년 9월 20일
편집: Star Strider 2024년 9월 20일
This seems to work —
Sp = @(t) (5+sin(2*pi*log(t)))/750;
St = @(t) (1+cos(1.5*pi*log(t)))/500;
figure (10)
yyaxis left
fplot(Sp,[0.001 1000],"black")
ylabel("S_{p}")
ylim ([0.000 0.04])
yyaxis right
fplot(St,[0.001 1000],"--")
ylabel("S_{t} (JK^{-1})")
ylim ([0.000 0.004])
xscale log
box on
xlabel("R_{2}/R_{1}")
hold off
.

추가 답변 (2개)

Shashi Kiran
Shashi Kiran 2024년 9월 20일
I understand that you want to plot two curves using yyaxis with fplot.
Based on the code you provided, here are some corrections made to achieve the correct result.
% Example functions
Sp = @(x) 0.02 * sin(log(x)) + 0.02;
St = @(x) 0.002 * cos(log(x)) + 0.002;
figure(10)
hold on
% Plot Sp using the left y-axis
yyaxis left
fplot(Sp, [0.001 1000], 'black')
ylabel("S_{p}")
ylim([0.000 0.04])
% Plot St using the right y-axis
yyaxis right
fplot(St, [0.001 1000], '--')
ylabel("S_{t} (JK^{-1})")
ylim([0.000 0.004])
% Common settings
set(gca, 'XScale', 'log') % Set x-axis to log scale
box on
xlabel("R_{2}/R_{1}")
hold off
Refer to the following documentations for more details about the functions:
  1. yyaxis: https://www.mathworks.com/help/matlab/ref/yyaxis.html?s_tid=doc_ta
  2. set: https://www.mathworks.com/help/matlab/ref/set.html?s_tid=doc_ta

dpb
dpb 2024년 9월 20일
편집: dpb 2024년 9월 20일
You forgot to define the two variables to plot, but presuming they were defined, then
yyaxis left
fplot(Sp,[0.001 1000],"black")
ylabel("S_{p}")
ylim ([0.000 0.04])
yyaxis right
fplot(St,[0.001 1000],"--")
ylabel("S_{t} (JK^{-1})")
ylim ([0.000 0.004])
xscale log
xlabel("R_{2}/R_{1}")
should work. You tried to set the axes targets after the fact, not before...

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by