update upload data
[PyX.git] / faq / pyxfaq.tex
blob472aae9c3a7ed78c6da9f79d0f598aaa61a0a2c9
1 % $Id$
2 \documentclass[11pt,DIV14]{scrartcl}
3 \usepackage[latin1]{inputenc}
4 \usepackage{url}
5 \usepackage{mathptmx}
6 %\usepackage[all,comments]{glifaq}
7 \usepackage[answered]{glifaq}
8 \usepackage[pdftex]{hyperref}
9 \hypersetup{pdftitle={PyX FAQ}%
10 ,pdfauthor={\textcopyright\ Gert-Ludwig Ingold
11 <gert.ingold@physik.uni-augsburg.de>}%
12 ,colorlinks=true%
13 ,linkcolor=blue}
14 \def\pyxversion{\input{pyxversion}}
15 \begin{document}
17 \begin{center}
18 \LARGE\sffamily Some frequently and\\
19 not so frequently asked questions\\
20 about \PyX
21 \par
22 {\small\sffamily (version \pyxversion)}\\[1truecm]
23 \large
24 Gert-Ludwig Ingold \par
25 \href{mailto:gert.ingold@physik.uni-augsburg.de}{\url{<gert.ingold@physik.uni-augsburg.de>}}
26 \end{center}
27 \toc
29 \vspace{2truecm}
30 \section*{Acknowledgements}
31 The following persons have in one way or the other, e.g.\ by asking good
32 questions or providing answers, contributed to this FAQ:\\
33 Walter Brisken, Alejandro Gaita-Arinyo, Pierre Joyot, Jörg Lehmann,
34 John Owens, Michael Schindler, Gerhard Schmid, Andr{\'e} Wobst.
35 \newpage
37 \section{General aspects of \PyX}
38 \question{a}{The name of the game}
40 {Originally, the name \PyX{} was constructed as a combination of
41 \textbf{P}ostscript, i.e.\ the first output format supported by \PyX{},
42 P\textbf{y}thon, i.e.\ the language in which \PyX{} is written, and
43 Te\textbf{X}, i.e.\ the program which \PyX{} uses for typesetting purposes.
44 Actually, the title of this question is a tribute to \TeX{} because it is
45 taken from the first chapter of the \TeX{}book\footnote{D.~Knuth, \textit{The
46 \TeX{}book} (Addison-Wesley, 1984)} where the origin of the name \TeX{} and its
47 pronunciation are explained.
49 Despite the ties between \TeX{} and \PyX{}, their pronunciation is quite
50 different. According to the developers of \PyX{}, it should be pronounced as
51 \tipagraph{[pYks]}{tipapyks}. Please do not pronounce it as
52 \tipagraph{[pYx]}{tipapyx} or \tipagraph{[pY\c c]}{tipapych}.
55 \question{a}{Where do I get the latest version of \PyX?}
57 {\label{q:where_do_I_get_PyX}
58 The current release of \PyX{} (as well as older ones) is freely available
59 from \url{http://pyx.sourceforge.net} where also a CVS repository with the
60 latest patches can be found. Possibly older versions of \PyX{} are
61 also available as package for
62 various Linux distributions: see, for instance,
63 \url{http://packages.debian.org/testing/python/python-pyx.html} for
64 information on the \PyX{} package in Debian GNU/Linux,
65 \url{http://packages.gentoo.org/ebuilds/?pyx-0.7.1} for a Gentoo Linux
66 ebuild, and
67 \url{http://www.novell.com/products/linuxpackages/professional/python-pyx.html}
68 for the \PyX{} package in the SUSE LINUX professional distribution.
71 \question{c}{How can I determine the version of \PyX{} running on my
72 machine?}
74 {Start a python session (usually by typing \texttt{python} at the system
75 prompt) and then type the following two commands (\texttt{>>>} is the python
76 prompt)
77 \begin{progcode}
78 >>> import pyx\\
79 >>> pyx.\us\us{}version\us\us
80 \end{progcode}
83 \question{a}{How can I access older versions of \PyX?}
85 {As at present it is not guaranteed that \PyX{} is backward compatible, it may
86 be desirable to access an older version of \PyX{} instead of adapting older
87 code to the current version of \PyX. In order to do that, one needs the
88 corresponding \PyX{} package (see \uaref{q:where_do_I_get_PyX} if you need to
89 download it), which should be unpacked below a directory, e.g.\
90 \texttt{/home/xyz/Python}, where you want to keep the various \PyX{} versions.
91 This will result in a subdirectory with a name like \texttt{PyX-0.8} which
92 contains the contents of the corresponding package. You can then ask Python to
93 first look in the appropriate directory before looking for the current version
94 of \PyX{} by inserting the following code (appropriately modified according
95 to your needs) at the beginning of your program before importing the \PyX{}
96 module:
97 \begin{progcode}
98 import sys\\
99 sys.path.insert(0, "/home/xyz/Python/PyX-0.8")
100 \end{progcode}
101 Including appropriate lines even if the current version of \PyX{} is used,
102 might turn out to be helpful when the current version has become an old
103 version (unless you have no difficulties determining the \PyX{} version by
104 looking at your code).
106 If your operating system supports path expansion, you might use as an
107 alternative:
108 \begin{progcode}
109 import sys, os\\
110 sys.path.insert(0, os.path.expanduser("\char126/Python/PyX-0.8"))
111 \end{progcode}
112 which will expand the tilde to your home directory.
115 \question{c}{Does \PyX{} run under my favorite operating system?}
117 {Yes, if you have installed Python (\uaref{q:what_is_python})
118 and \TeX{} (\uaref{q:what_is_tex}). Both are available for
119 a large variety of operating systems so chances are pretty good that you will
120 get \PyX{} to work on your system.
123 \question{c}{Under which versions of Python will \PyX{} run?}
125 {\PyX{} is supposed to work with Python 2.1 and above. However, most of the
126 development takes place under the current production version of Python
127 (2.4.1 by the time of this writing) and thus \PyX{} is better tested
128 with this version. On the other hand, the examples and tests are
129 verified to run with Python 2.1 and above using the latest bugfix
130 releases. \PyX{} will not work with earlier Python versions due
131 to missing language features.
133 The version of your Python interpreter can be determined by calling
134 it with the option \texttt{-V}. Alternatively, you can simply start the
135 interpreter and take a look at the startup message. Note that there may be
136 different versions of Python installed on your system at the same time. The
137 default Python version need not be the same for all users.
140 \question{a}{Does \PyX{} provide a GUI to view the produced image?}
142 {No, \PyX{} itself does not provide a means to view the produced image. The
143 result of a \PyX{} run is an EPS (= Encapsulated PostScript) file, a
144 PS (= PostScript) file or a PDF (= Portable Document Format) file, which can
145 be viewed, printed or imported into other applications.
147 There are several means of viewing PS and EPS files. A common way
148 would be to use \texttt{ghostview} which provides a user interface to
149 the PostScript interpreter \texttt{ghostscript}. More information
150 about this software, which is available for a variety of platforms,
151 can be found at \url{http://www.cs.wisc.edu/~ghost/}. If you do not
152 own a printer which is capable of printing PostScript files directly,
153 \texttt{ghostscript} may also be useful to translate PS and EPS files
154 produced by \PyX{} into something your printer will understand.
156 PDF files can be viewed by means of the \texttt{Adobe
157 Reader\textsuperscript{\textregistered}}
158 available from
159 \url{http://www.adobe.com/products/acrobat/readstep2.html}. On systems
160 running X11, \texttt{xpdf} might be an alternative. It is available from
161 \url{http://www.foolabs.com/xpdf/}.}
163 \question{a}{I am a Gnuplot user and want to try \PyX. Where can I get some
164 help?}
167 There exists a tutorial by Titus Winters which explains how to perform
168 standard Gnuplot tasks with \PyX. The tutorial can be found at
169 \url{http://www.cs.ucr.edu/~titus/pyxTutorial/}.
172 \question{a}{Where can I get help if my question is not answered in this
173 FAQ?}
175 {The \PyX{} sources contain a reference manual which is also available
176 online at \url{http://pyx.sourceforge.net/manual/}. Furthermore, there
177 exists a set of examples demonstrating various features of \PyX, which is
178 available in the sources or can be browsed at \url{http://pyx.sourceforge.net/examples.html}.
179 If the feature you are looking for is among them, using the appropriate part
180 of the example code or adapting it for your purposes may help.
182 There is also a user discussion list about \PyX{} which you can subscribe to
183 at \url{http://lists.sourceforge.net/lists/listinfo/pyx-user}. The archive of
184 the discussion list is available at \url{http://sourceforge.net/mailarchive/forum.php?forum_name=pyx-user}.
186 Finally, it might be worth checking \url{http://pyx.sourceforge.net/pyxfaq.pdf}
187 for an updated version of this FAQ.
190 \section{Python}
192 \question{c}{What is Python?}
194 {\label{q:what_is_python}
195 From \url{www.python.org}:
196 \begin{quote}
197 Python is an \textit{interpreted, interactive, object-oriented} programming
198 language. It is often compared to Tcl, Perl, Scheme or Java.
200 Python combines remarkable power with very clear syntax. It has modules,
201 classes, exceptions, very high level dynamic data types, and dynamic typing.
202 There are interfaces to many system calls and libraries, as well as to various
203 windowing systems (X11, Motif, Tk, Mac, MFC). New built-in modules are easily
204 written in C or C++. Python is also usable as an extension language for
205 applications that need a programmable interface.
207 The Python implementation is portable: it runs on many brands of UNIX, on
208 Windows, OS/2, Mac, Amiga, and many other platforms. If your favorite system
209 isn't listed here, it may still be supported, if there's a C compiler for it.
210 Ask around on \href{news:comp.lang.python}{news:comp.lang.python} --- or just
211 try compiling Python yourself.
213 The Python implementation is
214 \href{http://www.python.org/doc/Copyright.html}{copyrighted}
215 but \textbf{freely usable and distributable, even for commercial use}.
216 \end{quote}
219 \question{a}{Where can I learn more about Python?}
221 {The place to start is \url{www.python.org} where you will find plenty of
222 information on Python including tutorials.
225 \question{c}{What do I need to import in order to use \PyX?}
227 {It is recommended to begin your Python code with
228 \begin{progcode}
229 from pyx import *
230 \end{progcode}
231 when using \PyX. This allows you for example to write simply
232 \texttt{graph.graphxy}
233 instead of \texttt{pyx.graph.graphxy}. The following modules will be loaded:
234 \texttt{attr}, \texttt{box}, \texttt{bitmap}, \texttt{canvas}, \texttt{color}, \texttt{connector},
235 \texttt{deco}, \texttt{deformer}, \texttt{document}, \texttt{epsfile}, \texttt{graph}, \texttt{path},
236 \texttt{pattern}, \texttt{style}, \texttt{trafo}, \texttt{text}, and \texttt{unit}.
238 For convenience, you might import specific objects of a module like in
239 \begin{progcode}
240 from graph import graphxy
241 \end{progcode}
242 which allows you to write \texttt{graphxy()} instead of \texttt{graph.graphxy()}.
244 All code segments in this document assume that the import line mentioned in
245 the first code snippet is present.
248 \question{a}{What is a raw string and why should I know about it when
249 using \PyX?}
251 {\label{q:raw_string}
252 The backslash serves in standard Python strings to start an escape sequence.
253 For example {\cs n} corresponds to a newline character. On the other hand,
254 \TeX{} and \LaTeX{}, which do the typesetting in \PyX, use the backslash to
255 indicate the start of a command. In order to avoid the standard interpretation,
256 the string should be marked as a raw string by prepending it by an \texttt{r}
257 like in
258 \begin{progcode}
259 c.text(0, 0, r"\${\cs alpha}{\cs beta}{\cs gamma}\$")
260 \end{progcode}
263 \section{General aspects of plotting with \PyX}
265 \question{a}{How do I generate multipage output?}
268 With versions 0.8 and higher it is possible to produce multipage output,
269 i.e. a Postscript or PDF file containing more than one page. In order to
270 achieve this, one creates pages by drawing on a canvas as usual and
271 appends them in the desired order to a document from which Postscript or
272 PDF output is produced. The following example serves as an illustration:
273 \begin{progcode}
274 from pyx import *\\
276 d = document.document()\\
277 for i in range(3):\\
278 ~~~~c = canvas.canvas()\\
279 ~~~~c.text(0, 0, "page \%i" \% (i+1))\\
280 ~~~~d.append(document.page(c, paperformat=document.paperformat.A4,\\
281 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~margin=3*unit.t\_cm,\\
282 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~fittosize=1))\\
283 d.writePSfile("multipage")
284 \end{progcode}
285 Here, \texttt{d} is the document into which pages are inserted by means
286 of the \texttt{append} method. When converting from a canvas to a document
287 page, the page properties like the paperformat are specified. In the last
288 line, output is produced from document \texttt{d}.
291 \section{Plotting of graphs}
293 \subsection{General aspects}
295 \question{c}{How do I generate a graph from data as simply as possible?}
297 {\label{q:mingraphdata}
298 Suppose that you have a data file \texttt{x.dat} containing values for
299 $x$ and $y$ in two columns. Then the following code will do the job
300 \begin{progcode}
301 from pyx import *\\
303 g = graph.graphxy(width=10)\\
304 g.plot(graph.data.file("x.dat", x=1, y=2))\\
305 g.writeEPSfile("x")
306 \end{progcode}
307 \texttt{graphxy} creates a canvas (called \texttt{g} in this example) onto
308 which the graph will be drawn and it sets the default behavior including the
309 axis. There is, however, no default value for the width of the graph. In
310 \texttt{plot} you have to specify the name of the data file and the columns
311 from which the data should be taken. Finally, \texttt{writeEPSfile} will
312 generate the postscript file \texttt{x.eps} which you can view or print.
314 A minimal example is also provided in the \PyX{} distribution as
315 \path{examples/graphs/minimal.py}.
318 \question{a}{How do I generate a graph of a function as simply as possible?}
320 {\label{q:mingraphfunc}
321 The following example will draw a parabola:
322 \begin{progcode}
323 from pyx import *\\
325 g = graph.graphxy(width=10,\\
326 ~~~~~~~~~~~~~~~~~~x=graph.axis.linear(min=-2, max=2)\\
327 ~~~~~~~~~~~~~~~~~~)\\
329 g.plot(graph.data.function("y(x)=x**2"))\\
331 g.writeEPSfile("x")
332 \end{progcode}
333 Most of the code has been explained in \uaref{q:mingraphdata}. The main
334 difference is that here you need to specify minimum and maximum for the
335 $x$-axis so that \PyX{} knows in which range to evaluate the function.
337 Another, slightly more complex, example is also provided in the \PyX{}
338 distribution as \path{examples/graphs/piaxis.py}.
341 \question{a}{How can I stack graphs?}
343 {\PyX{} always needs a canvas to draw on. One possibility therefore consists
344 in creating a new canvas with
345 \begin{progcode}
346 c = canvas.canvas()
347 \end{progcode}
348 and inserting the graphs into this canvas with \texttt{c.insert(...)}. Here,
349 \texttt{...} has to be replaced by the name of the graph. Alternatively, the
350 canvas created with \texttt{graph.graphxy} for one of the graphs can be used
351 to insert the other graphs even if they will be positioned outside the
352 first graph.
354 The second issue to address is positioning of the graphs. By specifying
355 \texttt{xpos} and \texttt{ypos} when calling \texttt{graphxy}, you can
356 define the position of a graph. Later on, the position and size of a
357 graph \texttt{g} can be referred to as \texttt{g.xpos}, \texttt{g.ypos},
358 \texttt{g.width}, and \texttt{g.height} even if for example the height has
359 never been specified explicitly but is only defined by a \PyX{} default.
361 The following example shows how to put graph \texttt{gupper} above graph
362 \texttt{glower} on a canvas \texttt{c}:
363 \begin{progcode}
364 from pyx import *\\
365 from graph import graphxy\\
367 c = canvas.canvas()\\
369 glower = graphxy(width=10)\\
370 glower.plot(...)\\
371 c.insert(glower)\\
373 gupper = graphxy(width=10, ypos=glower.ypos+glower.height+2)\\
374 gupper.plot(...)\\
376 c.insert(gupper)\\
377 c.writeEPSfile(...)
378 \end{progcode}
379 where \texttt{...} has to be replaced by the appropriate information like
380 data and symbol specifications and the name of the output file. Here,
381 \texttt{c.insert} is used to actually insert the subcanvasses
382 for the graphs into the main canvas \texttt{c} and \texttt{c.writeEPSfile}
383 in the last line requests to write the contents of this canvas to a file.
385 %In order to suppress the labels of the $x$-axis of the upper graph, use
386 %\begin{progcode}
387 %myaxispainter = graph.axispainter(labelattrs=None)
389 %gupper = graph.graphxy(...,
390 % x=graph.axis.linear(...,
391 % part=graph.linpart(),
392 % painter=myaxispainter)
394 %\end{progcode}
397 \question{a}{How can I plot grid data?}
399 {\PyX{} offers support for plotting three-dimensional data as two-dimensional
400 color plots or grey-scale plots and of vector fields by providing ways to
401 plot rectangles and arrows in graphs.
403 We start by considering the task of creating a two-dimensional color plot by
404 plotting a number of filled rectangles. One first needs to create a data set
405 which consists of five entries per data point. These are the lower left corner
406 $(x_\mathrm{min},y_\mathrm{min})$ and the upper right corner
407 $(x_\mathrm{max},y_\mathrm{max})$ of the triangle and a value between 0 and 1
408 determining the color via a \PyX{} color palette. The following code gives an
409 idea of how to proceed:
410 \begin{progcode}
411 g.plot(graph.data.file("datafile.dat", xmin=1, xmax=2, ymin=3, ymax=4, color=5),\\
412 ~~~~~~~[graph.style.rect(color.palette.ReverseRainbow)]\\
413 ~~~~~~)\\
414 g.dodata()
415 \end{progcode}
416 Here, we assume that the data are stored in \texttt{datafile.dat} and the
417 columns contain $x_\mathrm{min}$, $x_\mathrm{max}$, $y_\mathrm{min}$,
418 $y_\mathrm{max}$, and the color value in this order. The columns are
419 numbered from 1, since the 0th column contains the line number. To
420 determine the color, we use the \texttt{ReverseRainbow} palette. The
421 last line instructs \PyX{} to plot the rectangles before plotting the
422 axes. Otherwise, the axes might be covered partially by the rectangles
423 and, in particular, the ticks might not be visible. Gray-scale plots
424 can easily be generated by specifying the palette \texttt{Gray} or
425 \texttt{ReverseGray} (cf.\ appendix C of the manual for a list of
426 predefined palettes).
428 At first sight, it seems surprising that plotting of grid data requires
429 the specification of four coordinates for the rectangle. The reason is that
430 this allows to draw rectangles of varying sizes which may help to reduce the
431 size of the postscript file by combining rectangles of the same color in
432 horizontal or vertical direction. For example, it may be sufficient to plot
433 a grey-scale image in a small number of grey shades and then combining
434 rectangles may be appropriate. Note, though, that this step is part of the
435 data creation and not preformed by \PyX{}. Another advantage of fully
436 specifying each rectangle is that it is straightforward to leave parts of the
437 graph blank.
439 The same ideas as for the color plot can be applied to plot vector fields where
440 each data point is represented by an arrow. In this case a data point is
441 specified by the position of the arrow, its size and its direction as indicated
442 in the following code snippet:
443 \begin{progcode}
444 g.plot(graph.data.file("datafile.dat"), x=1, y=2, size=3, angle=4),\\
445 ~~~~~~~[graph.style.arrow()]\\
446 ~~~~~~)
447 \end{progcode}
449 Complete code examples can be found in \path{examples/graphs/mandel.py} and
450 \path{examples/graphs/arrows.py}\,.
453 \question{a}{How can I access points in problem coordinates of a graph?}
455 {\label{q:problemcoord}
456 Sometimes it may be necessary to add graphical elements to a graph in addition
457 to the data or function(s) which have been plotted as described in
458 \uaref{q:mingraphdata} and \uaref{q:mingraphfunc}. For a graph instance
459 \texttt{g} the positioning can easily be done in canvas coordinates by making
460 use of the origin (\texttt{g.xpos}, \texttt{g.ypos}) and the width
461 (\texttt{g.width}) and height (\texttt{g.height}) of the graph.
463 Occasionally, it may be more convenient to specify the position of the
464 additional material in terms of problem coordinates. However, this requires
465 that the mapping from problem coordinates to canvas coordinates is known.
466 By default this is not the case before the content of the canvas is written
467 to the output which is too late for our purpose. One therefore needs to
468 explicitly instruct \PyX{} to determine this mapping. One possibility is to
469 ask \PyX{} to finish the graph by means of \texttt{g.finish()}. Now, problem
470 coordinates can be used to insert additional material which will end up in
471 front of the graph. If this is not desired, one should only fix the layout
472 of the graph by means of \texttt{g.dolayout()}. Then, the additional material
473 can be put onto the canvas before the graph is drawn and it will therefore
474 appear behind the graph.
476 The conversion of problem coordinates (\texttt{px}, \texttt{py}) to canvas
477 coordinates (\texttt{x}, \texttt{y}) is performed as follows:
478 \begin{progcode}
479 x, y = g.pos(px, py)
480 \end{progcode}
481 By default, the problem coordinates will refer to the ranges of the $x$ and $y$
482 axes. If several axes with different ranges exist, the
483 instances of the desired axes should be passed to the \texttt{pos} method by
484 means of the keyword arguments \texttt{xaxis} and \texttt{yaxis}.
486 We remark that the drawing of lines parallel to one of the axes at specific
487 problem coordinates can also be done by adapting the method described in
488 \uaref{q:zeroline}.
491 \question{t}{I would like a key for only some of my data sets. How do I do
492 that?}
497 \subsection{Axis properties}
499 \question{a}{How do I specify the tick increment?}
501 {In the partition of a linear axis, the increments associated with ticks,
502 subticks etc.\ can be specified as argument of \texttt{parter.linear}. In the
503 following example, ticks will be drawn at even values while subticks will
504 be drawn at all integers:
505 \begin{progcode}
506 from pyx.graph import axis\\
507 tg = graph.graphxy(width=10,\\
508 ~~~~~~~~~~~~~~~~~~~x=axis.linear(min=1, max=10,\\
509 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~parter=axis.parter.linear(tickdists=[2,1]))\\
510 ~~~~~~~~~~~~~~~~~~~)
511 \end{progcode}
514 \question{a}{How do I plot the zero line?}
517 \label{q:zeroline}
518 \PyX{} releases before 0.6 offered the possibility to stroke a zero line by
519 specifying \texttt{zeropathattrs} in the painter constructor. In more recent
520 releases, one proceeds as follows. First one has to fix the layout information
521 of the graph by means of the \texttt{finish} or \texttt{dolayout} method (see
522 \ref{q:problemcoord} for a more detailed explanation). Then, the
523 \texttt{xgridpath} or \texttt{ygridpath} method of a graph will return a grid
524 path parallel to the $y$ or $x$ axis, respectively, at the specified $y$ value.
525 As an example, a zero line in $x$ direction can be drawn as follows:
526 \begin{progcode}
527 g.finish()\\
528 g.stroke(g.ygridpath(0))
529 \end{progcode}
532 \question{a}{How can I add grid lines to a graph?}
535 Specifying \texttt{gridattrs} for the painter of an axis will generate grid
536 lines orthogonal to this axis. At least an empty list is needed like in
537 \begin{progcode}
538 g = graph.graphxy(width=10,\\
539 ~~~~~~~~~~~~~~~~~~x=graph.axis.linear(painter=graph.axis.painter.regular(gridattrs=[])),\\
540 ~~~~~~~~~~~~~~~~~~y=graph.axis.linear()\\
541 ~~~~~~~~~~~~~~~~~~)
542 \end{progcode}
543 where grid lines in vertical direction are drawn in default style.
545 Occassionally, one might want to draw grid lines corresponding to ticks and
546 subticks in a different style. This can be achieved by specifiying
547 changeable attributes using \texttt{changelist}. The following code
548 \begin{progcode}
549 my\_xpainter = graph.axis.painter.regular(gridattrs=\\
550 ~~~~~~~~~~~~~~~~~~~~[attr.changelist([style.linestyle.solid, style.linestyle.dashed])]\\
551 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)\\
552 my\_ypainter = graph.axis.painter.regular(gridattrs=\\
553 ~~~~~~~~~~~~~~~~~~~~[attr.changelist([color.rgb.red, color.rgb.blue])]\\
554 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)\\
556 g = graph.graphxy(width=10,\\
557 ~~~~~~~~~~~~~~~~~~x=graph.axis.linear(painter=my\_xpainter),\\
558 ~~~~~~~~~~~~~~~~~~y=graph.axis.linear(painter=my\_ypainter)\\
559 ~~~~~~~~~~~~~~~~~~)
560 \end{progcode}
561 will create vertical solid and dashed grid lines for ticks and subticks,
562 respectively. The horizontal grid lines will be red for ticks and blue for
563 subticks. The changeable attributes are applied in a cyclic manner. Therefore,
564 in this example grid lines at subsubticks would be plotted in the same style
565 as for ticks. If this is not desired, the list of attributes should be extended
566 by an appropriate third style. The keyword \texttt{None} will switch off
567 the respective level of grid lines in case you want to draw them only e.g.\
568 for ticks but not subticks.
571 \subsection{Data properties}
573 \question{a}{How do I choose the symbol and its attributes?}
575 {\label{q:choose_symbol}
576 Suppose a graph called \texttt{g} has been initialized, e.g.\ by using
577 \texttt{graph.graphxy}. Then, data and the style of their representation
578 in the graph are defined by calling \texttt{g.plot} like in the following
579 example in which filled circles are requested:
580 \begin{progcode}
581 g.plot(graph.data.file("test.dat"),\\
582 ~~~~~~~[graph.style.symbol(graph.style.symbol.circle, symbolattrs=[deco.filled])]\\
583 ~~~~~~~)
584 \end{progcode}
585 As another example, if the linewidth of the symbol is too thin for your
586 purposes, you could use something like:
587 \begin{progcode}
588 [graph.style.symbol(graph.style.symbol.plus,
589 symbolattrs=[style.linewidth.Thick])]\\
590 \end{progcode}
593 \question{a}{How do I choose the color of the symbols?}
595 {Colors are not properties of the symbol as such and can therefore not
596 be specified in \texttt{symbolattrs} directly. The color is rather related
597 to the plotting of the symbol as defined by \texttt{deco.stroked} or
598 \texttt{deco.filled}. With
599 \begin{progcode}
600 graph.style.symbol(graph.style.symbol.circle,\\
601 ~~~~~~~~~~~~~~~~~~~symbolattrs=[deco.stroked([color.rgb.red]),\\
602 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~deco.filled([color.rgb.green])]\\
603 ~~~~~~~~~~~~~~~~~~~)
604 \end{progcode}
605 you will obtain a circle filled in green with a red borderline.
608 \question{a}{How do I choose the line style?}
610 {If you do not want to use symbols, you can set the line style as in this
611 example
612 \begin{progcode}
613 g.plot(graph.data.file("test.dat"),\\
614 ~~~~~~~[graph.style.line([style.linewidth.Thin])]\\
615 ~~~~~~~)
616 \end{progcode}
617 where the linewidth is set.
619 If you also want to use symbols, you can combine the symbol and the
620 line style as in
621 \begin{progcode}
622 g.plot(graph.data.file("test.dat"),\\
623 ~~~~~~~[graph.style.line(lineattrs=[style.linewidth.Thin,\\
624 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~style.linestyle.dashed]),\\
625 ~~~~~~~~graph.style.symbol(graph.style.symbolline.circle,\\
626 ~~~~~~~~~~~~~~~~~~~~~~~~~~~symbolattrs=[deco.filled])
627 ~~~~~~~]
628 ~~~~~~)
629 \end{progcode}
630 to plot the symbols on top of a thin, dashed line. You may alter the
631 order of the styles to plot the line on top of the symbols.
634 \question{a}{How can I change the color of symbols or lines according to a
635 palette?}
637 {If several data sets should be plotted in different colors, one can specify
638 in \texttt{symbolattrs} and/or \texttt{lineattrs} a palette like
639 \texttt{color.palette.Rainbow}. Equidistant colors are chosen spanning the
640 palette from one end to the other. For example, for three data sets the
641 colors are chosen from the palette at $0., 0.5,$ and $1$. For the rainbow
642 palette, this would correspond to red, green, and blue, respectively.
644 In the following example, symbols vary in form and change their color according
645 to the rainbow palette at the same time as the connecting lines:
646 \begin{progcode}
647 mystyle = [graph.style.symbol(graph.style.symbol.changecircle,\\
648 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~symbolattrs=[color.palette.Rainbow]),\\
649 ~~~~~~~~~~~graph.style.line(lineattrs=[color.palette.Rainbow])]
650 \end{progcode}
651 See question~\ref{q:changelist} for a more complete example demonstrating
652 how to use this style definition and for a comment on the necessity of
653 defining \texttt{mystyle} (you are of course free to choose a different name).
656 \question{a}{How can I specify changing colors (or other attributes) for
657 symbols or lines?}
659 {\label{q:changelist}
660 In \texttt{symbolattrs} and/or \texttt{lineattrs} so-called changelist can
661 be used. As an example
662 \begin{progcode}
663 mystyle = graph.style.symbol(symbolattrs=\\
664 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~[attr.changelist([color.rgb.red, color.rgb.green])])\\
665 g.plot(graph.data.file("x.dat", x=1, y=2), [mystyle])\\
666 g.plot(graph.data.file("y.dat", x=1, y=2), [mystyle])\\
667 g.plot(graph.data.file("z.dat", x=1, y=2), [mystyle])
668 \end{progcode}
669 will switch between red and green symbols each time a new data set is
670 plotted. Several changelists can be specified. They are cycled independently
671 and need not be of the same length. It should be noted that the definition of
672 \texttt{mystyle} in this example ensures that there is only one instance of
673 the definition of \texttt{symbolattrs}. Putting an explicit definition of
674 \texttt{symbolattrs} in each call to \texttt{plot} would not lead to the
675 desired result because each time a new instance would be created which then
676 starts with the first item in the changelist.
678 It may be necessary to repeat attributes
679 in order that several changelists cooperate to produce the desired result.
680 A common situation is that one would like to cycle through a list of symbols
681 which should be used in alternating colors. This can be achieved with
682 the following code:
683 \begin{progcode}
684 mystyle = graph.style.symbol(\\
685 ~~~~~~~~~~~~~~~~graph.style.symbol.changetriangletwice,\\
686 ~~~~~~~~~~~~~~~~symbolattrs=[attr.changelist([color.rgb.red, color.rgb.green])])
687 \end{progcode}
688 which will produce a red triangle, a green triangle, a red circle, a green
689 circle and so on for diamond and square because \texttt{changetriangletwice}
690 lists each symbol twice. If instead of changing between colors
691 one would like to change between filled and open symbols, one can make use of
692 a predefined changelist
693 \begin{progcode}
694 mystyle = graph.style.symbol(\\
695 ~~~~~~~~~~~~~~~~graph.style.symbol.changetriangletwice,\\
696 ~~~~~~~~~~~~~~~~symbolattrs=[graph.style.symbol.changefilledstroked])
697 \end{progcode}
700 \section{Other plotting tasks}
702 \question{a}{How can I rotate text?}
704 {Text can be written at an arbitrary angle by specifying the appropriate
705 transformation as an attribute. The command
706 \begin{progcode}
707 c.text(0, 0, "Text", [trafo.rotate(60)])
708 \end{progcode}
709 will write at an angle of 60 degrees relative to the horizontal axis. If no
710 pivot is specified (like in this example), the text is rotated around the
711 reference point given in the first two arguments of \texttt{text}. In the
712 following example, the pivot coincides with the center of the text:
713 \begin{progcode}
714 c.text(0, 0, "Text", [text.halign.center,text.valign.middle,trafo.rotate(60)])
715 \end{progcode}
718 \question{a}{How can I clip a canvas?}
720 {In order to use only a part of a larger canvas, one may want to clip it. This
721 can be done by creating a clipping object which is used when creating a canvas
722 instance:
723 \begin{progcode}
724 clippath = path.circle(0.,0.,1.)\\
725 clipobject = canvas.clip(clippath)\\
726 c = canvas.canvas([clipobject])
727 \end{progcode}
728 In this example, the clipping path used to define the clipping object is a
729 circle.
731 \section{\TeX{} and \LaTeX{}}
733 \subsection{General aspects}
735 \question{a}{What is \TeX/\LaTeX{} and why do I need it?}
737 {\label{q:what_is_tex}
738 \TeX{} is a high quality typesetting system developed by Donald E. Knuth
739 which is available for a wide variety of operating systems. \LaTeX{} is a
740 macro package originally developed by Leslie Lamport which makes life with
741 \TeX{} easier, in particular for complex typesetting tasks. The current
742 version of \LaTeX{} is referred to as \LaTeXe{} and offers e.g.\ improved
743 font selection as compared to the older \LaTeX{} 2.09 which should no longer
744 be used.
746 All typesetting tasks in \PyX{} are finally handed over to \TeX{} (which is the
747 default) or \LaTeX{}, so that \PyX{} cannot do without it. On the other hand,
748 the capabilities of \TeX{} and \LaTeX{} can be used for complex tasks where
749 both graphics and typesetting are needed.
752 \question{a}{I don't know anything about \TeX{} and \LaTeX{}. Where can I read
753 something about it?}
755 {\label{q:intro_tex_latex}
756 Take a look at CTAN (\uaref{q:ctan}) where in \ctan{info}
757 you may be able to find some useful information. There exists for example
758 ``A Gentle Introduction to \TeX'' by M.~Doob (\ctan{gentle/gentle.pdf}) and
759 ``The Not So Short Introduction to \LaTeXe''
760 (\ctan{info/lshort/english/lshort.pdf}) by T.~Oetiker et al. The latter has
761 been translated into a variety of languages among them korean (which you will
762 not be able to read unless you have appropriate fonts installed) and mongolian.
764 Of course, it is likely that these documents will go way beyond what you
765 will need for generating graphics with \PyX{} so you don't have to read all
766 of it (unless you want to use \TeX{} or \LaTeX{} for typesetting which can be
767 highly recommended).
769 There exists also a number of FAQs on \TeX{} at \ctan{help}.
772 \question{a}{What is CTAN?}
774 {\label{q:ctan}
775 CTAN is the Comprehensive TeX Archive Network where you will find almost
776 everything related to \TeX{} and friends. The main CTAN servers are
777 \url{tug.ctan.org}, \url{dante.ctan.org}, and \url{cam.ctan.org}. A list of
778 FTP mirrors can be found at \ctan{CTAN.sites}.
780 In this FAQ, \texttt{CTAN:} refers to the root of an anonymous ftp CTAN tree,
781 e.g.\ \url{ftp://ctan.tug.org/tex-archive/},
782 \url{ftp://ftp.dante.de/tex-archive/},
783 and \url{ftp://ftp.tex.ac.uk/tex-archive/}. The links to CTAN in this document
784 point to one of these servers but you might consider using a FTP mirror closer
785 to you in order to reduce traffic load.
788 \question{a}{Is there support for Con\TeX{}t?}
790 {No, and as far as I know there no plans to provide it in the near future.
791 Given the close ties between Con\TeX{}t and Meta\-Post, Con\TeX{}t users
792 probably prefer to stick with the latter anyway.
795 \subsection{\TeX{} and \LaTeX{} commands useful for \PyX}
797 \question{a}{How do I get a specific symbol with \TeX{} or \LaTeX?}
799 {A list of mathematical symbols together with the appropriate command name
800 can be found at \ctan{info/symbols/math/symbols.ps}. A comprehensive list
801 containing more than 2500 symbols for use with \LaTeX{} can be obtained from
802 \ctan{info/symbols/comprehensive/symbols-a4.pdf}. In some cases it might be
803 necessary to install fonts or packages available from CTAN
804 (\uaref{q:ctan}).
807 \subsection{\TeX{} and \LaTeX{} errors}
809 \question{a}{Undefined control sequence \cs{usepackage}}
811 {\label{q:undefined_usepackage}
812 The command \cs usepackage is specific to \LaTeX{}. Since by default \PyX{}
813 uses \TeX{}, you have to specify the correct mode:
814 \begin{progcode}
815 text.set(mode="latex")
816 \end{progcode}
819 \question{a}{Undefined control sequence \cs{frac}}
821 {\label{q:undefined_frac}
822 The command \cs frac is only available in \LaTeX{}. In \TeX{} you should
823 use \texttt{\cb{a\cs over b}} in math mode to produce ${a\over b}$. As an
824 alternative you may ask for the \LaTeX{} mode as explained in
825 \ref{q:undefined_usepackage}.
828 \question{a}{Missing \$ inserted}
830 {You have specified \TeX- or \LaTeX-code which is only valid in math mode.
831 Typical examples are greek symbols, sub- and superscripts or fractions.
833 On the \PyX{} level, you can specify math mode for the whole string by using
834 \texttt{text.mathmode} as in
835 \begin{progcode}
836 c.text(0, 0, r"{\cs alpha}", text.mathmode)
837 \end{progcode}
838 Keep also in mind that the standard Python interpretation of the backslash as
839 introducing escape sequences needs to be prevented
840 \uaref{q:raw_string}.
842 On the \TeX/\LaTeX{} level you should enclose the commands requiring math
843 mode in \$'s. As an example, \texttt{\$\cs alpha\us i\hat j\$} will produce
844 $\alpha_i^j$. This allows you to specify math mode also for substrings. There
845 exist other ways to specify math mode in \TeX{} and \LaTeX{} which are
846 particularly useful for more complex typesetting tasks. To learn more about
847 it, you should consult the documentation
848 \uaref{q:intro_tex_latex}.
851 \question{a}{Why do environments like itemize or eqnarray seem not to work?}
853 {An itemize environment might result in a \LaTeX{} error complaining about
854 a ``\texttt{missing \cs item}'' or an eqnarray might lead to a \LaTeX{} message
855 ``\texttt{missing \cs endgroup inserted}'' even though the syntax appears to be
856 correct. The \TeX{}nical reason is that in \PyX{} text is typeset in left-right
857 mode (LR mode) which does not allow linebreaks to occur. There are two ways out.
859 If the text material should go in a box of given width, a parbox can be used
860 like in the following example:
861 \begin{progcode}
862 text.set(mode="latex")\\
863 c = canvas.canvas()\\
864 w = 2\\
865 c.text(0, 0, r"\cs begin\cb{itemize}\cs item a\cs item b\cs end\cb{itemize}", [text.parbox(w)])
866 \end{progcode}
868 Occasionally, one would like to have the box in which the text appears to be as
869 small as possible. Then the \texttt{fancybox} package developed by Timothy Van
870 Zandt is useful which provides several environments like \texttt{Bitemize} and
871 \texttt{Beqnarray} which can be processed in LR mode. The relevant part of the
872 code could look like:
873 \begin{progcode}
874 text.set(mode="latex")\\
875 text.preamble(r"\cs usepackage\cb{fancybox}")\\
876 c = canvas.canvas()\\
877 c.text(0, 0, r"\cs begin\cb{Bitemize}\cs item a\cs item b\cs end\cb{Bitemize}")
878 \end{progcode}
879 Other environments provided by the \texttt{fancybox} package include
880 \texttt{Bcenter}, \texttt{Bflushleft}, \texttt{Bflushright},
881 \texttt{Benumerate}, and \texttt{Bdescription}. For more details, the
882 documentation of the package should be consulted.
885 \question{a}{Font shape `OT1/xyz/m/n' undefined}
887 {\label{q:fontshape_undefined}
888 You have asked to use font \texttt{xyz} which is not available. Make sure that
889 you have this font available in Type1 format, i.e.\ there should be a
890 file \texttt{xyz.pfb} somewhere. If your \TeX{} system is TDS compliant
891 (TDS=\TeX{} directory structure, cf.\ \ctan{tds/draft-standard/tds/tds.pdf})
892 you should take a look at the subdirectories of
893 \path{TEXMF/fonts/type1}.
896 \question{a}{File \dots\ is not available or not readable}
898 {\label{q:no_lfs}
899 Such an error message might already occur when running the example file
900 \texttt{hello.py} included in the \PyX{} distribution. Usually, the error
901 occurs due to an overly restrictive umask setting applied when unpacking the
902 \texttt{tar.gz} sources. This may render the file mentioned in the error
903 message unreadable because the python distutil installation package doesn't
904 change the file permissions back to readable for everyone.
906 If the file exists, the problem can be solved by changing the permissions to
907 allow read access.}
909 \question{a}{No information for font `cmr10' found in font mapping
910 file}
912 {\label{q:no_cmr10}
913 Such an error message can already be encountered by simply running the example
914 file \texttt{hello.py} included in the \PyX{} distribution. The likely reason
915 is that the \TeX{} system does not find the cmr fonts in Type1 format.
916 \PyX{} depends on these fonts as it does not work with the traditional
917 pk fonts which are stored as bitmaps.
919 Therefore, the first thing to make sure is that the cmr Type1 fonts are
920 installed. In some \TeX{} installations, the command \texttt{kpsewhich
921 cmr10.pfb} will return the appropriate path if the cmr fonts exist in the
922 binary Type1 format (extension \texttt{pfb}) required by \PyX. If the command
923 does not work but the TeX{} system is TDS compliant
924 (\uaref{q:fontshape_undefined}), a look should be taken at
925 \path{TEXMF/fonts/type1/bluesky/cm} where \texttt{TEXMF} is the root of the
926 \texttt{texmf} tree.
928 If the Type1 fonts do not exist on the system, they may be obtained from
929 the CTAN \uaref{q:ctan} at \ctan{fonts/cm/ps-type1/bluesky}. See the
930 \texttt{README} for information about who produced these fonts and why they
931 are freely available.
933 If the Type1 fonts exist, the next step is to take a look at
934 \texttt{psfonts.map}. There may be several files with this name on the system,
935 so it is important to find out which one TeX is actually using.
936 \texttt{kpsewhich psfonts.map} might give this information.
938 The most likely problem is that this file does not contain a line telling TeX
939 what to do if it encounters a request for font \texttt{cmr10}, i.e. the
940 following line
941 may be missing
942 \begin{progcode}
943 ~~~cmr10~~~~~~~~~~~CMR10~~~~~~~~~~~<cmr10.pfb
944 \end{progcode}
945 It is probable that the required lines (in practice, you do not just need
946 \texttt{cmr10}) are found in a file named \texttt{psfonts.cmz} which resides in
947 \path{TEXMF/dvips/bluesky}.
949 One solution is to instruct \PyX{} to read additional map files like
950 \texttt{psfonts.cmz} or \texttt{psfonts.amz}. This can be achieved by modifying
951 the appropriate \texttt{pyxrc} file which is either the systemwide
952 \texttt{/etc/pyxrc} or \texttt{.pyxrc} in the user's home directory. Here, the
953 names of the map files to be read by \PyX{} should be appended separated by
954 whitespaces like in the following example:
955 \begin{progcode}
956 [text]\\
957 fontmaps = psfonts.map psfonts.cmz psfonts.amz
958 \end{progcode}
959 The same effect can be achieved by inserting the following line into the
960 \PyX{} code:
961 \begin{progcode}
962 text.set(fontmaps="psfonts.map psfonts.cmz psfonts.amz")
963 \end{progcode}
964 Note that the default map (psfonts.map) has to be specified explicitly.
966 An alternative approach consists in modifying the \TeX{} installation by
967 inserting the contents of the desired map files like \texttt{psfonts.cmz} into
968 \texttt{psfonts.map}. Probably, \texttt{psfonts.map} recommends not to do this
969 by hand. In this case the instructions given in the file should be followed.
970 Otherwise, \texttt{psfonts.cmz} should be copied into \texttt{psfonts.map} while
971 keeping a backup of the old \texttt{psfonts.map} just in case. After these
972 changes, \PyX{} most likely will be happy. When inserting \texttt{psfonts.cmz}
973 into \texttt{psfonts.map} it may be a good idea to include \texttt{psfonts.amz}
974 as well. \texttt{psfonts.amz} contains information about some more fonts which
975 might be needed at some point. Making these changes ot \texttt{psfonts.map}
976 will imply that the \TeX{} system will use the cmr fonts in Type1 format
977 instead of pk format which is actually not a bad thing, in particular if
978 \texttt{latex}~/ \texttt{dvips}~/ \texttt{ps2pdf} is used to generate PDF
979 output. With fonts in pk format this will look ugly and using Type1 fonts
980 solves this problem as well. When \texttt{pdflatex} is used to create PDF files,
981 Type1 fonts will be used anyway.
984 \subsection{Fonts}
986 \question{t}{I have Type1 fonts in \texttt{pfa} format. How do I obtain the
987 corresponding \texttt{pfb} files needed by \PyX?}
992 \question{a}{I want to use a font other than computer modern roman}
994 {\label{q:other_font}
995 As long as you have a font in Type1 format available, this should be no
996 problem (even though it may cost you some time to set up things properly).
998 In the simplest case, your \LaTeX{} system contains everything needed.
999 Including the following line into your code will probably work
1000 \begin{progcode}
1001 text.set(mode="latex")\\
1002 text.preamble(r"\cs{usepackage}\cb{mathptmx}")
1003 \end{progcode}
1004 and give you Times as roman font.
1006 If you own one of the more common commercial fonts, take a look at
1007 \ctan{fonts} and its subdirectories as well as at the web page
1008 \url{http://home.vr-web.de/was/fonts.html} of Walter Schmidt. It is not
1009 unlikely that somebody has already done most of the work for you and created
1010 the files needed for the font to work properly with \LaTeX. But remember:
1011 we are talking about commercial fonts here, so do not expect to find the fonts
1012 themselves for free.
1014 If none of these cases applies, you should spend some time reading
1015 manuals about font installation, e.g.\ \ctan{macros/latex/doc/fntguide.pdf}
1016 (of course, I do not expect font wizards to read the last few lines).
1019 \question{a}{Can I use a TrueType font with \PyX?}
1021 {Not directly as \PyX{} only knows how to handle Type1 fonts (although it is
1022 possible to get \LaTeX{} to work with TrueType fonts). However, you may use
1023 \texttt{ttf2pt1} (from \url{http://ttf2pt1.sourceforge.net}) to convert a
1024 TrueType font into a Type1 font which you then install in your \TeX{} system
1025 \uaref{q:other_font}. You will loose hinting information
1026 in the conversion process but this should not really matter on output devices
1027 with not too low resolution.
1030 \end{document}