Matlab: plotting frequency distribution with a curve

前端 未结 2 485
梦谈多话
梦谈多话 2021-01-26 19:00

I have to plot 10 frequency distributions on one graph. In order to keep things tidy, I would like to avoid making a histogram with bins and would prefer having lines that follo

2条回答
  •  耶瑟儿~
    2021-01-26 19:46

    You're using the default number of bins for your histogram and, I will assume, for your kernel density estimation calculations.

    Depending on how many data points you have, that will certainly not be optimal, as you've discovered. The first thing to try is to calculate the optimum bin width to give the smoothest curve while simultaneously preserving the underlying PDF as best as possible. (see also here, here, and here);

    If you still don't like how smooth the resulting plot is, you could try using the bins output from hist as a further input to ksdensity. Perhaps something like this:

    [kcounts,kbins] = ksdensity(data,bins,'npoints',length(bins));
    

    I don't have your data, so you may have to play with the parameters a bit to get exactly what you want.

    Alternatively, you could try fitting a spline through the points that you get from hist and plotting that instead.

    Some code:

    data = randn(1,1e4);
    
    optN = sshist(data);
    
    figure(1)
    [N,Center] = hist(data);
    [Nopt,CenterOpt] = hist(data,optN);
    [f,xi] = ksdensity(data,CenterOpt);
    
    dN = mode(diff(Center));
    dNopt = mode(diff(CenterOpt));
    
    plot(Center,N/dN,'.-',CenterOpt,Nopt/dNopt,'.-',xi,f*length(data),'.-')
    legend('Default','Optimum','ksdensity')
    

    The result:

    Different styles of histogram

    Note that the "optimum" bin width preserves some of the fine structure of the distribution (I had to run this a couple times to get the spikes) while the ksdensity gives a smooth curve. Depending on what you're looking for in your data, that may be either good or bad.

提交回复
热议问题