Histogram
조회 수: 21 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Image Analyst
2011년 9월 13일
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
Image Analyst
2011년 9월 13일
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?
추가 답변 (1개)
Walter Roberson
2011년 9월 13일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!