How to set the x axis on heatmap

Hi all, I'm trying to plot a heatmap, but I'm not able to set up the x axis properly.
I have a data set that I run through this code to obtain a 2D histogram (that's what I'm aiming for)
[M,N]=size(data);
xbin=[0:0.2:8];
[counts,l]=histcounts(data(1,:),xbin);
result=counts;
for i=2:M
[counts,l]=histcounts(data(i,:),xbin);
result=[result; counts];
end
Then I try to plot the heatmap with this
ybin=[1:31];
heatmap(result,'Xdata',xbin,'YData',ybin);
colormap jet
But I get the error that lenght of xbin doesn't match the lenght of result.
I know that the error it's because xbin values are edges and not centers, so I end up having 1 extra value on the xbin var. Do you know how I could set up the edges value on the x axis? Thanks.
A workaround could be using hist instead of histcounts, so the values of xbin will be centers and not edges, but I'd like to avoid this solution.

 Accepted Answer

You know the problem, you just have to decide what approach you want take. It's easy enough to use indexing to determine the points to use
heatmap(result,'Xdata',xbin(1:end-1),'YData',ybin);
If you do want to use the center point, try something like this
midpt = movmean(xbin,2,'Endpoints','discard');
heatmap(result,'Xdata',midpt,'YData',ybin);

5 Comments

Thank you for your answer, but isn't (1:end-1) just removing the last value from xbin? Heatmap will still treat those values as center points, so basically I'll have my x axis shifted to the right by half bin lenght.
This is what I get
And this is what I'm looking for
Ah, now I see. Unfortunately, heatmaps don't have an XTick setting. You could use the second option I gave you above, which computes the centerpoint of the bin. The only other way I can think to label the edges would be to use histogram2. You'd have to set the edges on both X and Y axes (one extra point in both x and ybin). Also, (0,0) is the bottom left corner.
xbin=[0:0.2:8];
ybin = [1:32];
h = histogram2("XBinEdges",xbin,"YBinEdges",ybin,"BinCounts",flipud(result)');
h.DisplayStyle = 'tile';
h.ShowEmptyBins = 'on';
set(gca,'XTick',xbin,'YTick',ybin);
colormap jet
view(2)
colorbar
Thank you, that does the job perfectly. I've been trying to use histogram2, but with no results, probably because I didn't understand very well the input data and I constantly got an error message. That's why I ended up with heatmap, because was the only fuction that was giving me something close to what I was looking for.
I appreciate it, thanks.
One last comment then. I'm not sure if your y-values are important, but if you want them to match what you were getting with heatmap, use axis ij. The benefit is you don't have to use flipud.
...
h = histogram2("XBinEdges",xbin,"YBinEdges",ybin,"BinCounts",result');
axis ij
...
Yes, they are important. Thank you for the suggestion.

Sign in to comment.

More Answers (1)

[M,N]=size(data);
xbin=[0:0.2:8];
[counts,l]=histcounts(data(1,:), [xbin, inf]);
result=counts;
for i=2:M
[counts,l]=histcounts(data(i,:), [xbin, inf]);
result=[result; counts];
end

Categories

Community Treasure Hunt

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

Start Hunting!