29

Is there a Haskell library for drawing plots similar to MATLAB, scilab or matplotlib? They all have very simple interfaces, which work like a state machine:

plot(xs, ys)
show() -- opens window with plot

It would be nice to display plots in a window and to have ability to write them to disk.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Trismegistos
  • 3,821
  • 2
  • 24
  • 41
  • 2
    I edited your question and replaced "numpy" with "matplotlib", since numpy is not a graphics/plotting library - I assume that you meant matplotlib, but feel free to correct it if I am wrong. – Chris Jan 26 '12 at 18:31

5 Answers5

12

What about gnuplot?

For example, plotList from Graphics.Gnuplot.Simple:

plotList [] [(1, 1), (2, 2), (3, 3)]
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Yuras
  • 13,856
  • 1
  • 45
  • 58
11

From a glance at matplotlib, I don't think the Haskell ecosystem has anything as feature-rich. However, I've been happy with the results produced by the Chart library. There are also bindings to graphviz (that links one of several) and Ubigraph.

Edit: Responding to the request for plotting (x,y) coordinates:

I'm not entirely clear what you want. If you have a function f :: x -> y then just use the plotWindow (or PNG, etc) function:

import Graphics.Rendering.Chart.Simple
main = plotWindow [0,0.1..5::Double] sin

If you have a bunch of points, [(x,y)], then the same code with a lookup into the list, instead of a continuous function like sin, should work fine. See the linked page for many many examples.

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
5

There is also the plot package. When used with plot-gtk graphs can be displayed and modified within GHCi. Plots can be written to disk in the formats that Cairo supports.

The Simple interface is similar to gnuplot's:

test_graph2 = do
     plot (ts,[point (ds,es) (Cross,red),line fs blue])
     title "Testing plot package:"
     subtitle "with 1 second of a 15Hz sine wave"
     xlabel "time (s)"
     ylabel "amplitude"
     yrange Linear (-1.25) 1.25
vivian
  • 1,434
  • 1
  • 10
  • 13
1

Try gnuplot. It's cross-language, quite fast at scale, and always a nice thing to know, even if it is old. These instructions should get you a working example:

cabal install gnuplot
sudo apt-get install gnuplot-x11

ghci
GHCi, version 7.4.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
h> import Graphics.Gnuplot.Simple
h> plotFunc [] (linearScale 1000 (-20,20)) (\x -> sin x / x)
RussellStewart
  • 5,293
  • 3
  • 26
  • 23
1

There is also matplotlib-haskell library (pretty new). It is very expressive yet simple to use.

Example:

import Graphics.Matplotlib

plotQuadraticFn :: IO ()
plotQuadraticFn = onscreen $ plot x y
  where
    x = [-10..10]
    y = fmap (**2) x
Erik Cupal
  • 2,735
  • 1
  • 19
  • 20