Using histogram or histfit
53 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192016/image.png)
Histifit
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192018/image.png)
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.
0 Kommentare
Antworten (1)
Image Analyst
am 18 Okt. 2018
Use the line() function.
line([xValue, xValue], ylim, 'Color', 'r', 'LineWidth', 2);
6 Kommentare
Image Analyst
am 21 Okt. 2018
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193329/image.png)
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.
Siehe auch
Kategorien
Mehr zu Histograms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!