Histogram Styles

Submitted by:Tim Kelly

Date added:03 June, 2013

Category: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.

Tags: histogram , styles

Code Snippet:

    %==========================================================
% Plots a histogram
% + Frequency labels
% + Bin edges
%==========================================================

% Number of bins
binNumbers = 10;
% Load in data (single column data file)
data = load('data.out');

% Close any graphs currently open
close all;

% Plot histogram
hist(data,binNumbers);

% Run hist function again to grab bins
[n,xout] = hist(data,binNumbers);

% Set length of bins
binLength = length(xout);

% Make graph fullscreen
set(gcf, 'Position', get(0,'Screensize'));

% Graph labels
xlabel('Bins');
ylabel('Count');
title('Histogram');

% Y-Offset value. Change accordingly
offsetY = 33;
% Put counts over the bars.
for bin = 1 : binLength
caption = sprintf('%d', n(bin));
text(xout(bin), n(bin)+offsetY, caption, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'center');
end

% Bin edge variables
max = max(data);
min = min(data);
width = ( max - min ) / binNumbers;
ticks = min:width:max;

% Set new intervals
set(gca,'XTick', ticks);
 
 

Comments