Using histogram or histfit

97 ビュー (過去 30 日間)
Orongo
Orongo 2018 年 10 月 18 日
コメント済み: Image Analyst 2018 年 10 月 21 日
Hi, I have a large set of data containing the result of the difference between simulated values and base. I want to illustrate the fit with a histogram and a line of normal distribution N(mu,sigma), where mu=average(difference) and sigma=std(difference). I tried both the commands histogram and histfit and find the difference of the bars striking. Now I'm lost as to what these commands do, here are the graphs I get
Histogram:
Histifit
How can I amend my histogram with a line of average and standard deviation? If there is room, I would also like to understand the difference in the commands in layman terms.
Many thanks.

回答 (1 件)

Image Analyst
Image Analyst 2018 年 10 月 18 日
Use the line() function.
line([xValue, xValue], ylim, 'Color', 'r', 'LineWidth', 2);
  6 件のコメント
Orongo
Orongo 2018 年 10 月 21 日
I appreciate you taking your time to comment on this. But there is no need to shout or being rude. I'm here to learn, and if the answer given is not clear and I have to fill in the blanks then one can wonder if the answer was good in the first place. Your answer gave my 3 bars that I don't see very useful. Please stop responding to this thread if all you want is to improve your response stats.
Image Analyst
Image Analyst 2018 年 10 月 21 日
Sorry to shout and be rude to you. When you asked "How can I amend my histogram with a line of average and standard deviation?" what most people would tell you do do would be to use the line() function to put up lines at those 3 locations. If you think the 3 red lines are so wide that they look like 3 bars, you can adjust the LineWidth parameter to be 1 instead of 2. Here is the full demo - just copy and paste:
% Generate sample data with a variable name of "difference".
difference = .6 + 0.2 * randn(1, 10000);
figure(1) % Not really needed.
% Show the histogram with a Normal line fitted over it.
histfit(difference)
% Compute the mean and standard deviation of the actual data.
mu=mean(difference)
sigma=std(difference)
% Put up lines to indicate the mean, and mean +/- one standard deviation.
line([mu, mu], ylim, 'Color', 'r', 'LineWidth', 1);
line([mu + sigma, mu + sigma], ylim, 'Color', 'r', 'LineWidth', 1);
line([mu - sigma, mu - sigma], ylim, 'Color', 'r', 'LineWidth', 1);
% Make the graph look nice with a grid and labels
grid on
xlabel('difference', 'FontSize', 15);
ylabel('count', 'FontSize', 15);
title('Red Lines at mean and mean +/- standard deviation', 'FontSize', 15);
% Put up labels on lines
yl = ylim % Get limits of y axis so we can find a nice height for the text labels.
message = sprintf('%.3f ', mu);
text(mu, 0.95 * yl(2), message, 'Color', 'r', 'HorizontalAlignment', 'right', 'FontWeight', 'bold');
message = sprintf(' %.3f', mu+sigma);
text(mu+sigma, 0.9 * yl(2), message, 'Color', 'r', 'FontSize', 15);
message = sprintf('%.3f ', mu-sigma);
text(mu-sigma, 0.9 * yl(2), message, 'Color', 'r', 'HorizontalAlignment', 'right', 'FontSize', 15);
And here is the plot:
I'm not responding to you to improve my stats. I'm already #2 and there is no chance at all that I'll ever overtake Walter to become #1. I'm doing it to help you. This time I threw in a few more bonus lines to put some more eye candy on the graph and make it look nicer for you and help you learn.
If this is still not what you want, then maybe you can mock up a picture (in Photoshop or whatever) that shows exactly what you want, and insert it with the green and brown frame icon, and explain better "How can I amend my histogram with a line of average and standard deviation?" and explain "3 bars that I don't see very useful" so I'll understand what you want and give you something that is useful.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by