summaryrefslogtreecommitdiff
path: root/lib/Graphics/Plot.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-23 09:49:04 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-23 09:49:04 +0000
commit3cfce69bf3cb7d7f7976abb454b64f6fa3a32c97 (patch)
tree6c8d4b0ccbfc2eb289d9112c9106a53af58c6820 /lib/Graphics/Plot.hs
parent778e63dcc2b348914a4f6975f5328ff7fd25638a (diff)
minor doc fix, clean Plot
Diffstat (limited to 'lib/Graphics/Plot.hs')
-rw-r--r--lib/Graphics/Plot.hs84
1 files changed, 23 insertions, 61 deletions
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 @@
3-- Module : Graphics.Plot 3-- Module : Graphics.Plot
4-- Copyright : (c) Alberto Ruiz 2005-8 4-- Copyright : (c) Alberto Ruiz 2005-8
5-- License : GPL-style 5-- License : GPL-style
6-- 6--
7-- Maintainer : Alberto Ruiz (aruiz at um dot es) 7-- Maintainer : Alberto Ruiz (aruiz at um dot es)
8-- Stability : provisional 8-- Stability : provisional
9-- Portability : uses gnuplot and ImageMagick 9-- Portability : uses gnuplot and ImageMagick
10-- 10--
11-- Very basic (and provisional) drawing tools using gnuplot and imageMagick. 11-- This module is deprecated. It can be replaced by improved drawing tools
12-- 12-- available in the plot\\plot-gtk packages by Vivian McPhail or Gnuplot by Henning Thielemann.
13-- This module is deprecated. It will be replaced by improved drawing tools based
14-- on the Gnuplot package by Henning Thielemann.
15----------------------------------------------------------------------------- 13-----------------------------------------------------------------------------
16 14
17module Graphics.Plot( 15module Graphics.Plot(
@@ -20,7 +18,7 @@ module Graphics.Plot(
20 18
21 plot, parametricPlot, 19 plot, parametricPlot,
22 20
23 splot, mesh, mesh', meshdom, 21 splot, mesh, meshdom,
24 22
25 matrixToPGM, imshow, 23 matrixToPGM, imshow,
26 24
@@ -32,35 +30,14 @@ import Numeric.Matrix
32import Data.List(intersperse) 30import Data.List(intersperse)
33import System.Process (system) 31import System.Process (system)
34 32
35size = dim
36
37-- | Loads a real matrix from a formatted ASCII text file
38--fromFile :: FilePath -> IO Matrix
39--fromFile filename = readFile filename >>= return . readMatrix read
40
41-- | Saves a real matrix to a formatted ascii text file
42toFile' :: FilePath -> Matrix Double -> IO ()
43toFile' filename matrix = writeFile filename (unlines . map unwords. map (map show) . toLists $ matrix)
44
45------------------------------------------------------------------------
46
47
48-- | From vectors x and y, it generates a pair of matrices to be used as x and y arguments for matrix functions. 33-- | From vectors x and y, it generates a pair of matrices to be used as x and y arguments for matrix functions.
49meshdom :: Vector Double -> Vector Double -> (Matrix Double , Matrix Double) 34meshdom :: Vector Double -> Vector Double -> (Matrix Double , Matrix Double)
50meshdom r1 r2 = (outer r1 (constant 1 (size r2)), outer (constant 1 (size r1)) r2) 35meshdom r1 r2 = (outer r1 (constant 1 (dim r2)), outer (constant 1 (dim r1)) r2)
51
52gnuplotX :: String -> IO ()
53gnuplotX command = do { _ <- system cmdstr; return()} where
54 cmdstr = "echo \""++command++"\" | gnuplot -persist"
55
56datafollows = "\\\"-\\\""
57
58prep = (++"e\n\n") . unlines . map (unwords . (map show))
59 36
60 37
61{- | Draws a 3D surface representation of a real matrix. 38{- | Draws a 3D surface representation of a real matrix.
62 39
63> > mesh (hilb 20) 40> > mesh $ build (10,10) (\\i j -> i + (j-5)^2)
64 41
65In certain versions you can interactively rotate the graphic using the mouse. 42In certain versions you can interactively rotate the graphic using the mouse.
66 43
@@ -70,15 +47,6 @@ mesh m = gnuplotX (command++dat) where
70 command = "splot "++datafollows++" matrix with lines\n" 47 command = "splot "++datafollows++" matrix with lines\n"
71 dat = prep $ toLists $ m 48 dat = prep $ toLists $ m
72 49
73mesh' :: Matrix Double -> IO ()
74mesh' m = do
75 writeFile "splot-gnu-command" "splot \"splot-tmp.txt\" matrix with lines; pause -1";
76 toFile' "splot-tmp.txt" m
77 putStr "Press [Return] to close the graphic and continue... "
78 _ <- system "gnuplot -persist splot-gnu-command"
79 _ <- system "rm splot-tmp.txt splot-gnu-command"
80 return ()
81
82{- | Draws the surface represented by the function f in the desired ranges and number of points, internally using 'mesh'. 50{- | Draws the surface represented by the function f in the desired ranges and number of points, internally using 'mesh'.
83 51
84> > let f x y = cos (x + y) 52> > let f x y = cos (x + y)
@@ -86,11 +54,15 @@ mesh' m = do
86 54
87-} 55-}
88splot :: (Matrix Double->Matrix Double->Matrix Double) -> (Double,Double) -> (Double,Double) -> Int -> IO () 56splot :: (Matrix Double->Matrix Double->Matrix Double) -> (Double,Double) -> (Double,Double) -> Int -> IO ()
89splot f rx ry n = mesh' z where 57splot f rx ry n = mesh z where
90 (x,y) = meshdom (linspace n rx) (linspace n ry) 58 (x,y) = meshdom (linspace n rx) (linspace n ry)
91 z = f x y 59 z = f x y
92 60
93{- | plots several vectors against the first one -} 61{- | plots several vectors against the first one
62
63> > let t = linspace 100 (-3,3) in mplot [t, sin t, exp (-t^2)]
64
65-}
94mplot :: [Vector Double] -> IO () 66mplot :: [Vector Double] -> IO ()
95mplot m = gnuplotX (commands++dats) where 67mplot m = gnuplotX (commands++dats) where
96 commands = if length m == 1 then command1 else commandmore 68 commands = if length m == 1 then command1 else commandmore
@@ -102,26 +74,6 @@ mplot m = gnuplotX (commands++dats) where
102 dats = concat (replicate (length m-1) dat) 74 dats = concat (replicate (length m-1) dat)
103 75
104 76
105{-
106mplot' m = do
107 writeFile "plot-gnu-command" (commands++endcmd)
108 toFile "plot-tmp.txt" (fromColumns m)
109 putStr "Press [Return] to close the graphic and continue... "
110 system "gnuplot plot-gnu-command"
111 system "rm plot-tmp.txt plot-gnu-command"
112 return ()
113 where
114 commands = if length m == 1 then command1 else commandmore
115 command1 = "plot \"plot-tmp.txt\" with lines\n"
116 commandmore = "plot " ++ plots ++ "\n"
117 plots = concat $ intersperse ", " (map cmd [2 .. length m])
118 cmd k = "\"plot-tmp.txt\" using 1:"++show k++" with lines"
119 endcmd = "pause -1"
120-}
121
122-- apply several functions to one object
123mapf fs x = map ($ x) fs
124
125{- | Draws a list of functions over a desired range and with a desired number of points 77{- | Draws a list of functions over a desired range and with a desired number of points
126 78
127> > plot [sin, cos, sin.(3*)] (0,2*pi) 1000 79> > plot [sin, cos, sin.(3*)] (0,2*pi) 1000
@@ -129,7 +81,8 @@ mapf fs x = map ($ x) fs
129-} 81-}
130plot :: [Vector Double->Vector Double] -> (Double,Double) -> Int -> IO () 82plot :: [Vector Double->Vector Double] -> (Double,Double) -> Int -> IO ()
131plot fs rx n = mplot (x: mapf fs x) 83plot fs rx n = mplot (x: mapf fs x)
132 where x = linspace n rx 84 where x = linspace n rx
85 mapf gs y = map ($ y) gs
133 86
134{- | Draws a parametric curve. For instance, to draw a spiral we can do something like: 87{- | Draws a parametric curve. For instance, to draw a spiral we can do something like:
135 88
@@ -165,6 +118,15 @@ imshow m = do
165 118
166---------------------------------------------------- 119----------------------------------------------------
167 120
121gnuplotX :: String -> IO ()
122gnuplotX command = do { _ <- system cmdstr; return()} where
123 cmdstr = "echo \""++command++"\" | gnuplot -persist"
124
125datafollows = "\\\"-\\\""
126
127prep = (++"e\n\n") . unlines . map (unwords . (map show))
128
129
168gnuplotpdf :: String -> String -> [([[Double]], String)] -> IO () 130gnuplotpdf :: String -> String -> [([[Double]], String)] -> IO ()
169gnuplotpdf title command ds = gnuplot (prelude ++ command ++" "++ draw) >> postproc where 131gnuplotpdf title command ds = gnuplot (prelude ++ command ++" "++ draw) >> postproc where
170 prelude = "set terminal epslatex color; set output '"++title++".tex';" 132 prelude = "set terminal epslatex color; set output '"++title++".tex';"