Reputation: 245
I was wondering if there is way to put the count of a bin in a histogram/bar chart above the bin itself. I know that it is possible to change the xtick values to whatever you want (below the bin), however, I'd like to keep the xtick values as they are relevant to my plot. I guess it would be possible to use the text(x, y, z, label) function to place text on a plot, however, this requires you to manually set the location the text appears at and I'm preferably looking for a less tedious method.
Any suggestions would be great. Thanks
Upvotes: 2
Views: 132
Reputation: 36
Here is a semi-automated method. It uses a for loop and text
[H X] = hist(data,bins);
bin_width = X(2) - X(1);
x_offset = 4;
y_offset = 20;
figure();
bar_plot = bar(X, H)
for ii=1:length(H)
text(X(ii)-bin_width/x_offset, H(ii)+max(H)/y_offset, num2str(H(ii)) );
end
Then the only two parameters you need to play around with are x_offset and y_offset. Hope this helps.
Upvotes: 0