fplot with two y-axis
Show older comments
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
Accepted Answer
More Answers (2)
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:
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...
Categories
Find more on Graphics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
