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!