Using histogram or histfit

조회 수: 72 (최근 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 CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by