org-plot.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 OPTIONS 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.
  84. Accepts an optional property list PARAMS, to which the options
  85. will be 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 for gnuplot.
  107. This means, in a format appropriate for grid plotting by gnuplot.
  108. PARAMS specifies which columns of TABLE should be plotted as independant
  109. and dependant variables."
  110. (interactive)
  111. (let* ((ind (- (plist-get params :ind) 1))
  112. (deps (if (plist-member params :deps)
  113. (mapcar (lambda (val) (- val 1)) (plist-get params :deps))
  114. (let (collector)
  115. (dotimes (col (length (first table)))
  116. (setf collector (cons col collector)))
  117. collector)))
  118. row-vals (counter 0))
  119. (when (>= ind 0) ;; collect values of ind col
  120. (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter))
  121. (cons counter (nth ind row))) table)))
  122. (when (or deps (>= ind 0)) ;; remove non-plotting columns
  123. (setf deps (delq ind deps))
  124. (setf table (mapcar (lambda (row)
  125. (dotimes (col (length row))
  126. (unless (memq col deps)
  127. (setf (nth col row) nil)))
  128. (delq nil row))
  129. table)))
  130. ;; write table to gnuplot grid datafile format
  131. (with-temp-file data-file
  132. (let ((num-rows (length table)) (num-cols (length (first table)))
  133. front-edge back-edge)
  134. (flet ((gnuplot-row (col row value)
  135. (setf col (+ 1 col)) (setf row (+ 1 row))
  136. (format "%f %f %f\n%f %f %f\n"
  137. col (- row 0.5) value ;; lower edge
  138. col (+ row 0.5) value))) ;; upper edge
  139. (dotimes (col num-cols)
  140. (dotimes (row num-rows)
  141. (setf back-edge
  142. (concat back-edge
  143. (gnuplot-row (- col 1) row (string-to-number
  144. (nth col (nth row table))))))
  145. (setf front-edge
  146. (concat front-edge
  147. (gnuplot-row col row (string-to-number
  148. (nth col (nth row table)))))))
  149. ;; only insert once per row
  150. (insert back-edge) (insert "\n") ;; back edge
  151. (insert front-edge) (insert "\n") ;; front edge
  152. (setf back-edge "") (setf front-edge "")))))
  153. row-vals))
  154. (defun org-plot/gnuplot-script (data-file num-cols params)
  155. "Write a gnuplot script to DATA-FILE respecting the options set in PARAMS.
  156. NUM-COLS controls the number of columns plotted in a 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. ;; facade functions
  230. ;;;###autoload
  231. (defun org-plot/gnuplot (&optional params)
  232. "Plot table using gnuplot. Gnuplot options can be specified with PARAMS.
  233. 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 (if (eq (first table) 'hline) (second table)
  254. (first table)))))
  255. (while (equal 'hline (first table)) (setf table (cdr table)))
  256. (when (equal (second table) 'hline)
  257. (setf params (plist-put params :labels (first table))) ;; headers to labels
  258. (setf table (delq 'hline (cdr table)))) ;; clean non-data from table
  259. ;; collect options
  260. (save-excursion (while (and (equal 0 (forward-line -1))
  261. (looking-at "#\\+"))
  262. (setf params (org-plot/collect-options params))))
  263. ;; dump table to datafile (very different for grid)
  264. (case (plist-get params :plot-type)
  265. ('2d (org-plot/gnuplot-to-data table data-file params))
  266. ('3d (org-plot/gnuplot-to-data table data-file params))
  267. ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data
  268. table data-file params)))
  269. (when y-labels (plist-put params :ylabels y-labels)))))
  270. ;; check for text ind column
  271. (let ((ind (- (plist-get params :ind) 1)))
  272. (when (and (>= ind 0) (equal '2d (plist-get params :plot-type)))
  273. (if (> (length
  274. (delq 0 (mapcar
  275. (lambda (el)
  276. (if (string-match org-table-number-regexp el)
  277. 0 1))
  278. (mapcar (lambda (row) (nth ind row)) table)))) 0)
  279. (plist-put params :textind t))))
  280. ;; write script
  281. (with-temp-buffer
  282. (if (plist-get params :script) ;; user script
  283. (progn (insert-file-contents (plist-get params :script))
  284. (goto-char (point-min))
  285. (while (re-search-forward "$datafile" nil t)
  286. (replace-match data-file nil nil)))
  287. (insert
  288. (org-plot/gnuplot-script data-file num-cols params)))
  289. ;; graph table
  290. (gnuplot-mode)
  291. (gnuplot-send-buffer-to-gnuplot))
  292. ;; cleanup
  293. (bury-buffer (get-buffer "*gnuplot*"))(delete-file data-file))))
  294. (provide 'org-plot)
  295. ;;; org-plot.el ends here