From 3cfce69bf3cb7d7f7976abb454b64f6fa3a32c97 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 23 Sep 2010 09:49:04 +0000 Subject: minor doc fix, clean Plot --- lib/Graphics/Plot.hs | 84 ++++++++++++++-------------------------------------- 1 file changed, 23 insertions(+), 61 deletions(-) (limited to 'lib/Graphics/Plot.hs') diff --git a/lib/Graphics/Plot.hs b/lib/Graphics/Plot.hs index c5b5a4c..0bdd803 100644 --- a/lib/Graphics/Plot.hs +++ b/lib/Graphics/Plot.hs @@ -3,15 +3,13 @@ -- Module : Graphics.Plot -- Copyright : (c) Alberto Ruiz 2005-8 -- License : GPL-style --- +-- -- Maintainer : Alberto Ruiz (aruiz at um dot es) -- Stability : provisional -- Portability : uses gnuplot and ImageMagick -- --- Very basic (and provisional) drawing tools using gnuplot and imageMagick. --- --- This module is deprecated. It will be replaced by improved drawing tools based --- on the Gnuplot package by Henning Thielemann. +-- This module is deprecated. It can be replaced by improved drawing tools +-- available in the plot\\plot-gtk packages by Vivian McPhail or Gnuplot by Henning Thielemann. ----------------------------------------------------------------------------- module Graphics.Plot( @@ -20,7 +18,7 @@ module Graphics.Plot( plot, parametricPlot, - splot, mesh, mesh', meshdom, + splot, mesh, meshdom, matrixToPGM, imshow, @@ -32,35 +30,14 @@ import Numeric.Matrix import Data.List(intersperse) import System.Process (system) -size = dim - --- | Loads a real matrix from a formatted ASCII text file ---fromFile :: FilePath -> IO Matrix ---fromFile filename = readFile filename >>= return . readMatrix read - --- | Saves a real matrix to a formatted ascii text file -toFile' :: FilePath -> Matrix Double -> IO () -toFile' filename matrix = writeFile filename (unlines . map unwords. map (map show) . toLists $ matrix) - ------------------------------------------------------------------------- - - -- | From vectors x and y, it generates a pair of matrices to be used as x and y arguments for matrix functions. meshdom :: Vector Double -> Vector Double -> (Matrix Double , Matrix Double) -meshdom r1 r2 = (outer r1 (constant 1 (size r2)), outer (constant 1 (size r1)) r2) - -gnuplotX :: String -> IO () -gnuplotX command = do { _ <- system cmdstr; return()} where - cmdstr = "echo \""++command++"\" | gnuplot -persist" - -datafollows = "\\\"-\\\"" - -prep = (++"e\n\n") . unlines . map (unwords . (map show)) +meshdom r1 r2 = (outer r1 (constant 1 (dim r2)), outer (constant 1 (dim r1)) r2) {- | Draws a 3D surface representation of a real matrix. -> > mesh (hilb 20) +> > mesh $ build (10,10) (\\i j -> i + (j-5)^2) In certain versions you can interactively rotate the graphic using the mouse. @@ -70,15 +47,6 @@ mesh m = gnuplotX (command++dat) where command = "splot "++datafollows++" matrix with lines\n" dat = prep $ toLists $ m -mesh' :: Matrix Double -> IO () -mesh' m = do - writeFile "splot-gnu-command" "splot \"splot-tmp.txt\" matrix with lines; pause -1"; - toFile' "splot-tmp.txt" m - putStr "Press [Return] to close the graphic and continue... " - _ <- system "gnuplot -persist splot-gnu-command" - _ <- system "rm splot-tmp.txt splot-gnu-command" - return () - {- | Draws the surface represented by the function f in the desired ranges and number of points, internally using 'mesh'. > > let f x y = cos (x + y) @@ -86,11 +54,15 @@ mesh' m = do -} splot :: (Matrix Double->Matrix Double->Matrix Double) -> (Double,Double) -> (Double,Double) -> Int -> IO () -splot f rx ry n = mesh' z where +splot f rx ry n = mesh z where (x,y) = meshdom (linspace n rx) (linspace n ry) z = f x y -{- | plots several vectors against the first one -} +{- | plots several vectors against the first one + +> > let t = linspace 100 (-3,3) in mplot [t, sin t, exp (-t^2)] + +-} mplot :: [Vector Double] -> IO () mplot m = gnuplotX (commands++dats) where commands = if length m == 1 then command1 else commandmore @@ -102,26 +74,6 @@ mplot m = gnuplotX (commands++dats) where dats = concat (replicate (length m-1) dat) -{- -mplot' m = do - writeFile "plot-gnu-command" (commands++endcmd) - toFile "plot-tmp.txt" (fromColumns m) - putStr "Press [Return] to close the graphic and continue... " - system "gnuplot plot-gnu-command" - system "rm plot-tmp.txt plot-gnu-command" - return () - where - commands = if length m == 1 then command1 else commandmore - command1 = "plot \"plot-tmp.txt\" with lines\n" - commandmore = "plot " ++ plots ++ "\n" - plots = concat $ intersperse ", " (map cmd [2 .. length m]) - cmd k = "\"plot-tmp.txt\" using 1:"++show k++" with lines" - endcmd = "pause -1" --} - --- apply several functions to one object -mapf fs x = map ($ x) fs - {- | Draws a list of functions over a desired range and with a desired number of points > > plot [sin, cos, sin.(3*)] (0,2*pi) 1000 @@ -129,7 +81,8 @@ mapf fs x = map ($ x) fs -} plot :: [Vector Double->Vector Double] -> (Double,Double) -> Int -> IO () plot fs rx n = mplot (x: mapf fs x) - where x = linspace n rx + where x = linspace n rx + mapf gs y = map ($ y) gs {- | Draws a parametric curve. For instance, to draw a spiral we can do something like: @@ -165,6 +118,15 @@ imshow m = do ---------------------------------------------------- +gnuplotX :: String -> IO () +gnuplotX command = do { _ <- system cmdstr; return()} where + cmdstr = "echo \""++command++"\" | gnuplot -persist" + +datafollows = "\\\"-\\\"" + +prep = (++"e\n\n") . unlines . map (unwords . (map show)) + + gnuplotpdf :: String -> String -> [([[Double]], String)] -> IO () gnuplotpdf title command ds = gnuplot (prelude ++ command ++" "++ draw) >> postproc where prelude = "set terminal epslatex color; set output '"++title++".tex';" -- cgit v1.2.3