0

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.

Bereket
  • 55
  • 8

1 Answers1

1

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.

honeste_vivere
  • 308
  • 5
  • 11
  • thanks for your input; however, I wanted the y values to range from 0 to 1, since it represents probability. I did this after creating the `hist` as you did above: `pdf=float(hist)/n_elements(b)` then `p_pdf=plot(locs,pdf,xtitle='values',ytitle='freq')` – Bereket Jun 10 '20 at 21:32
  • 1
    @Bereket - Then just normalize `hist` by `TOTAL(hist)` to get relative occurrence rates for each bin. – honeste_vivere Jun 10 '20 at 21:47
  • yep, that's essentially what I did. Thanks! – Bereket Jun 10 '20 at 21:51