MATLAB Answers


Axis numbers on histogram

Asked by John
on 7 Jan 2012

Hello there,

I'm trying to plot a histogram plot of departure times on the 24-hour time scale.

Here is an example of what I'm trying to achieve:

My question is: How can I get the x-axis to start at 5am and finish at 4am like it is in the example. When I plot it it goes from 0 - 24.

Here is an example of my plot

Also how could I plot the cumulative distribution over the histogram.

My code:

DepartureTimes = load('Departure Times.txt')
h = hist(DepartureTimes,24); 
h = h/sum(h); 
bar(h, 'DisplayName', 'Depature Times'); 

Many thanks for your help





No products are associated with this question.

2 Answers

Answer by Image Analyst
on 7 Jan 2012
 Accepted answer

% Demo to plot histogram and cdf of grayscale image.
% By ImageAnalyst
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
	% Didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist.', fullFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') 
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2); 
title('Histogram of image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;
% Compute the CDF
cdf = cumsum(pixelCount) / numel(grayImage);
% Plot both hist and cdf on same plot
hold on;
z = zeros(1,length(pixelCount));
plotyy(grayLevels, z, grayLevels, cdf);
title('Histogram and CDF of image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;


Answer by Image Analyst
on 7 Jan 2012

Did you try xlim()?


on 7 Jan 2012


Thank you, xlim() worked. Would you know how I could plot the cumulative distribution over the histogram with the scale on the right hand side of the chart as in the example? I'm having no luck :(

Appreciate any help


See full demo code below.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!