Shaded region inside plot

조회 수: 9 (최근 30일)
LIM MING HUI
LIM MING HUI 2022년 4월 30일
댓글: Voss 2022년 4월 30일
Hello, I want to ask is it possible to shade thses 4 region with different colour?
x=[0:0.001:20];
y=(1/(2*0.2*9.81))*x.^2;
z=x*(10*3.7/(((3)^(1/2))*0.2*9.81))^(1/2);
plot(x,y,'m-', 'LineWidth', 3);
hold on;
plot(x,z,'k-', 'LineWidth', 3);
hold on;
x2 = [x,fliplr(x)];
y2 = [y,fliplr(z)];
fill(x2,y2,'b','LineStyle','none');

채택된 답변

Voss
Voss 2022년 4월 30일
x=[0:0.001:20];
y=(1/(2*0.2*9.81))*x.^2;
z=x*(10*3.7/(((3)^(1/2))*0.2*9.81))^(1/2);
colors = [ ...
1 1 0; ... % 1: yellow
0 1 0; ... % 2: green
0 1 1; ... % 3: cyan
0.5 0.5 0.5]; % 4: grey
idx = find(y > z,1);
fill( ...
[x x(end) x(1)], ...
[z(1:idx) y(idx+1:end) 120 120], ...
colors(1,:),'LineStyle','none');
hold on
fill( ...
[x(1:idx-1) x(idx-1:-1:1)], ...
[z(1:idx-1) y(idx-1:-1:1)], ...
colors(2,:),'LineStyle','none');
fill( ...
[x(idx:end) x(end:-1:idx)], ...
[y(idx:end) z(end:-1:idx)], ...
colors(3,:),'LineStyle','none');
fill( ...
[x x(end) x(1)], ...
[y(1:idx) z(idx+1:end) 0 0], ...
colors(4,:),'LineStyle','none');
% plot the y and z lines last so they aren't obscured by the fills
plot(x,y,'m-', 'LineWidth', 3);
plot(x,z,'k-', 'LineWidth', 3);
  댓글 수: 2
LIM MING HUI
LIM MING HUI 2022년 4월 30일
Thanks for your help!
Voss
Voss 2022년 4월 30일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by