Creating three shaded regions in a loglog plot

조회 수: 13 (최근 30일)
Yoshi
Yoshi 2024년 3월 28일 20:08
댓글: Star Strider 2024년 4월 1일 14:10
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

채택된 답변

Star Strider
Star Strider 2024년 3월 28일 20:24
편집: Star Strider 2024년 3월 28일 20:33
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')
.
  댓글 수: 2
Yoshi
Yoshi 2024년 4월 1일 13:56
thank you!!
Star Strider
Star Strider 2024년 4월 1일 14:10
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2024년 3월 28일 20:25
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)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by