Norman Duncan
Norman Duncan

Reputation: 51

Superimposed plots with haskell-chart

I am trying to use haskell-chart to draw a histogram with a superimposed density function. Following the examples on the project wiki and the tests, I wrote this code:

chart = toRenderable $ do
    plot $ fmap histToPlot $ liftEC $ do
        plot_hist_title .= "My histogram"
        plot_hist_bins .= 10
        plot_hist_values .= ([0,1..100] :: Double)
        plot_hist_norm_func .= const id
    plot (line "my curve" myCurve) where 
        myCurve = [zip [0,1..100] (repeat 20)] :: [[(Double, Double)]]

This gives a type error, because the histogram plot is EC (Layout Double Int) and the curve plot is EC (Layout Double Double). I tried to make the histogram plot into an EC (Layout Double Double) by changing plot_hist_norm_func .= const id into const (fromIntegral . id :: Int -> Double) and providing an instance of Default (PlotHist x Double). The resulted code type-checked but did not draw anything. My question is: how do I draw a histogram with a curve in haskell-chart?

Upvotes: 2

Views: 371

Answers (1)

Norman Duncan
Norman Duncan

Reputation: 51

The histogram package exported everything I needed to make a Default (PlotHist x Double) instance like this:

import Graphics.Rendering.Chart.Plot.Histogram(defaultNormedPlotHist)
instance Default (PlotHist x Double) where
    def = defaultNormedPlotHist

Here is the plot I wanted

enter image description here

Upvotes: 2

Related Questions