Plot a rectangular/square box around a set of random points

19 vues (au cours des 30 derniers jours)
Sim
Sim le 10 Juin 2024
Commenté : Sim le 10 Juin 2024
Given a random set of points:
x=rand(1,100)*5;
y=rand(1,100)*5;
scatter(x,y,20,'blue','filled')
xlim([-1 6])
ylim([-1 6])
how can I draw a rectangle that wraps that set of points, and such that the following conditions are fulfilled?
(1) the upper edge and the lower edge of the rectangle are placed at the lowest and highest y-coordinates of the set of points, i.e. at
yl = [min(y) max(y)];
(2) the left and the right edges are placed at the leftmost and rightmost x-coordinates of the set of points, i.e.
xl = [min(x) max(x)];
Visually, my desired output would be the following one:

Réponse acceptée

Mann Baidi
Mann Baidi le 10 Juin 2024
Modifié(e) : Mann Baidi le 10 Juin 2024
Hi,
You can plot lines around the random points that will wrap all the points using the following line of code:
% Generate random points
numPoints = 50; % Number of random points
x = rand(1, numPoints) * 10; % Random x coordinates between 0 and 10
y = rand(1, numPoints) * 10; % Random y coordinates between 0 and 10
% Determine the min and max coordinates
xmin = min(x);
xmax = max(x);
ymin = min(y);
ymax = max(y);
% Create coordinates for the rectangular box
box_x = [xmin, xmax, xmax, xmin, xmin];
box_y = [ymin, ymin, ymax, ymax, ymin];
% Plot the points
figure;
scatter(x, y, 'filled');
hold on;
% Plot the rectangular box
plot(box_x, box_y, 'r-', 'LineWidth', 2);

Plus de réponses (0)

Catégories

En savoir plus sur Two y-axis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by