How can I add a summation column to a heatmap?

2 views (last 30 days)
Hi everyone,
I am trying to make a plot with bins of wave heights on one axis and wave periods on the other, with each bin colored by its percent occurrence over a period of time in my dataset. I was able to "trick" MATLAB's heatmap function into doing this with a dummy variable for the Color Data, but I have not been able to figure out how to add a row and column summing up the percentages of each respective row and column. Below is the figure that I have made:
What I would like to do is something like the following (which I think is just a table in Excel):
Note the SUM [%] row and column; I would like to add these to the former plot. I have looked through the documentation for the heatmap function and can't seem to find anything. Let me know if you have any ideas! Thanks.

Accepted Answer

Voss
Voss on 13 Dec 2022
Augment your matrix with the row and column sums before sending it to heatmap. Then set the color-limits to the range of your original matrix.
data = magic(5);
data_limits = [min(data(:)) max(data(:))];
data(end+1,:) = sum(data,1);
data(:,end+1) = sum(data,2);
heatmap(data);
caxis(data_limits) % set the color-limits
You may get some variation in the color in the sum column and sum row, in general.
  4 Comments
Chase
Chase on 15 Dec 2022
Thanks, I didn't know about histcounts2! I ended up using groupcounts, which did basically the same thing.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!