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