From: adam on 8 Sep 2009 15:00 Hi everyone, I've got some measure data from which I create a histogram. And now I've got a function for like the expected values, but it's a plot. Everything is fine if I hist() the data and plot() the function, but I'd like to combine them in one single graph. I've tried hist(...) Hold on plot(...) but that didn't work I also haven't found anything on the web yet..
From: Nathan on 8 Sep 2009 15:00 On Sep 8, 11:55 am, "adam " <unique...(a)gmail.com> wrote: > Hi everyone, > > I've got some measure data from which I create a histogram. > And now I've got a function for like the expected values, but it's a plot.. > > Everything is fine if I hist() the data and plot() the function, but I'd like to combine them in one single graph. > > I've tried > hist(...) > Hold on > plot(...) > but that didn't work > > I also haven't found anything on the web yet.. doc subplot %%%%%%%%%%%%% subplot(2,1,1),hist(...); subplot(2,1,2),plot(...); -Nathan
From: adam on 8 Sep 2009 15:11 Nathan <ngreco32(a)gmail.com> wrote in message <7d763f1a-0bad-449b-a1c6-b11bae9b29e5(a)l35g2000pra.googlegroups.com>... > > doc subplot > > %%%%%%%%%%%%% > > subplot(2,1,1),hist(...); > subplot(2,1,2),plot(...); > > -Nathan Thanks for your quick answer. It isn't exactly what I wanted though. I try to put them in the same graph, it should look like this (random screenshot from google, i'm aware it's not MATLAB): http://www.qualityadvisor.com/sqc/images/histogram2.JPG The plot should overly the histogram.
From: Tom Lane on 8 Sep 2009 15:30 >> subplot(2,1,1),hist(...); >> subplot(2,1,2),plot(...); >> >> -Nathan > > Thanks for your quick answer. > It isn't exactly what I wanted though. > > I try to put them in the same graph, it should look like this (random > screenshot from google, i'm aware it's not MATLAB): > http://www.qualityadvisor.com/sqc/images/histogram2.JPG > > The plot should overly the histogram. You didn't say exactly what was wrong when you tried the histogram and plot with "hold on." It may be that the two were not on the same scale. A histogram has bars that sum to the number of observations. A probability density, if that's what you were trying to plot, is scaled so that it integrates to one. You can use the histogram bar width and the number of observations to adjust either one so that it's on the same scale as the other. Here's an example: y = 100 + 10*randn(1000,1); hist(y,70:5:130) x = linspace(70,130); fx = 5 * 1000 * normpdf(x,100,10); % binwidth * nobs * density line(x,fx,'color','r') Of course you can use "hold on" and "plot" where I used "line" instead. I used normpdf from the Statistics Toolbox, but you may have your own density function or plan to write one. -- Tom
From: Nathan on 8 Sep 2009 15:38 On Sep 8, 12:11 pm, "adam " <unique...(a)gmail.com> wrote: > Nathan <ngrec...(a)gmail.com> wrote in message <7d763f1a-0bad-449b-a1c6-b11bae9b2...(a)l35g2000pra.googlegroups.com>... > > > doc subplot > > > %%%%%%%%%%%%% > > > subplot(2,1,1),hist(...); > > subplot(2,1,2),plot(...); > > > -Nathan > > Thanks for your quick answer. > It isn't exactly what I wanted though. > > I try to put them in the same graph, it should look like this (random screenshot from google, i'm aware it's not MATLAB):http://www.qualityadvisor.com/sqc/images/histogram2.JPG > > The plot should overly the histogram. Hm. Well, I have something exactly like that... but I'm not sure where I got the function from. (It's been part of my program before I even started working on it) However, this function applies the histogram to your data. It doesn't allow you to just put your data + a random histogram together. I hope this helps (and hope it's okay that I shared it). Here it is in it's entirety: function [h,S] = histfitp(data,nbins,PlotColor) %HISTFIT Histogram with superimposed fitted normal density. % HISTFIT(DATA,NBINS,[PlotColor]) plots a histogram of the values in the vector DATA. % using NBINS bars in the histogram. With one input argument, NBINS is set % to the square root of the number of elements in DATA. % % PlotColor default is 'b' (blue) % % H = HISTFIT(DATA,NBINS) returns a vector of handles to the plotted lines. % H(1) is a handle to the histogram, H(2) is a handle to the density curve. % % Amended by: PNath(a)London.edu 15-Jan-2002 % % B.A. Jones 2-14-95 % Copyright (c) 1993-98 by The MathWorks, Inc. % $Revision: 2.7 $ $Date: 1998/05/28 20:13:55 $ if nargin < 3 PlotColor = 'b'; end [row,col] = size(data); if min(row,col) > 1 error('First argument has to be a vector.'); end if row == 1 row = col; data = data(:); end if nargin < 2 nbins = ceil(sqrt(row)); end [n,xbin]=hist(data,nbins); mr = mean(data); % Estimates the parameter, MU, of the normal distribution. sr = std(data); % Estimates the parameter, SIGMA, of the normal distribution. x=(-3*sr+mr:0.1*sr:3*sr+mr)';% Evenly spaced samples of the expected data range. hh = bar(xbin,n,1,PlotColor); % Plots the histogram. No gap between bars. np = get(gca,'NextPlot'); set(gca,'NextPlot','add') xd = get(hh,'Xdata'); % Gets the x-data of the bins. rangex = max(xd(:)) - min(xd(:)); % Finds the range of this data. binwidth = rangex/nbins; % Finds the width of each bin. y = normpdf(x,mr,sr); y = row*(y*binwidth); % Normalization necessary to overplot the histogram. hh1 = plot(x,y,'r-','LineWidth',2); % Plots density line over histogram. % Create an output structure S.xbin = xbin; S.n = n; S.PlotColor = PlotColor; S.x = x; S.y = y; if nargout >= 1 h = [hh; hh1]; end set(gca,'NextPlot',np) %%%%%%%%%%%%%%%%%%%%%%%%%%5 -Nathan
|
Next
|
Last
Pages: 1 2 Prev: How to insert a variable as a cell array in GUIDE? Next: cell2mat function |