1
$\begingroup$

My program produces a large data set, which is distributed by some unknown function. I've used SmoothHistogram to visualise it crudely as a probability distribution function, but now I want to export the plot data from SmoothHistogram - is there a way for me to retrieve just the line SmoothHistogram plots, so I can analyse it in an external program?

$\endgroup$
1
  • $\begingroup$ Try First@Cases[SmoothHistogram[something ...], Line[a___] :> a, Infinity]! This is the data now use Export on this data and for help with the possible export file format check the docs. $\endgroup$ – PlatoManiac Nov 29 '13 at 12:50
4
$\begingroup$

There are two methods:

  1. Extract the data directly from the SmoothHistogram plot:

    r = RandomReal[NormalDistribution[], 1000];
    plot = SmoothHistogram[r]
    

    enter image description here

    pts1 = Join @@ Cases[plot, Line[p__] :> p, ∞]
    
    {{-4.10173, 0.}, {-4.10173, 0.}, {-4.09273, 0.000372864}, ... }
    
  2. Use regular HistogramList with the subsequent smoothing

    pts2 = Transpose@{(Most[#] + Rest[#])/2, 
      GaussianFilter[#2, 12 {3, 1}, Padding -> 0.0]} & @@ 
        HistogramList[r, {-3.5, 3.5, 0.02}, "PDF"];
    

    Here 12*3 = 36 is the radius of Gaussian smoothing and 12*1 = 12 is the standard deviation in units 0.02.

Verification:

ListLinePlot[{pts1, pts2}]

enter image description here

Then you can Export pts1 or pts2.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.