Histogram

21 views (last 30 days)
Irene
Irene on 13 Sep 2011
Hi, I am trying to make a histogram plot using 2 variables and the resulting frequency should be plotted as a bar graph. I tried using the bar function with the following sample code:
x=rand(10,1); y=rand(10,1); bar(x,y,'hist');
I wanted to bin the values in my x-axis. I don't have any idea on how to do it. Your suggestions will be most appreciated.
Thanks, Irene

Accepted Answer

Image Analyst
Image Analyst on 13 Sep 2011
What you've put doesn't really make sense. See if this is what you want. It computes the histograms of x and y and then plots them.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
x=rand(10,1);
y=rand(10,1);
% Plot the original x data.
subplot(2, 2, 1);
bar(x);
xlabel('Index of X', 'fontSize', fontSize);
ylabel('Value of X', 'fontSize', fontSize);
title('X as a function of Index', 'fontSize', fontSize);
% Plot the original y data.
subplot(2, 2, 2);
bar(y);
xlabel('Index of Y', 'fontSize', fontSize);
ylabel('Value of Y', 'fontSize', fontSize);
title('Y as a function of Index', 'fontSize', fontSize);
% Get the histograms.
[xCounts xValues] = hist(x)
[yCounts yValues] = hist(y)
% Plot X histogram.
subplot(2, 2, 3);
bar(xValues, xCounts);
xlabel('Value of X', 'fontSize', fontSize);
ylabel('Frequency of Occurrence for X', 'fontSize', fontSize);
grid on;
title('Histogram of X', 'fontSize', fontSize);
subplot(2, 2, 4);
bar(yValues, yCounts);
xlabel('Value of Y', 'fontSize', fontSize);
ylabel('Frequency of Occurrence forY', 'fontSize', fontSize);
grid on;
title('Histogram of Y', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
  2 Comments
Irene
Irene on 13 Sep 2011
sorry for the confusion. i need a plot like this.
http://img30.imageshack.us/img30/1804/sampleplot.gif
thanks
Image Analyst
Image Analyst on 13 Sep 2011
That is a bar chart showing the frequency of occurrence of just one variable, like X. But you have two, X and Y. So wouldn't you have such a histogram chart for each one, or a single one showing the joint frequency of occurrence?

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 13 Sep 2011
If you have two variables, are you trying to produce a 3D bar chart such as is shown in the examples in bar3 ?
If you are wanting to histogram against both x and y, then you will want to use one of the MATLAB File Exchange contributions such as http://www.mathworks.com/matlabcentral/fileexchange/9896-2d-histogram-calculation

Community Treasure Hunt

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

Start Hunting!