Skip to main content

export - Exporting SmoothHistogram plot data



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?



Answer



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.



Comments