Axis numbers on histogram

7 views (last 30 days)
John
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: http://img593.imageshack.us/img593/3032/depaturetime.jpg
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.
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');
legend('show');
Many thanks for your help
John

Accepted Answer

Image Analyst
Image Analyst on 7 Jan 2012
% 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);
uiwait(warndlg(errorMessage));
return;
end
end
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);
bar(pixelCount);
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
subplot(2,2,3);
bar(pixelCount);
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;

More Answers (1)

Image Analyst
Image Analyst on 7 Jan 2012
Did you try xlim()?
  2 Comments
John
John on 7 Jan 2012
Hello,
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 :(
http://img593.imageshack.us/img593/3032/depaturetime.jpg
Appreciate any help
John
Image Analyst
Image Analyst on 7 Jan 2012
See full demo code below.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!