Posted By

gobbledygook88 on 01/21/11


Tagged

histogram


Versions (?)

Histogram Styles


 / Published in: MatLab
 

Plots a histogram from an external file with the frequency of each bin shown above each bar. The XTick is also the intervals of the histogram.

  1. %==========================================================
  2. % Plots a histogram
  3. % + Frequency labels
  4. % + Bin edges
  5. %==========================================================
  6.  
  7. % Number of bins
  8. binNumbers = 10;
  9. % Load in data (single column data file)
  10. data = load('data.out');
  11.  
  12. % Close any graphs currently open
  13.  
  14. % Plot histogram
  15. hist(data,binNumbers);
  16.  
  17. % Run hist function again to grab bins
  18. [n,xout] = hist(data,binNumbers);
  19.  
  20. % Set length of bins
  21. binLength = length(xout);
  22.  
  23. % Make graph fullscreen
  24. set(gcf, 'Position', get(0,'Screensize'));
  25.  
  26. % Graph labels
  27. xlabel('Bins');
  28. ylabel('Count');
  29. title('Histogram');
  30.  
  31. % Y-Offset value. Change accordingly
  32. offsetY = 33;
  33. % Put counts over the bars.
  34. for bin = 1 : binLength
  35. caption = sprintf('%d', n(bin));
  36. text(xout(bin), n(bin)+offsetY, caption, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'center');
  37. end
  38.  
  39. % Bin edge variables
  40. max = max(data);
  41. min = min(data);
  42. width = ( max - min ) / binNumbers;
  43. ticks = min:width:max;
  44.  
  45. % Set new intervals
  46. set(gca,'XTick', ticks);

Report this snippet  

You need to login to post a comment.