# Creating three shaded regions in a loglog plot

2 ビュー (過去 30 日間)
Yoshi 2024 年 3 月 28 日
コメント済み: Star Strider 2024 年 4 月 1 日
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 2024 年 3 月 28 日

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 2024 年 4 月 1 日
thank you!!
Star Strider 2024 年 4 月 1 日
As always, my pleasure!

サインインしてコメントする。

### その他の回答 (1 件)

Steven Lord 2024 年 3 月 28 日
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)
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Yoshi 2024 年 4 月 1 日
thank you!

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by