1 #+OPTIONS: H:3 num:nil toc:t \n:nil ::t |:t ^:t -:t f:t *:t tex:t d:(HIDE) tags:not-in-toc
2 #+STARTUP: align fold nodlcheck hidestars oddeven lognotestate
3 #+SEQ_TODO: TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
4 #+TAGS: Write(w) Update(u) Fix(f) Check(c)
5 #+TITLE: Plotting tables in Org-Mode using org-plot
7 #+EMAIL: schulte.eric@gmail.com
10 #+CATEGORY: worg-tutorial
12 # This file is the default header for new Org files in Worg. Feel free
13 # to tailor it to your needs.
15 [[file:../index.org][{Back to Worg's index}]]
19 This tutorial provides instructions for installing and using org-plot
20 as well as a complete overview of the org-plot options and
21 demonstrations of some of the many types of graphs which can be
22 generated automatically from tables in your org-mode files using
25 While graphs will be included in the html version of this tutorial, if
26 you would like to play along at home you can download the original org
27 file here [[http://repo.or.cz/w/Worg.git/blob_plain/HEAD:/org-tutorials/org-plot.org][org-plot.org]].
33 Org-plot uses Gnuplot as well as the Emacs Gnuplot-mode to power its
34 graphing. To download and install these two requirements see the
38 - Gnuplot :: [[http://www.gnuplot.info/]]
39 - Gnuplot-Mode :: [[http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html]]
42 First we need to make sure that org-plot is on your system and
43 available to emacs. If you have a recent version of org-mode (version
44 6.07 or later) then org-plot is already included. Otherwise you can
45 grab the latest org-plot.el from [[http://github.com/eschulte/org-plot/tree/master][github/eschulte/org-plot]]. Once you
46 have org-plot loaded it may be useful to bind the main plotting
47 command =org-plot/gnuplot= to a key chord, I use =C-M-g= for the
48 mnemonic "graph" which can be done by executing the following elisp
49 snippet. This command will be the only org-plot command needed for
50 the remainder of this tutorial.
52 [[elisp:(local-set-key "\M-\C-g" 'org-plot/gnuplot)]]
54 Alright, we're now ready to start generating some graphs.
57 ** 2d plots (lines and histograms)
59 First, to plot the following table use the key sequence defined above
60 =C-M-g=. This will call =org-plot/gnuplot= which finds and plots the
61 nearest table. The options specified in any =#+PLOT= lines above the
62 table are read and applied to the plot. Notice that the second
63 =#+PLOT:= line specifies labels for each column, if this line is
64 removed the labels will default to the column headers in the table,
65 try removing this line and re-plotting.
67 [[file:../images/org-plot/example-1.png]]
69 For a complete list of all of the options and their meanings see the
70 [[options]] section at the end of this file. For more information on
71 gnuplot options see [[http://gnuplot.sourceforge.net/documentation.html][the gnuplot documentation]], nearly all gnuplot
72 options should be accessible through org-plot.
74 #+PLOT: title:"example table" ind:1 type:2d with:lines
75 #+PLOT: labels:("first new label" "second column" "last column")
76 #+TBLNAME:org-plot-example-1
77 | independent var | first dependent var | second dependent var |
78 |-----------------+---------------------+----------------------|
79 | 0.1 | 0.425 | 0.375 |
80 | 0.2 | 0.3125 | 0.3375 |
81 | 0.3 | 0.24999993 | 0.28333338 |
82 | 0.4 | 0.275 | 0.28125 |
84 | 0.6 | 0.25833338 | 0.24999993 |
85 | 0.7 | 0.24642845 | 0.23928553 |
86 | 0.8 | 0.23125 | 0.2375 |
87 | 0.9 | 0.23333323 | 0.2333332 |
89 | 1.1 | 0.20909075 | 0.22272708 |
90 | 1.2 | 0.19999998 | 0.21458333 |
91 | 1.3 | 0.19615368 | 0.21730748 |
92 | 1.4 | 0.18571433 | 0.21071435 |
93 | 1.5 | 0.19000008 | 0.2150001 |
94 | 1.6 | 0.1828125 | 0.2046875 |
95 | 1.7 | 0.18088253 | 0.1985296 |
96 | 1.8 | 0.17916675 | 0.18888898 |
97 | 1.9 | 0.19342103 | 0.21315783 |
98 | 2 | 0.19 | 0.21625 |
99 | 2.1 | 0.18214268 | 0.20714265 |
100 | 2.2 | 0.17727275 | 0.2022727 |
101 | 2.3 | 0.1739131 | 0.1989131 |
102 | 2.4 | 0.16770833 | 0.1916667 |
103 | 2.5 | 0.164 | 0.188 |
104 | 2.6 | 0.15769238 | 0.18076923 |
105 | 2.7 | 0.1592591 | 0.1888887 |
106 | 2.8 | 0.1598214 | 0.18928565 |
107 | 2.9 | 0.15603453 | 0.1844828 |
110 Org-plot can also produce histograms from 2d data, plot the following
111 table. Notice that the column specified as =ind= contains textual
112 non-numeric data, when this is the case org-plot will use the data as
113 labels for the x-axis using the gnuplot =xticlabels()= function.
115 [[file:../images/org-plot/example-2.png]]
117 #+PLOT: title:"Citas" ind:1 deps:(3) type:2d with:histograms set:"yrange [0:]"
118 | Sede | Max cites | H-index |
119 |-----------+-----------+---------|
120 | Chile | 257.72 | 21.39 |
121 | Leeds | 165.77 | 19.68 |
122 | São Paolo | 71.00 | 11.50 |
123 | Stockholm | 134.19 | 14.33 |
124 | Morelia | 257.56 | 17.67 |
127 For another example of plotting histograms instead of lines, change
128 the following options on the first table on this page, and replot
129 1) remove the =ind:1= option
130 2) replace the =with:lines= option with =with:histograms=
132 [[file:../images/org-plot/example-3.png]]
136 There are also some functions for plotting 3d or grid data. To see an
137 example of a grid plot call org-plot/gnuplot =C-M-g= which will plot
138 the following table as a grid.
140 [[file:../images/org-plot/example-4.png]]
142 To see the effect of =map= try setting it to =t=, and then
145 [[file:../images/org-plot/example-5.png]]
147 #+PLOT: type:grid map:t title:"org mode"
148 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
149 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
150 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
151 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
152 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
153 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
154 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 0 |
155 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
156 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
157 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
158 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
159 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
160 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
161 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
163 Plotting grids also respects the independent variable (=ind:=) option,
164 and uses the values of the independent row to label the resulting
165 graph. The following example borrows a short description of org-mode
166 from Bernt Hansen on the mailing list (a more practical usage would
167 label every single row with something informative).
169 [[file:../images/org-plot/example-6.png]]
171 #+PLOT: type:grid map:t title:"Org-Mode" ind:1
172 | text | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
173 | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
174 | plain | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
175 | | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
176 | in | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
177 | | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 0 |
178 | everything | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
179 | | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
180 | track | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
181 | | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
182 | and | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
183 | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
184 | Organize | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
188 Finally the last type of graphing currently supported is 3d graphs of
189 data in a table. This will probably require some more knowledge of
190 gnuplot to make full use of the many options available.
192 [[file:../images/org-plot/example-7.png]]
194 For some simple demonstrations try the following graph using some
195 different =with:= options =with:points=, =with:lines=, and
198 [[file:../images/org-plot/example-8.png]]
200 #+PLOT: type:3d with:pm3d
201 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
202 | 0 | 2 | 2 | 2 | 2 | 2 | 0 |
203 | 0 | 2 | 3 | 3 | 3 | 2 | 0 |
204 | 0 | 2 | 3 | 4 | 3 | 2 | 0 |
205 | 0 | 2 | 3 | 3 | 3 | 2 | 0 |
206 | 0 | 2 | 2 | 2 | 2 | 2 | 0 |
207 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
209 ** Setting Axis Titles
210 The question of the proper syntax for setting axis labels via org-plot
211 has occurred on the mailing list.[fn:1] The answer is to use this:
213 #+PLOT: set:"xlabel 'Name'" set:"ylabel 'Name'"
221 Gnuplot options (see [[http://gnuplot.sourceforge.net/documentation.html][the gnuplot documentation]]) accessible through
222 `org-plot', common gnuplot options are specifically supported, while
223 all other options are accessible through specification of generic set
224 commands, script lines, or specification of custom script files.
225 Possible options are...
227 - set :: specify any gnuplot option to be set when graphing
228 - title :: specify the title of the plot
229 - ind :: specify which column of the table to use as the x axis
230 - deps :: specify the columns to graph as a lisp style list,
231 surrounded by parenthesis and separated by spaces for
232 example =dep:(3 4)= to graph the third and fourth columns
233 (defaults to graphing all other columns aside from the ind
235 - type :: specify whether the plot will be '2d' '3d or 'grid'
236 - with :: specify a with option to be inserted for every col being
237 plotted (e.g. lines, points, boxes, impulses, etc...)
239 - file :: if you want to plot to a file specify the path to the
241 - labels :: list of labels to be used for the deps (defaults to column
242 headers if they exist)
243 - line :: specify an entire line to be inserted in the gnuplot script
244 - map :: when plotting 3d or grid types, set this to true to graph a
245 flat mapping rather than a 3d slope
246 - script :: if you want total control you can specify a script file
247 (place the file name inside quotes) which will be used to
248 plot, before plotting every instance of $datafile in the
249 specified script will be replaced with the path to the
250 generated data file. Note even if you set this option you
251 may still want to specify the plot type, as that can
252 impact the content of the data file.
253 - timefmt :: if there is time and/or date data to be plotted, set the
254 format. For example, =timefmt:%Y-%m-%d= if the data look
261 [fn:1] http://www.mail-archive.com/emacs-orgmode@gnu.org/msg08669.html