fplot with two y-axis

2 次查看(过去 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