1

Is there a way to display the number of elements on each bar of an histogram in MATLAB?

ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
Maria
  • 35
  • 5
  • possible duplicate of [Labeling points in order in a plot](http://stackoverflow.com/questions/4140312/labeling-points-in-order-in-a-plot) – Shai Apr 22 '14 at 09:12
  • but this is about points. I want to display the number of elements of each bar on top of the bar. – Maria Apr 22 '14 at 09:48
  • The top of each bar is also a 2D point. – Shai Apr 22 '14 at 09:49
  • This is not really a duplicate of [Labeling points in order in a plot](http://stackoverflow.com/questions/4140312/labeling-points-in-order-in-a-plot), as labelling a histogram plot with `text` is a lot more of a hassle than labelling a scatter plot. – Sam Roberts Apr 22 '14 at 09:55
  • text command worked for me! Thanks – Maria Apr 22 '14 at 22:14

1 Answers1

0

On the toolbar of MATLAB plots, including a histogram plot, there's a button called Data Cursor (it looks like a blue curve with a black cross and a yellow table). Click this, and then select a bar in the histogram - it will add an explanatory label to the bar, including information about the bin count of the bar. You can pick this label up and drag it to other bars, or add additional labels to other bars.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • This works, but the label can not be placed in the center of the bar, only at the corners of the square – Maria Apr 22 '14 at 10:29
  • Really? For me, the label is placed in the exact centre of the top of whichever bar I select. – Sam Roberts Apr 22 '14 at 10:32
  • can you send me the code of "edit text update Function"? You will find it by right clicking on a point – Maria Apr 22 '14 at 11:27
  • Sure. `function output_txt = myfunction(obj,event_obj) % Display the position of the data cursor % obj Currently not used (empty) % event_obj Handle to event object % output_txt Data cursor text string (string or cell array of strings). pos = get(event_obj,'Position'); output_txt = {['X: ',num2str(pos(1),4)],... ['Y: ',num2str(pos(2),4)]}; % If there is a Z-coordinate in the position, display it as well if length(pos) > 2 output_txt{end+1} = ['Z: ',num2str(pos(3),4)]; end` – Sam Roberts Apr 22 '14 at 12:00
  • Yes, same for me, but at my histogram, it displays a box with the coordinates, not in the center – Maria Apr 22 '14 at 16:39