13

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 2

12

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.

dataframe

The dataframe library is a tool for exploratory data analysis in Haskell, and lets you plot ASCII graphs in the REPL:

dataframe example

as well as saving it to interactive browser charts like

ghci> P.plotScatter "dogs" "joy" df >>= P.showInDefaultBrowser
Saving plot to: ~/plot-chart_ACzzzidiLidnydNLE32ZmgMH114vwdH87VQwxANWcezbIZ.html

You can also use it as a Jupyter notebook, this is from a demo you can try:

enter image description here

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

Sign up to request clarification or add additional context in comments.

Comments

10

haskell-chart seems to be good.

The wiki contains a list of graphs drawn using that package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.