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 Center および File ExchangeGraphics Object Properties についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by