Clear Filters
Clear Filters

Plot a heatmap using small size data

31 views (last 30 days)
Raihan
Raihan on 25 Jul 2024
Answered: Jatin Singh on 6 Aug 2024 at 8:59
I have 20 x 3 data points: 10 different subjects with different heights and BMIs. Each subject has its own injury probability value.
I would like to know if it is possible to plot these into a heatmap. I tried to plot it but encountered NaN values at the boundaries.
I expect to plot something like this, which is from a previous study where the dataset is much larger.
If a heatmap is not possible, could you suggest an alternative way to visualize the relationship between these variables? Thank you.
  1 Comment
Umar
Umar on 25 Jul 2024
Hi Raihan,
Since you mentioned that you are having trouble with heatmap plot,consider creating a scatter plot matrix in Matlab to visualize the relationship between heights, BMIs, and injury probabilities without NaN issues. I hope this will help resolve your issue.

Sign in to comment.

Answers (1)

Jatin Singh
Jatin Singh on 6 Aug 2024 at 8:59
Hi,
The problem with encountering NaN values generally comes when the data size is small. In this scenario, to get a better visualization we might want a bigger data size.
Data interpolation is a technique that can be used to estimate values within the range of known data points. In MATLAB this can be done using the “griddata” method which is used to interpolate 2-D and 3-D data.
The “griddata” function returns a grid data which contains interpolated data using the known data points but can still contain NaN values which are out of the scope of the known data points, to fill these values we can use “fillmissing” function.
Kindly refer to the below documentations of “griddata” and “fillmissing” to get a better understanding:
As “heatmap” function is designed for 2D matrix data, “imagesc” function can be used to plot and visualize grid data in this case. Kindly look at the below example code to plot a heatmap with small data size and use of “griddata” and “fillmissing” for data interpolation.
% define dataset
height = [170, 180, 175, 160, 165, 155, 172, 168, 177, 182];
BMI = [22, 25, 24, 20, 23, 21, 26, 22, 27, 25];
prob = [10, 30, 20, 5, 15, 10, 35, 20, 40, 30];
% Define the grid for height and BMI
height_grid = linspace(min(height), max(height), 50);
BMI_grid = linspace(min(BMI), max(BMI), 50);
[H, B] = meshgrid(height_grid, BMI_grid);
% Interpolate the injury probability values onto the grid
prob_grid = griddata(height, BMI, prob, H, B, 'linear');
% Fill NaN values using linear interpolation
prob_grid = fillmissing(prob_grid, 'linear');
% Clip the interpolated values to be within the range [0, 1]
prob_grid = max(0.05, min(100, prob_grid));
% Create the heatmap using imagesc
figure;
imagesc([min(height), max(height)], [min(BMI), max(BMI)], prob_grid);
% Correct the Y-axis direction
set(gca, 'YDir', 'normal');
title('Probability Heatmap');
xlabel('Height(cm)');
ylabel('BMI(Kg/m^2)');
% Apply the custom colormap
colormap(jet);
colorbar;
You can also refer to below documentation of “imagesc” for more details:

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!