diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-09-23 09:49:04 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-09-23 09:49:04 +0000 |
commit | 3cfce69bf3cb7d7f7976abb454b64f6fa3a32c97 (patch) | |
tree | 6c8d4b0ccbfc2eb289d9112c9106a53af58c6820 /lib/Graphics | |
parent | 778e63dcc2b348914a4f6975f5328ff7fd25638a (diff) |
minor doc fix, clean Plot
Diffstat (limited to 'lib/Graphics')
-rw-r--r-- | lib/Graphics/Plot.hs | 84 |
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 | ||
17 | module Graphics.Plot( | 15 | module 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 | |||
32 | import Data.List(intersperse) | 30 | import Data.List(intersperse) |
33 | import System.Process (system) | 31 | import System.Process (system) |
34 | 32 | ||
35 | size = 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 | ||
42 | toFile' :: FilePath -> Matrix Double -> IO () | ||
43 | toFile' 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. |
49 | meshdom :: Vector Double -> Vector Double -> (Matrix Double , Matrix Double) | 34 | meshdom :: Vector Double -> Vector Double -> (Matrix Double , Matrix Double) |
50 | meshdom r1 r2 = (outer r1 (constant 1 (size r2)), outer (constant 1 (size r1)) r2) | 35 | meshdom r1 r2 = (outer r1 (constant 1 (dim r2)), outer (constant 1 (dim r1)) r2) |
51 | |||
52 | gnuplotX :: String -> IO () | ||
53 | gnuplotX command = do { _ <- system cmdstr; return()} where | ||
54 | cmdstr = "echo \""++command++"\" | gnuplot -persist" | ||
55 | |||
56 | datafollows = "\\\"-\\\"" | ||
57 | |||
58 | prep = (++"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 | ||
65 | In certain versions you can interactively rotate the graphic using the mouse. | 42 | In 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 | ||
73 | mesh' :: Matrix Double -> IO () | ||
74 | mesh' 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 | -} |
88 | splot :: (Matrix Double->Matrix Double->Matrix Double) -> (Double,Double) -> (Double,Double) -> Int -> IO () | 56 | splot :: (Matrix Double->Matrix Double->Matrix Double) -> (Double,Double) -> (Double,Double) -> Int -> IO () |
89 | splot f rx ry n = mesh' z where | 57 | splot 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 | -} | ||
94 | mplot :: [Vector Double] -> IO () | 66 | mplot :: [Vector Double] -> IO () |
95 | mplot m = gnuplotX (commands++dats) where | 67 | mplot 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 | {- | ||
106 | mplot' 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 | ||
123 | mapf 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 | -} |
130 | plot :: [Vector Double->Vector Double] -> (Double,Double) -> Int -> IO () | 82 | plot :: [Vector Double->Vector Double] -> (Double,Double) -> Int -> IO () |
131 | plot fs rx n = mplot (x: mapf fs x) | 83 | plot 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 | ||
121 | gnuplotX :: String -> IO () | ||
122 | gnuplotX command = do { _ <- system cmdstr; return()} where | ||
123 | cmdstr = "echo \""++command++"\" | gnuplot -persist" | ||
124 | |||
125 | datafollows = "\\\"-\\\"" | ||
126 | |||
127 | prep = (++"e\n\n") . unlines . map (unwords . (map show)) | ||
128 | |||
129 | |||
168 | gnuplotpdf :: String -> String -> [([[Double]], String)] -> IO () | 130 | gnuplotpdf :: String -> String -> [([[Double]], String)] -> IO () |
169 | gnuplotpdf title command ds = gnuplot (prelude ++ command ++" "++ draw) >> postproc where | 131 | gnuplotpdf 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';" |