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

18 ビュー (過去 30 日間)
Sim
Sim 2024 年 6 月 10 日
コメント済み: Sim 2024 年 6 月 10 日
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:

採用された回答

Mann Baidi
Mann Baidi 2024 年 6 月 10 日
編集済み: Mann Baidi 2024 年 6 月 10 日
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);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTwo y-axis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by