2D heatmap based on weight (not density)

조회 수: 34 (최근 30일)
Michaela Warnecke
Michaela Warnecke 2024년 10월 30일 4:03
댓글: Michaela Warnecke 2024년 10월 30일 13:50
I have an X, Y and X matrix, each N x M of size, where X indicates a location on the x axis, Y indicates the y data point and Z is the weight. Currently, I use scatter in order to plot X and Y with a weight of Z to indicate the "importance" of each X/Y data point.
I'd like to create something like a heatmap instead, where the x/y coordinates are colored, instead of changed in size, based on the Z weight. For example, large Z values could be red, small Z values could be green and the color vector linearly interpolates between red and green as Z changes.
The code below will create an example setup similar to what I am working with.
X = randi(100, [100 1]); X = sort(X);
Y = randi(50, [100 1]);
Z = linspace(100, 1, 100);
figure;
scatter(X, Y, Z, 'k', 'o', 'filled');
I could see one of 3 options:
  1. something like either "hotspots" where large circles are and decreasing intensity of hotspots as the circles get smaller
  2. "color spots" for which large circles might start out red and change in color from red to yellow to green as they get smaller
  3. something like a "segmentation" of the entire background of the plot into "red, yellow, green" zones (e.g. as Z decreases, certain thresholds indicate a change from red to yellow to green, so "patches" could indicate such changes). However, I have no idea how to draw the lines of the patch (e.g. where data points with Z = 90 and above lay could be red, 89 and below could be orange, ..), and maybe smooth them a bit.
Any help will be greatly appreciated.

채택된 답변

Shivam
Shivam 2024년 10월 30일 4:18
You can achieve the above 3 implementations using the scatter, imagesc and contourf functions respectively. You can have a look at each of the implementation below:
  • Colour spots using scatter
% Example data
X = randi(100, [100, 1]);
Y = randi(50, [100, 1]);
Z = linspace(100, 1, 100);
% Create the scatter plot
figure;
scatter(X, Y, 100, Z, 'filled'); % Use Z for color
% Set colormap from red to green
colormap(jet); % Or use other colormaps like 'hot', 'cool', etc.
colorbar; % Show color scale
  • Using imagesc for heatmap: This approach bins your data into a grid.
% Define grid size
x_edges = linspace(min(X), max(X), 50);
y_edges = linspace(min(Y), max(Y), 25);
% Create a 2D histogram
heatmap_data = zeros(length(y_edges)-1, length(x_edges)-1);
for i = 1:length(X)
x_idx = find(X(i) >= x_edges, 1, 'last');
y_idx = find(Y(i) >= y_edges, 1, 'last');
if x_idx < length(x_edges) && y_idx < length(y_edges)
heatmap_data(y_idx, x_idx) = heatmap_data(y_idx, x_idx) + Z(i);
end
end
% Plot the heatmap
figure;
imagesc(x_edges, y_edges, heatmap_data);
set(gca, 'YDir', 'normal'); % Correct the y-axis direction
colormap(jet); % Or any other colormap
colorbar;
  • Segmentation for contourf
% Create a grid for contour
[X_grid, Y_grid] = meshgrid(linspace(min(X), max(X), 100), linspace(min(Y), max(Y), 100));
% Interpolate Z values on the grid
Z_grid = griddata(X, Y, Z, X_grid, Y_grid, 'cubic');
Warning: Duplicate x-y data points detected: using average values for duplicate points.
% Plot the contour map
figure;
contourf(X_grid, Y_grid, Z_grid, 10, 'LineColor', 'none'); % 10 levels
colormap(jet);
colorbar;
You can adjust the number of levels in contourf to change how many segments are displayed.
I hope it helps.
Thanks
  댓글 수: 1
Michaela Warnecke
Michaela Warnecke 2024년 10월 30일 13:50
Thanks so much, this is exactly what I was looking for! It works splendidly.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by