Colour coding based on the number of data points in each grid?

4 views (last 30 days)
Hi everyone,
I have created a grid map and place data points in different bins based on its x, y co-ordinates. I want to allot a red color for bins with less than 100 data points and blue color for bins with more than 100 data points.
May someone suggets me how can, I fix this.
Here is my script (if someone need data please let me know)
Thank you!
x = -130.05:0.01:-129.95;
y = 45.90:0.01:46.00;
n = length(x ) ;
m = length(y ) ;
[X,Y] = meshgrid(x,y ) ;
x = axil_long ; y = axil_lat ; z = time_axil(:,1) ;
plot(X,Y,'r',X',Y','r ')
hold on
for i = 1:m-1
for j = 1:n-1
P = [X(i,j) Y(i,j ) ;
X(i,j+1) Y(i,j+1 ) ;
X(i+1,j+1) Y(i+1,j+1 ) ;
X(i+1,j) Y(i+1,j )] ;
idx = inpolygon(x,y,P(:,1),P(:,2 )) ;
iwant = [ z(idx )] ;
plot(x(idx),y(idx ),'.', color='blue') % this line is tricky, may be we need some changes here
zStore{i,j} = z(idx);
end
end
axis([-130.05 -129.95 45.90 46.00])
Here, is my grid map.

Accepted Answer

Voss
Voss on 4 Jun 2022
You can use nnz to give you the number of points in each bin. Since you already have a logical index idx that tells you whether each point is within the current bin, then the number of points in that bin is nnz(idx). Check if nnz(idx) is greater than or equal to 100 (or greater than 100 if you like - depending on how you want the to color the points when there are exactly 100 points in the bin) or not, and color them blue or red accordingly.
Here it is with some random data:
axil_long = -130+0.02*randn(9000,1);
axil_lat = 45.95+0.02*randn(9000,1);
x = -130.05:0.01:-129.95;
y = 45.90:0.01:46.00;
n = length(x ) ;
m = length(y ) ;
[X,Y] = meshgrid(x,y ) ;
x = axil_long ; y = axil_lat ; % z = time_axil(:,1) ;
plot(X,Y,'r',X',Y','r ')
hold on
for i = 1:m-1
for j = 1:n-1
P = [X(i,j) Y(i,j ) ;
X(i,j+1) Y(i,j+1 ) ;
X(i+1,j+1) Y(i+1,j+1 ) ;
X(i+1,j) Y(i+1,j )] ;
idx = inpolygon(x,y,P(:,1),P(:,2 )) ;
% iwant = [ z(idx )] ;
if nnz(idx) >= 100
my_color = 'blue';
else
my_color = 'red';
end
plot(x(idx),y(idx ),'.', color=my_color)
% zStore{i,j} = z(idx);
end
end
axis([-130.05 -129.95 45.90 46.00])

More Answers (0)

Categories

Find more on Desktop in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!