Colour coding based on the number of data points in each grid?
1 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1021270/image.png)
0 个评论
采纳的回答
Voss
2022-6-4
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])
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Network Parameters 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!