how to create a plot with a histogram in the same graph
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I want to create a plot of a distribution with a histogram together in the same plot.i'v only been able to plot them separately.
thanks in advance
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 11 Aug. 2012
The histogram is the distribution. Please explain. If you want to plot a curve - some curve representing the theoretical distribution rather than the actual distribution like what the histogram is, then call "hold on" after you call bar() to plot your histogram and before you call plot() to plot the curve:
bar(binCenters, countData);
hold on;
plot(x, theoreticalCurve, 'bo-');
3 Kommentare
Image Analyst
am 11 Aug. 2012
Bearbeitet: Image Analyst
am 11 Aug. 2012
Huh? The numbers you took the histogram of could also be called a "dataset" if you want, as could the counts which is the actual histogram itself. A histogram is a probability density function - the actual density of your actual data, which may be different than the theoretical "dataset" you'd get if you took the histogram of an infinite set of numbers. I'm not sure if you thought your comment clarified things, but it did not. What I mean is have you tried something like this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Generate values from a normal distribution
% with mean 1 and standard deviation 2:
data = 1 + 2 .* randn(10000,1);
[counts binCenters] = hist(data, 50);
bar(binCenters, counts);
grid on;
hold on;
title('Histogram', 'FontSize', fontSize);
xlabel('Value', 'FontSize', fontSize);
ylabel('Number of Counts', 'FontSize', fontSize);
% Enlarge figure to half screen.
set(gcf, 'units','normalized','outerposition',[0 .50 1 .5]);
% Get theoretical curve
x = binCenters;
% Make up some arbitrary curve.
% Note: this is not the theoretically accurate PDF for the randn() function.
theoreticalCurve = [0 5200*diff(erf((x-1)/3))];
% Plot it but also keep the bar chart.
plot(x, theoreticalCurve, 'ro-', 'LineWidth', 2);
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!