org-plot.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; org-plot.el --- Support for plotting from Org-mode
  2. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Eric Schulte <schulte dot eric at gmail dot com>
  5. ;; Keywords: tables, plotting
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.06b
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;; Borrows ideas and a couple of lines of code from org-exp.el.
  23. ;; Thanks to the org-mode mailing list for testing and implementation
  24. ;; and feature suggestions
  25. ;;; Code:
  26. (require 'org)
  27. (require 'org-exp)
  28. (require 'org-table)
  29. (eval-and-compile
  30. (require 'cl))
  31. (declare-function gnuplot-delchar-or-maybe-eof "ext:gnuplot" (arg))
  32. (declare-function gnuplot-mode "ext:gnuplot" ())
  33. (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot" ())
  34. (defvar org-plot/gnuplot-default-options
  35. '((:plot-type . 2d)
  36. (:with . lines)
  37. (:ind . 0))
  38. "Default options to gnuplot used by `org-plot/gnuplot'")
  39. (defun org-plot/add-options-to-plist (p options)
  40. "Parse an OPTONS line and set values in the property list P.
  41. Returns the resulting property list."
  42. (let (o)
  43. (when options
  44. (let ((op '(("type" . :plot-type)
  45. ("script" . :script)
  46. ("line" . :line)
  47. ("set" . :set)
  48. ("title" . :title)
  49. ("ind" . :ind)
  50. ("deps" . :deps)
  51. ("with" . :with)
  52. ("file" . :file)
  53. ("labels" . :labels)
  54. ("map" . :map)))
  55. (multiples '("set" "line"))
  56. (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
  57. (start 0)
  58. o)
  59. (while (setq o (pop op))
  60. (if (member (car o) multiples) ;; keys with multiple values
  61. (while (string-match
  62. (concat (regexp-quote (car o)) regexp)
  63. options start)
  64. (setq start (match-end 0))
  65. (setq p (plist-put p (cdr o)
  66. (cons (car (read-from-string
  67. (match-string 1 options)))
  68. (plist-get p (cdr o)))))
  69. p)
  70. (if (string-match (concat (regexp-quote (car o)) regexp)
  71. options)
  72. (setq p (plist-put p (cdr o)
  73. (car (read-from-string
  74. (match-string 1 options)))))))))))
  75. p)
  76. (defun org-plot/goto-nearest-table ()
  77. "Move the point forward to the beginning of nearest table.
  78. Return value is the point at the beginning of the table."
  79. (interactive) (move-beginning-of-line 1)
  80. (while (not (or (org-at-table-p) (< 0 (forward-line 1)))))
  81. (goto-char (org-table-begin)))
  82. (defun org-plot/collect-options (&optional params)
  83. "Collect options from an org-plot '#+Plot:' line. Accepts an
  84. optional property list PARAMS, to which the options will be
  85. added. Returns the resulting property list."
  86. (interactive)
  87. (let ((line (thing-at-point 'line)))
  88. (if (string-match "#\\+PLOT: +\\(.*\\)$" line)
  89. (org-plot/add-options-to-plist params (match-string 1 line))
  90. params)))
  91. (defun org-plot-quote-tsv-field (s)
  92. "Quote field S for export to gnuplot."
  93. (if (string-match org-table-number-regexp s) s
  94. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")))
  95. (defun org-plot/gnuplot-to-data (table data-file params)
  96. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  97. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  98. (with-temp-file
  99. data-file (insert (orgtbl-to-generic
  100. table
  101. (org-combine-plists
  102. '(:sep "\t" :fmt org-plot-quote-tsv-field)
  103. params))))
  104. nil)
  105. (defun org-plot/gnuplot-to-grid-data (table data-file params)
  106. "Export the data in TABLE to DATA-FILE in a format appropriate
  107. for grid plotting by gnuplot. PARAMS specifies which columns of
  108. TABLE should be plotted as independant and dependant variables."
  109. (interactive)
  110. (let* ((ind (- (plist-get params :ind) 1))
  111. (deps (if (plist-member params :deps)
  112. (mapcar (lambda (val) (- val 1)) (plist-get params :deps))
  113. (let (collector)
  114. (dotimes (col (length (first table)))
  115. (setf collector (cons col collector)))
  116. collector)))
  117. row-vals (counter 0))
  118. (when (>= ind 0) ;; collect values of ind col
  119. (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter))
  120. (cons counter (nth ind row))) table)))
  121. (when (or deps (>= ind 0)) ;; remove non-plotting columns
  122. (setf deps (delq ind deps))
  123. (setf table (mapcar (lambda (row)
  124. (dotimes (col (length row))
  125. (unless (memq col deps)
  126. (setf (nth col row) nil)))
  127. (delq nil row))
  128. table)))
  129. ;; write table to gnuplot grid datafile format
  130. (with-temp-file data-file
  131. (let ((num-rows (length table)) (num-cols (length (first table)))
  132. front-edge back-edge)
  133. (flet ((gnuplot-row (col row value)
  134. (setf col (+ 1 col)) (setf row (+ 1 row))
  135. (format "%f %f %f\n%f %f %f\n"
  136. col (- row 0.5) value ;; lower edge
  137. col (+ row 0.5) value))) ;; upper edge
  138. (dotimes (col num-cols)
  139. (dotimes (row num-rows)
  140. (setf back-edge
  141. (concat back-edge
  142. (gnuplot-row (- col 1) row (string-to-number
  143. (nth col (nth row table))))))
  144. (setf front-edge
  145. (concat front-edge
  146. (gnuplot-row col row (string-to-number
  147. (nth col (nth row table)))))))
  148. ;; only insert once per row
  149. (insert back-edge) (insert "\n") ;; back edge
  150. (insert front-edge) (insert "\n") ;; front edge
  151. (setf back-edge "") (setf front-edge "")))))
  152. row-vals))
  153. (defun org-plot/gnuplot-script (data-file num-cols params)
  154. "Write a gnuplot script to DATA-FILE respecting the options set
  155. in PARAMS. NUM-COLS controls the number of columns plotted in a
  156. 2-d plot."
  157. (let* ((type (plist-get params :plot-type))
  158. (with (if (equal type 'grid)
  159. 'pm3d
  160. (plist-get params :with)))
  161. (sets (plist-get params :set))
  162. (lines (plist-get params :line))
  163. (map (plist-get params :map))
  164. (title (plist-get params :title))
  165. (file (plist-get params :file))
  166. (ind (plist-get params :ind))
  167. (text-ind (plist-get params :textind))
  168. (deps (if (plist-member params :deps) (plist-get params :deps)))
  169. (col-labels (plist-get params :labels))
  170. (x-labels (plist-get params :xlabels))
  171. (y-labels (plist-get params :ylabels))
  172. (plot-str "'%s' using %s%d%s with %s title '%s'")
  173. (plot-cmd (case type
  174. ('2d "plot")
  175. ('3d "splot")
  176. ('grid "splot")))
  177. (script "reset") plot-lines)
  178. (flet ((add-to-script (line) (setf script (format "%s\n%s" script line))))
  179. (when file ;; output file
  180. (add-to-script (format "set term %s" (file-name-extension file)))
  181. (add-to-script (format "set output '%s'" file)))
  182. (case type ;; type
  183. ('2d ())
  184. ('3d (if map (add-to-script "set map")))
  185. ('grid (if map
  186. (add-to-script "set pm3d map")
  187. (add-to-script "set pm3d"))))
  188. (when title (add-to-script (format "set title '%s'" title))) ;; title
  189. (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line
  190. (when sets ;; set
  191. (mapc (lambda (el) (add-to-script (format "set %s" el))) sets))
  192. (when x-labels ;; x labels (xtics)
  193. (add-to-script
  194. (format "set xtics (%s)"
  195. (mapconcat (lambda (pair)
  196. (format "\"%s\" %d" (cdr pair) (car pair)))
  197. x-labels ", "))))
  198. (when y-labels ;; y labels (ytics)
  199. (add-to-script
  200. (format "set ytics (%s)"
  201. (mapconcat (lambda (pair)
  202. (format "\"%s\" %d" (cdr pair) (car pair)))
  203. y-labels ", "))))
  204. (case type ;; plot command
  205. ('2d (dotimes (col num-cols)
  206. (unless (and (equal type '2d)
  207. (or (and ind (equal (+ 1 col) ind))
  208. (and deps (not (member (+ 1 col) deps)))))
  209. (setf plot-lines
  210. (cons
  211. (format plot-str data-file
  212. (or (and (not text-ind) ind
  213. (> ind 0) (format "%d:" ind)) "")
  214. (+ 1 col)
  215. (if text-ind (format ":xticlabel(%d)" ind) "")
  216. with
  217. (or (nth col col-labels) (format "%d" (+ 1 col))))
  218. plot-lines)))))
  219. ('3d
  220. (setq plot-lines (list (format "'%s' matrix with %s title ''"
  221. data-file with))))
  222. ('grid
  223. (setq plot-lines (list (format "'%s' with %s title ''"
  224. data-file with)))))
  225. (add-to-script
  226. (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) "\\\n ,")))
  227. script)))
  228. ;;-----------------------------------------------------------------------------
  229. ;; facad functions
  230. ;;;###autoload
  231. (defun org-plot/gnuplot (&optional params)
  232. "Plot table using gnuplot. Gnuplot options can be specified
  233. with PARAMS. If not given options will be taken from the +PLOT
  234. line directly before or after the table."
  235. (interactive)
  236. (require 'gnuplot)
  237. (save-window-excursion
  238. (delete-other-windows)
  239. (when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running
  240. (save-excursion
  241. (set-buffer "*gnuplot*") (goto-char (point-max))
  242. (gnuplot-delchar-or-maybe-eof nil)))
  243. (org-plot/goto-nearest-table)
  244. ;; set default options
  245. (mapc
  246. (lambda (pair)
  247. (unless (plist-member params (car pair))
  248. (setf params (plist-put params (car pair) (cdr pair)))))
  249. org-plot/gnuplot-default-options)
  250. ;; collect table and table information
  251. (let* ((data-file (make-temp-file "org-plot"))
  252. (table (org-table-to-lisp))
  253. (num-cols (length (first table))))
  254. (while (equal 'hline (first table)) (setf table (cdr table)))
  255. (when (equal (second table) 'hline)
  256. (setf params (plist-put params :labels (first table))) ;; headers to labels
  257. (setf table (delq 'hline (cdr table)))) ;; clean non-data from table
  258. ;; collect options
  259. (save-excursion (while (and (equal 0 (forward-line -1))
  260. (looking-at "#\\+"))
  261. (setf params (org-plot/collect-options params))))
  262. ;; dump table to datafile (very different for grid)
  263. (case (plist-get params :plot-type)
  264. ('2d (org-plot/gnuplot-to-data table data-file params))
  265. ('3d (org-plot/gnuplot-to-data table data-file params))
  266. ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data
  267. table data-file params)))
  268. (when y-labels (plist-put params :ylabels y-labels)))))
  269. ;; check for text ind column
  270. (let ((ind (- (plist-get params :ind) 1)))
  271. (when (and (>= ind 0) (equal '2d (plist-get params :plot-type)))
  272. (if (> (length
  273. (delq 0 (mapcar
  274. (lambda (el)
  275. (if (string-match org-table-number-regexp el)
  276. 0 1))
  277. (mapcar (lambda (row) (nth ind row)) table)))) 0)
  278. (plist-put params :textind t))))
  279. ;; write script
  280. (with-temp-buffer
  281. (if (plist-get params :script) ;; user script
  282. (progn (insert-file-contents (plist-get params :script))
  283. (goto-char (point-min))
  284. (while (re-search-forward "$datafile" nil t)
  285. (replace-match data-file nil nil)))
  286. (insert
  287. (org-plot/gnuplot-script data-file num-cols params)))
  288. ;; graph table
  289. (gnuplot-mode)
  290. (gnuplot-send-buffer-to-gnuplot))
  291. ;; cleanup
  292. (bury-buffer (get-buffer "*gnuplot*"))(delete-file data-file))))
  293. (provide 'org-plot)
  294. ;;; org-plot.el ends here