Create a 2d coordinate system with areas separated by color

3 vues (au cours des 30 derniers jours)
Tomer Tuchner
Tomer Tuchner le 1 Fév 2020
Réponse apportée : Divyam le 18 Sep 2024
I want to categorize all the points in the range x=[0,1], y=[0,1], to two groups, and display these groups as painted areas in a 2d "graph".
I cannot express those groups as a function, they will be some binary relation and its complementary relation over the range.
General structure/pseudo code:
x = linspace(0,1,1000);
y = linspace(0,1,1000);
group1 = [];
group2 = [];
for i=1:1:length(x)
for j=1:1:length(y)
% check if (x,y) belongs to group1
if belongs_to_group1(x(i),y(j))
group1 = vertcat(group1, [x(i),y(j)]);
else
group2 = vertcat(group2, [x(i),y(j)]);
end
end
end
% plot colored categorization
plot(group1, 'red', group2, 'blue')
xlabel('x')
ylabel('y')
title('Red is group1, blue is group2')

Réponses (1)

Divyam
Divyam le 18 Sep 2024
After you determine the binary function to categorize the points, use the "filled" parameter of the "scatter" plot function to display these categorized group of points as painted areas.
% Define the grid of points
x = linspace(0, 1, 100);
y = linspace(0, 1, 100);
% Initialize groups
group1 = [];
group2 = [];
% Define the binary relation function
belongs_to_group1 = @(x, y) (x.^2 + (2*y).^2 < 0.2);
% Categorize points
for i = 1:length(x)
for j = 1:length(y)
if belongs_to_group1(x(i), y(j))
group1 = [group1; x(i), y(j)];
else
group2 = [group2; x(i), y(j)];
end
end
end
% Plot the categorized points
figure;
hold on;
% Plot group1 as red area
if ~isempty(group1)
scatter(group1(:,1), group1(:,2), 5, 'r', 'filled', 'DisplayName','Group1');
end
% Plot group2 as blue area
if ~isempty(group2)
scatter(group2(:,1), group2(:,2), 5, 'b', 'filled', 'DisplayName','Group2');
end
xlabel('x');
ylabel('y');
legend
hold off;
You can alternatively use the "fill" function for filling out the areas based on the binary function.
For more information regarding the "scatter" and "fill" functions, refer to the following documentation links:

Catégories

En savoir plus sur Line Plots 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