How to shade with upper and lower limits along a curve line? (see example in image below)
10 次查看(过去 30 天)
显示 更早的评论
采纳的回答
albara
2023-4-26
To shade the area between two curves in MATLAB, you can use the fill() function. Here is an example code that demonstrates how to do this:
% Define x and y values for the curves
x = linspace(0, 4*pi, 100);
y1 = sin(x);
y2 = cos(x);
% Define the upper and lower limits
y_upper = y1 + 0.5;
y_lower = y2 - 0.5;
% Plot the curves
plot(x, y1, 'b-', 'LineWidth', 2);
hold on;
plot(x, y2, 'r-', 'LineWidth', 2);
% Fill the area between the curves
fill([x fliplr(x)], [y_upper fliplr(y1)], 'b', 'FaceAlpha', 0.2);
fill([x fliplr(x)], [y_lower fliplr(y2)], 'r', 'FaceAlpha', 0.2);
% Add legend and axis labels
legend('y1', 'y2');
xlabel('x');
ylabel('y');
In this example, the fill() function is used twice to fill the area between the two curves. The fliplr() function is used to create a mirrored set of x values so that the area is filled in properly. The 'FaceAlpha' parameter is used to set the transparency of the filled area.
You can modify the y_upper and y_lower arrays to define your upper and lower limits for your specific application.
is this helps?
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
更多回答(1 个)
chicken vector
2023-4-26
N = 100;
x = linspace(0,2*pi,N);
y = 1 + cos(x);
yNoiseUp = y + 0.5 + .25*(sin(x) + .25*(rand(1,N) - 0.5));
yNoiseDown = y - 0.5 - .25*(sin(x) + .25*(rand(1,N) - 0.5));
figure;
grid on;
hold on;
fill([x flip(x)],[yNoiseUp flip(yNoiseDown)],[.8 .8 .8],'FaceAlpha',.7)
plot(x,y,'k','LineWidth',2);
plot(x,yNoiseUp,'k');
plot(x,yNoiseDown,'k');
hold off;
xlim([0,x(end)]);
xlabel('\theta_3 [deg]')
ylabel('C_{p,3}')
Result:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!