$\lambda$plot

A simple plotting library for Haskell. It creates SVG graphics that can be directly included in html documents, or converted to pdf for $\LaTeX$.

install

This is included in the artools package, available from the easyVision repository. Clone the repo, checkout the branch reorg, cd packages/tools, and cabal install.

import

import Numeric.LinearAlgebra.HMatrix
import Graphics.SVG
import Util.Statistics(histogram)

simple plots

Plot the graphs of some functions with desired line widths and colors, and default options:

plot1 = hPlot
    [ plot x (sin x)     "blue"  1 "sin x"
    , plot x (cos (2*x)) "brown" 3 "cos(2x)"
    ]
  where
    x = linspace 100 (0,2*pi)


































 

 

































 0 
 1 
 2 
 3 
 4 
 5 
 6 

  




 -1.0 
 -0.8 
 -0.6 
 -0.4 
 -0.2 
 0.0 
 0.2 
 0.4 
 0.6 
 0.8 
 1.0 







  












































 

 


























 sin x 
 cos(2x) 





The graphs can be customized with several options:

plot2 = hPlot
    [ Title "SAMPLES", Labels "x" "sqrt(x)", PlotSize 700 400
    , MarginX 0.05, MinY 0, DeltaX 1, DeltaY 0.5, DecY 1
    , GridSty "none", LegendPos 0.1 0.8
    , plotMark x (sqrt x) "green" 1 circles "red" 4 "sqrt"
    , textAt 8 2.5 "Hi!"
    ]
  where
    x = vector [1,3,4,7,16]




 SAMPLES 








































 

 



























 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 

 x 




 0.0 
 0.5 
 1.0 
 1.5 
 2.0 
 2.5 
 3.0 
 3.5 
 4.0 







 sqrt(x) 



















































 

 


















 sqrt 



 Hi! 






The effect of most options should be more or less evident. In this graph the number of decimal places in the y axis is not computed correctly and we have overriden it using DecY.

In the next graph we fix a desired aspect ratio by setting the axes limits and VisibleSize:

plot3 = putStr $ hPlot
    [ Title "spiral", Labels "x" "y", VisibleSize 400 400
    , MinX (-5), MaxX 5, MinY (-5), MaxY 5
    , plot (0.3*t*cos t) (0.3*t*sin t) "red" 1 ""
    ]
  where
    t = linspace 100 (0,4*pi)




 spiral 





































 

 






















 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 

 x 




 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 







 y 















































histogram

This is a demo of plotHistogram and we also show a "hacked" plot style by abusing the color string:

plot4 = putStr $ hPlot
    [ LegendPos 0.7 0.9, Labels "x" "P(x)", Title "histogram"
    , MinY 0, MarginX 0.05
    , plotHistogram hist   "pink" "red" 1 "empirical"
    , plot          x teor  dashblue    2 "theoretical"
    ]
  where
    samples = randomVector 666 Gaussian 200
    hist = histogram 20 (-4,4) samples
    teor = 1/sqrt(2*pi)/1*exp(-0.5*((x-0)/1)**2)
    x = linspace 100 (-4,4)
    dashblue = "blue;stroke-dasharray:2"




 histogram 





























 

 

































 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 

 x 




 0.0 
 0.1 
 0.2 
 0.3 
 0.4 







 P(x) 








































 

 


























 empirical 
 theoretical 





filled plot

Filled styles are provisional, but the effect looks like this:

plot5 = hPlot
    [ MinY 0, LegendPos 0.1 0.8
    , Plots [ fromY 0 (x,y1) ~> (plotFill "lightgreen" "green" 2, "good ")
            , fromY 0 (x,y2) ~> (plotFill "pink"       "red"   2, "bad")
            ]
    ]
  where
    x = linspace 20 (0,10)
    y1 = 3*x + randomVector 666 Gaussian 20
    y2 = 2*x + randomVector 777 Gaussian 20































 

 

































 0 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 

  




 0 
 10 
 20 
 30 







  









































 

 


























 good  
 bad 





Another example:

plot5b = hPlot
    [ MinY 0
    , Plots [ fromY 0 (x',f x') ~> (plotFill "skyblue" "none" 1, "")
            ,         (x, f x)  ~> (plotColor "black" 2, "")
            ]
    ]
  where
    f x = 1/sqrt(2*pi)/1*exp(-0.5*((x-0)/1)**2)
    x  = linspace 100 (-4,4)
    x' = linspace 20 (0.5,2::Double)






























 

 

































 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 

  




 0.0 
 0.1 
 0.2 
 0.3 
 0.4 







  







































scatter plot

The scatter function is easily defined from plotFullMark and takes the points from the rows of a matrix.

plot6 = putStr $ hPlot
    [ scatter c1 circles  "green" 4 "black" 1 "good "
    , scatter c2 squares  "red"   4 "black" 1 "bad"
    , scatter c3 diamonds "cyan"  5 "blue"  1 "?"
    , MarginX 0.05, Title "scatterplot", PlotSize 600 500
    ]
  where
    c1 = gaussianSample 666 30 (vector [3,3])   (matrix 2 [1, 0.8,  0.8,1])
    c2 = gaussianSample 777 20 (vector [-3,-3]) (matrix 2 [1,-0.8, -0.8,1])
    c3 = gaussianSample 888 20 (vector [-2,2]) (matrix 2  [0.5,0, 0,0.5])
    scatter (toColumns -> [x,y]) shape col sz colb wb lb
        = plotFullMark x y "none" 1 shape col sz colb wb lb




 scatterplot 

























 

 


















































































































 -4 
 -2 
 0 
 2 
 4 

  




 -4 
 -2 
 0 
 2 
 4 







  




































 

 














































 good  
 bad 
 ? 





error bars

plot7 = putStr $ hPlot
    [ MarginX 0.1, MarginY 0.1
    , plotErrorMark x (sqrt x) ϵ "orange" 1 circles "green" 3 "blue" 5 "sqrt"
    ]
  where
    x = vector [1,3,4,7,16]
    ϵ = vector [0.2,0.1,0.3,0.2,0.2]





























 

 


























































 0 
 2 
 4 
 6 
 8 
 10 
 12 
 14 
 16 

  




 1 
 2 
 3 
 4 







  







































 

 


















 sqrt 





box plots

plot8 = hPlot
    [ MarginX 0.1, MarginY 0.1, DeltaX 20
    , MinX 0.1, MaxX 4.9, MinY 0, MaxY 10

    , boxPlot x y samples "orange" 2 "test"

    ]
  where
    x = [1,2,3,4]
    y = [3,6,5,7]
    samples = toColumns
        $ gaussianSample 666 100 (vector y) (diagl [0.1,0.5,1,0.5])



























 

 






























































  




 0 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 







  





































 

 


















 test 





in construction

Other types of graphs and animations will be added in the future.