plotstatisticsdistributionidl-programming-language

How to draw Probability Density Function using line plots in IDL?


I have a vector containing values and I would like to draw a probability density function (PDF) graph for the values contained. Let's say I have a vector given by b=[1,1,3,4,5,2,3,5,1,4,2,4,1,1,4,2,3,5]. I can draw a histogram as follows

cghistoplot, b, binsize=1, xtitle='values', ytitle='freq', /fill

enter image description here

However, I want to draw the pdf using a line plot, i.e. I want y values to be normalized by the number of values in the vector (18 here). I know I can use cgplot to make line plots, but it needs x- and y-values.


Solution

  • You can just use the HISTOGRAM built-in routine like the following:

    hist = HISTOGRAM(b,BINSIZE=1,LOCATIONS=locs,MIN=0,MAX=6)
    

    Then you can plot the results doing something like:

    PLOT,locs,hist,PSYM=10,XTITLE='values',YTITLE='freq'
    

    This will show a quick and easy histogram form. If you want a line plot, just remove the PSYM keyword setting.