Plotting a graph using Haskell

Viewed 8486

Is that possible to plot a simple graph using Haskell ? Can any of you show me how to do that ?

The graph should contain at lest 3 points

2 Answers

Apart from Chart, there are some very good language-agnostic plotting libraries/systems, usable from Haskell. I use Vega and matplotlib myself.

Vega

You can use Vega-Lite with the Haskell package hvega, which gives you an html file with a chart drawn with the vega-lite js library.

Basic usage:

import           Graphics.Vega.VegaLite
main = do
 toHtmlFile "myplot.html" $ toVegaLite
  [ dataFromUrl "rows.tsv" [TSV]
  , mark Bar [MTooltip TTEncoding]
  , enc []
  , height 600
  , width 400
  ]
 where enc = encoding
      . position X [ PName "date"
                   , PmType Ordinal
                   ]
      . position Y [ PName "joy"
                   , PmType Quantitative
                   ]
      . color [MName "activity", MmType Nominal]

(Then put the tsv and html files in a directory served by a web server, e.g. $ python -m SimpleHTTPServer in that directory.) This example gives a stacked bar chart of three columns (assuming rows.tsv has the column names used in the code):

vega-haskell plot example

The documentation has lots of examples: https://hackage.haskell.org/package/hvega/docs/Graphics-Vega-Tutorials-VegaLite.html

matplotlib

If you can live with installing some Python deps, matplotlib makes it easy to interactively plot and explore.

Simple example:

onscreen $ let b = bar [21..23] in
   b [56,57,56] @@ [o2 "color" "#4C78A8"] 
 % b [26,24,25] @@ [o2 "color" "#F58518"] 
 % b [16,15,14] @@ [o2 "color" "#E45756"] 

matplotlib-haskell plot example

See the readme for more examples.

Plotly

Plotly (originally for R) is usable from Haskell with the package plotlyhs. You get out a standalone html page with an interactive plot.

Example usage: https://glutamate.github.io/plotlyhs/

Gnuplot

There are various packages that create gnuplot scripts which can be used to make png's etc: https://hackage.haskell.org/packages/search?terms=gnuplot

Related