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