highlighting a sections in a plot using patch

9 次查看(过去 30 天)
i have the following code and i get the result as shown in the image. I eant to shade all the way dwon to the line what i do wrong?
% Fit the lowess curve
ywf =fitresultLow(sort(xw));
% Determine where the function goes up and where it goes down
up_indices = find(diff(ywf) > 0);
down_indices = find(diff(ywf) < 0);
% Calculate regions where ywf is above a threshold (0.5 in this example)
threshold = 0.5;
up_regions = [];
down_regions = [];
if ~isempty(up_indices)
up_regions = [up_indices(1)]; % Initialize with the first up index
for i = 2:length(up_indices)
if up_indices(i) > up_indices(i-1) + 1
up_regions = [up_regions, up_indices(i)];
end
end
end
if ~isempty(down_indices)
down_regions = [down_indices(1)]; % Initialize with the first down index
for i = 2:length(down_indices)
if down_indices(i) > down_indices(i-1) + 1
down_regions = [down_regions, down_indices(i)];
end
end
end
% Sort regions and create shading
regions = sort([up_regions, down_regions]);
xw1 = sort(xw);
lower_y_coordinate = min(Nu);
figure;
plot(xw1, Nu);
hold on;
for k = 1:2:length(regions) - 1
xrng = regions(k):regions(k + 1);
patch([xw1(xrng), flip(xw1(xrng))], [Nu(xrng)' ones(size(Nu(xrng)')) * lower_y_coordinate], 'r', 'FaceAlpha', 0.5);
end
% Create a polygon to extend shading all the way down
fill([xw1(1), xw1(end), xw1(end), xw1(1)], [lower_y_coordinate, lower_y_coordinate, lower_y_coordinate, lower_y_coordinate], 'r', 'FaceAlpha', 0.5);
hold off;
grid on;

采纳的回答

ran
ran 2023-9-12
移动:Dyuman Joshi 2023-9-12
i fix it, all u need to do it creating vactors in the patch area
patch([xw1(xrng);flip(xw1(xrng))], [Nu(xrng);ones(size(Nu(xrng)')) * lower_y_coordinate]
the fill function can be removed

更多回答(0 个)

类别

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