Creating three shaded regions in a loglog plot

I'm trying to create three regions with different colors on a loglog plot. I've tried implementing the patch function but every iteration that I've tried to imlement just screws the entire figure. I've verified that there are no negative values so not sure what is going on. Any help would be much appreciated!
R = 8.31;
N = 6.022e23;
Dia_air = 3.5e-10;
P = 101325;
Kn_1 = 0.1;
Kn_2 = 10;
Dia_al = [1e-2,1e-1,1e0,1e1,1e2]*1e-6;
T_1= flip(Kn_1*(sqrt(2)*pi()*Dia_air^2*N*P.*Dia_al)/R);
T_2= flip(Kn_2*(sqrt(2)*pi()*Dia_air^2*N*P.*Dia_al)/R);
figure(1)
loglog(Dia_al/1e-6,T_1,'k--')
hold on
loglog(Dia_al/1e-6,T_2,'k')
xlim([1e-2 1e2])
ylim([1e1 1e5])
hold off

 Accepted Answer

Perhaps something like this —
R = 8.31;
N = 6.022e23;
Dia_air = 3.5e-10;
P = 101325;
Kn_1 = 0.1;
Kn_2 = 10;
Dia_al = [1e-2,1e-1,1e0,1e1,1e2]*1e-6;
T_1= flip(Kn_1*(sqrt(2)*pi()*Dia_air^2*N*P.*Dia_al)/R);
T_2= flip(Kn_2*(sqrt(2)*pi()*Dia_air^2*N*P.*Dia_al)/R);
figure(1)
loglog(Dia_al/1e-6,T_1,'k--')
hold on
loglog(Dia_al/1e-6,T_2,'k')
patch([Dia_al flip(Dia_al)]/1e-6, [ones(size(T_1))*min(T_1) flip(T_1)], 'r', 'EdgeColor','none')
patch([Dia_al flip(Dia_al)]/1e-6, [T_1 flip(T_2)], 'b', 'EdgeColor','none')
patch([Dia_al flip(Dia_al)]/1e-6, [T_2 ones(size(T_2))*max(ylim)], 'g', 'EdgeColor','none')
xlim([1e-2 1e2])
ylim([1e1 1e5])
hold off
The patch calls require a closed contour, and that can be provided by choosing the y-vectors apporopriately, since they all share the same x-vector.
EDIT — Added: 'EdgeColor','none' to each patch call.
Note that in earlier versions of MATLAB (I do not remember when that changed), patch does not work with logarithmic axis scales. In that event, use plot for the loglog calls, keep the patch calls as they are, and afterwards use:
set(gca, 'XScale','log', 'YScale','log')
.

More Answers (1)

If you're using release R2023a or later and you want to create horizontal or vertical regions, consider using the xregion and/or yregion functions.
semilogx(1:10, 1:10)
xregion(2, 3)
xregion(4, 7, FaceColor = "r")
yregion(5, 6, FaceColor = "b")
xticks(1:10)

Categories

Tags

Asked:

on 28 Mar 2024

Commented:

on 1 Apr 2024

Community Treasure Hunt

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

Start Hunting!