필터 지우기
필터 지우기

Plot Heatmap from distinct coordinates and data array of floats

조회 수: 21 (최근 30일)
Elinor Ginzburg
Elinor Ginzburg 2023년 8월 6일
댓글: Elinor Ginzburg 2023년 8월 13일
Hi,
I have an array of floats of size 200x2 where each row is the (x,y) coordinate of a specific point on the heatmap (x=column 1 and y=column 2). I also have an array of floats of size 1x200, which is the data I want to plot, I'll denote it as z. At each row of the coordinates array, the point (x,y) is associated with the value in z at the same index. I want to create a heatmap using this data.
I tried creating a meashgrid from the two columns of the coordinate array, but then I have this problem that z doesn't have the proper dimantions. I believe I need to create an array filed with zeros, and fill it with z values at the adequate coordinates, but than I have the problem that the coordinates are floats.
This is the code I used (which is of course problematic):
coords = load('grid_points.mat').array;
accuracies = load('test_accuracies.mat').array;
xcoord = coords(:,1);
ycoord = coords(:,2);
[X,Y] = meshgrid(xcoord,ycoord);
x = X(:); % Your varaible x should something like this as a column vector
y = Y(:); % Similar for variable y
z = accuracies(:); % Similar for variable z
tbl = table(x,y,z); % Combine them in a table
h = heatmap(tbl,'x','y','ColorVariable','z'); % Generate heat map
patial view of the coordinates array:
patial view of the data array:
I'd appreciate it if anyone could help me understand how can I overcome the fact that the coordinates array has floats in it (I don't want to convert it into integers, I want to keep it as floats if possible).
Thank you very much for your time and attention

채택된 답변

KSSV
KSSV 2023년 8월 6일
It depends on your data whether it is structured or unstructured. Let x, y, z be your column data vectors.
%%structured
xi = unique(x) ; yi = unique(y) ;
[X,Y] = meshgrid(xi,yi) ;
Z = reshape(z,size(X)) ;
figure
surf(X,Y,Z)
%%unstructured
dt = delaunayTriangulation(x,y) ;
tri = dt.ConnectivityList ;
figure
trisurf(tri,x,y,z)
Also have a look on griddata and scatteredInterpolant
  댓글 수: 5
KSSV
KSSV 2023년 8월 7일
load test_accuracies.mat
z = array' ;
load grid_points.mat ;
x = double(array(:,1)) ;
y = double(array(:,2)) ;
pgon = polyshape(x,y) ;
xp = pgon.Vertices(:,1) ;
yp = pgon.Vertices(:,2) ;
F = scatteredInterpolant(x,y,z) ;
zp = F(xp,yp) ;
x1 = x(1:100) ; y1 = y(1:100) ;
x2 = x(101:200) ; y2 = y(101:200) ;
xi = linspace(min(x),max(x),500) ;
yi = linspace(min(y),max(y),500) ;
[X,Y] = meshgrid(xi,yi) ;
idx = inpolygon(X,Y,xp,yp) ;
id = ~isnan(zp) ;
Z = griddata(xp(id),yp(id),zp(id),X,Y) ;
Z(~idx) = NaN ;
pcolor(X,Y,Z)
shading interp
Elinor Ginzburg
Elinor Ginzburg 2023년 8월 13일
I'm sorry for the late reply. Thank you so much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by