Hoodlums Message Board › Tip: simple charting from ghci

Tip: simple charting from ghci

Neil B
user 13347289
London, GB
Post #: 7
At this month's hoodlums meetup we wanted to plot some numeric values quickly. We printed the values to newline-separated text file and used the gnuplot utility. (It's a great tool to know).

If you really want to do it in haskell and from the comfort of GHCI, here is the simplest example of the chart package.

To install it, cabal install chart.

import Graphics.Rendering.Chart.Simple

main = plotWindow xs ys "x" "series name here"
xs = [1..]::[Double]
ys = [5,10,20,7,3]::[Double]

(Yuck, indentation is lost by this message board!)

For beginners, here's a helper script:

import Graphics.Rendering.Chart.Simple
simplePlot ys = plotWindow ([1..]::[Double]) ys "x" "unnamed"

If you save that and :load it into your ghci session, you can generate a simple plot with the following:
simplePlot [3.1, 6.2, 10, 8, 1]
or (revisiting our square-wave generation):
simplePlot $ take 100 $ cycle $ ((replicate 8 1 ++ replicate 8 (-1))::[Double])

That needs some some tweaking to work nicely with Int, Integer and Float. For now, though, if you have a list "myValues" of elements of type Int or Integer, you can do simplePlot $ map fromIntegral myValues

That package has many more powerful rendering options.
Guy Thomas B.
user 14444283
London, GB
Post #: 1
Hmm there seem to be some issues with using this from Ubuntu. I think you might need to install the latest version of GHC manually rather than using apt-get. (Cabal is basically refusing to self update and you can't install chart without the latest version apparently).

Macports version of ghc seems to be struggling too...
Powered by mvnForum

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy