# How to create different Plots for different functions while each function has been plotted using hold on

9 views (last 30 days)
I created a code to calculate entropy and enthalpy for different pressures (which forms my x axis) at different temperatures.
I want to plot isotherms for enthalpy and using hold on, multiple isotherms in the same plot.
But using "hold on" not only clubs the different isotherms for enthalpy together in one plot, but also clubs entropy isotherms into the same plot too. Due to the difference in order of magnitude between the two, the entropy lines are simply not visible. Scaling enthalpy down by 10^3 has helped in making both of them visible but I want them to be in seperate graphs or plots.
%enthalpy plot (KJ/mol Vs Bar)-
hold on;
for t = 1:nt %varying temperature to create isotherms
for p = 1:np
enth(1, p) = 0.001*enthalpy(T(1, t), P(1, p), [T_ref, P_ref, H_ref], [A B], [a b c], R);
end
plot1 = plot(P, enth); %one plot for particular value of temperature
end
hold off
%entropy plot (J/K Vs Bar)-
hold on
for t = 1:nt
for p = 1:np
ent(1, p) = entropy(T(1, t), P(1, p), [T_ref, P_ref, S_ref], [A B], [a b c], R);
end
plot2 = plot(P, ent);
end
the indivisual functions of entropy and enthalpy are trivial, the long list of arguments is basically temperature and pressure that i want to calculate the function in plus the refernce states and constants.

dpb on 6 Sep 2022
%enthalpy plot (KJ/mol Vs Bar)-
hold on;
for t = 1:nt %varying temperature to create isotherms
for p = 1:np
enth(1, p) = enthalpy(T(1, t), P(1, p), [T_ref, P_ref, H_ref], [A B], [a b c], R);
end
plot1 = plot(P, enth); %one plot for particular value of temperature
end
figure
%entropy plot (J/K Vs Bar)-
hold on
for t = 1:nt
for p = 1:np
ent(1, p) = entropy(T(1, t), P(1, p), [T_ref, P_ref, S_ref], [A B], [a b c], R);
end
plot2 = plot(P, ent);
end
What you missed is simply creating a new figure for the second plot.
Or you could use tiledlayout and have two subplots instead.
As a stylistic and computing note; if the two functions enth and entropy were vectorized, then you could simply call them with the T,P arrays and get back the arrays of results eliminating the looping at the outer level.
Oh i didn't see the change lol. Thanks a ton ! exactly what i had in mind, didn't know it would be this simple.

Cris LaPierre on 6 Sep 2022
Perhaps what you need is to create a plot with two y axes, one for entropy and the other for enthalpy? If so, use yyaxis.
x = linspace(0,10);
y = sin(3*x);
yyaxis left
plot(x,y)
z = sin(3*x).*exp(0.5*x);
yyaxis right
plot(x,z)
ylim([-150 150])
##### 2 CommentsShowHide 1 older comment
Cris LaPierre on 6 Sep 2022
Then you need to create a new figure for each one before plotting. Use the figure command
x = linspace(0,10);
y = sin(3*x);
plot(x,y)
figure
z = sin(3*x).*exp(0.5*x);
plot(x,z)
ylim([-150 150])

### Categories

Find more on Graphics Performance in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!