1 \chapter{Module graph: graph plotting
}
3 \section{Introductory notes
}
5 The graph module is considered to be in constant, gradual development.
6 For the moment we concentrate ourself on standard
2d xy-graphs taking
7 all kind of possible specialties into account like any number of axes.
8 Architectural decisions play the most substantial role at the moment
9 and have hopefully already been done that way, that their flexibility
10 will suffice for future usage in quite different graph applications,
11 \emph{e.g.
} circular
2d graphs or even
3d graphs. We will describe
12 those parts of the graph module here, which are in a totally usable
13 state already and are hopefully not to be changed later on. However,
14 future developments certainly will cause incompatibilities, for
15 example they are expected to happen for automatic axis ticking (which
16 will therefore not yet be covered within this manual). At least be
17 warned: Nobody knows the hole list of things that will break. At the
18 moment, keeping backwards compatibility in the graph module is not at
19 all an issue. Although we do not yet claim any backwards compatibility
20 for the future at all, the graph module is certainly one of the
21 biggest construction sites within
\PyX.
23 The task of drawing graphs is splitted in quite some subtasks, which
24 are implemented by classes of its own. We tried to make those
25 components as independend as it is usefull and possible in order to
26 make them reuseable for different graph types. They are also
27 replaceable by the user to get more specialized graph drawing tasks
28 done without needing to implement a hole graph system. A major
29 abstraction layer are the so-called graph coordinates. Their range is
30 generally fixed to $
[0;
1]$. Only the graph does know about the
31 conversion between these coordinates and the position at the canvas.
32 By that, all other components can be reused for different graph
38 A common feature of a graph are axes. An axis is responsible for the
39 conversion of values to graph coordinates. There are predefined axis
43 axis type&description\\
45 \texttt{linaxis
}&linear axis\\
46 \texttt{logaxis
}&logarithmic axis\\
50 Additional axis types are likely to be added in the future.
52 \subsection{Axes properties
}
54 Global properties of an axis are set as named parameters in the axis
55 constructor. Both predefined axis, the
\verb|linaxis| and the
56 \verb|logaxis|, have the same set of named parameters listed in the
60 \begin{tabularx
}{\linewidth}{l>
{\raggedright\arraybackslash}X
}
61 argument name&description\\
63 \texttt{title
}&axis title\\
64 \texttt{min
}&fixes axis minimum; if not set, it is automatically determined, but this might fail, for example for the $x$-range of functions, when it is not specified there\\
65 \texttt{max
}&as above, but for the maximum\\
66 \texttt{reverse
}&boolean; exchange minimum and maximum (might be used without setting minimum and maximum); if min>max and reverse is set, they cancel each other\\
67 \texttt{divisor
}&numerical divisor for the axis partitioning (its default value is
1)\\
68 \texttt{suffix
}&a suffix to indicate the divisor within an automatic axis labeling\\
69 \texttt{datavmin
}&minimal graph coordinate when adjusting the axis minima to the graph data; default is
0.05\\
70 \texttt{datavmax
}&as above, but for the maximum; default is
0.95\\
71 \texttt{tickvmin
}&minimal graph coordinate for placing ticks to the axis; default is
0\\
72 \texttt{tickvmax
}&as above, but for the maximum; default is
1\\
73 \texttt{part
}&axis partitioning (described below)\\
74 \texttt{painter
}&axis painter (described below)\\
78 \subsection{Partitioning of axes
}
80 The definition of ticks and labels appropriate to an axis range is
81 called partitioning. The axis partioning within
\PyX{} uses rational
82 arithmetics, which avoids any kind of rounding problems to the cost of
83 performance. The class
\verb|frac| supplies a rational number.
84 However, a partitioning is composed out of a sorted list of ticks,
85 where the class
\verb|tick| is derived from
\verb|frac| and has
86 additional properties called
\verb|ticklevel|,
\verb|labellevel|. If
87 those values are
\verb|None| it just means not present,
\verb|
0| means
88 tick or label, respectively,
\verb|
1| means subtick or sublevel and so
89 on. When
\verb|labellevel| is not
\verb|None|, a
\verb|text| might be
90 explicitly given, which will get used as the text of that label.
92 Although there is a rudimentary automatic axis partitioning, the
93 recommended solution at the moment is a manual axis partitioning,
94 because the manual axis partitioning will hopefully not break in
95 future versions, while the automatic axis breaking will change for
96 sure at least in the results it creates.
98 There are three different manual partition schemes, a manual
99 partition, another appropriate for linear axes and a third one for
102 \subsubsection{Manual partitioning
}
104 The class
\verb|manualpart| creates a manual partition as described by
105 named parameters of the constructor:
108 \begin{tabularx
}{\linewidth}{ll>
{\raggedright\arraybackslash}X
}
109 argument name&default&description\\
111 \texttt{ticks
}&
\texttt{None
}&position of ticks, subticks, etc. (see below)\\
112 \texttt{labels
}&
\texttt{None
}&position of labels, sublabels, etc. (see below)\\
113 \texttt{texts
}&
\texttt{None
}&force text at labels, sublabels, etc. (see below)\\
114 \texttt{mix
}&
\texttt{()
}&ordered tick list to be merged into the result\\
118 The parameters
\verb|ticks|,
\verb|labels|, and
\verb|texts| can
119 either be a sequence, or a sequence of sequences. (When it is not a
120 sequence at all, it is converted to a sequence with a single entry.)
121 When it is a sequence of sequences, than the first sequence stands for
122 the ticks, labels, and texts of the labels, the second sequence stands
123 for the subticks, sublabels, and texts of the sublabels, and so on.
124 When it is just a sequence, it stands for the ticks, labels and texts
127 The single entries of
\verb|ticks| and
\verb|labels| can either be a
128 frac or a string, which will be converted to a frac. However, a float
129 is not valid in order to avoid a conversion from a float to a frac.
130 Valid strings are just numbers like
\verb|"
0.1"|, or fractions like
133 \subsubsection{Partitioning of linear axes
}
135 The class
\verb|linpart| creates a linear partition as described by
136 named parameters of the constructor:
139 \begin{tabularx
}{\linewidth}{ll>
{\raggedright\arraybackslash}X
}
140 argument name&default&description\\
142 \texttt{ticks
}&
\texttt{None
}&distance between ticks, subticks, etc. (see comment below); when the parameter is
\texttt{None
}, ticks will get placed at labels\\
143 \texttt{labels
}&
\texttt{None
}&distance between labels, sublabels, etc. (see comment below); when the parameter is
\texttt{None
}, labels will get placed at ticks\\
144 \texttt{extendtick
}&
\texttt{0}&allow for a range extention to include the next tick of the given level\\
145 \texttt{extendlabel
}&
\texttt{None
}&as above, but for labels\\
146 \texttt{epsilon
}&
\texttt{1e-10}&allow for exceeding the range by that relative value\\
147 \texttt{texts
}&
\texttt{None
}&as in manualpart\\
148 \texttt{mix
}&
\texttt{()
}&as in manualpart\\
152 The
\verb|ticks| and
\verb|labels| can either be a sequence or just a
153 single entry. When a sequence is provided, the first entry stands for
154 the tick or label, respectively, the second for the subtick or
155 sublabel, and so on. The entries can either be a frac or a string,
156 as in
\verb|manualpart|.
158 \subsubsection{Partitioning of logarithmic axes
}
160 The class
\verb|logpart| create a logarithmic partition. The class has
161 the same arguments as
\verb|linpart| upto the interpretation of two
162 arguments
\verb|ticks| and
\verb|labels|. Both parameters can contain
163 just a single entry or a sequence --- the interpretation of those
164 possibilities is the same as it was for
\verb|linpart|. The entries
165 have to be
\verb|shiftfracs|, which contains a
\verb|frac| for the
166 shift, say $s$, and a list of
\verb|frac| for the positions, say
167 $p_i$. Valid positions are then $s^np_i$, where $n$ can be any integer
168 number. Within
\verb|logpart| there are numerous predefined
169 \verb|shiftfracs|, namely:
173 name&values it descibes\\
175 \texttt{shift5fracs1
}&
1 and multiple of $
10^
5$\\
176 \texttt{shift4fracs1
}&
1 and multiple of $
10^
4$\\
177 \texttt{shift3fracs1
}&
1 and multiple of $
10^
3$\\
178 \texttt{shift2fracs1
}&
1 and multiple of $
10^
2$\\
179 \texttt{shiftfracs1
}&
1 and multiple of $
10$\\
180 \texttt{shiftfracs125
}&
1,
2,
5 and multiple of $
10$\\
181 \texttt{shiftfracs1to9
}&
1,
2,
\dots,
9 and multiple of $
10$\\
185 \subsubsection{Automatic partitioning
}
187 When no explicit axis partitioning is given, an automatic axis
188 partitioning is already available, but it is still considered to be
189 under development. A major feature is missing, namely the rating of
190 possible partitions does not yet attend the label texts, which is
191 important in order to avoid overlap of label texts. In order to
192 provide it, the rating of axis partitions has to be moved into the
193 axis painter. That has just to be done and is considered for the next
194 major release of
\PyX.
196 \subsection{Painting of axes
}
198 A major task of an axis is the painting of itself. It is done by
199 instances of
\verb|axispainter|, provided to the constructor of an
200 axis as its painter. The constructor of the axis painter receives a
201 numerous list of named parameters to modify the axis look. A list of
202 parameters is provided in the following table:
205 \begin{tabularx
}{\linewidth}{l>
{\raggedright\arraybackslash}X
}
206 argument name&description\\
208 \texttt{innerticklengths
}$^
{1,
4}$&tick length of inner ticks (visual length);
\newline default:
\texttt{axispainter.defaultticklengths
}\\
209 \texttt{outerticklengths
}$^
{1,
4}$&as before, but for outer ticks; default:
\texttt{None
}\\
210 \texttt{tickattrs
}$^
{2,
4}$&stroke attributes for ticks; default:
\texttt{()
}\\
211 \texttt{gridattrs
}$^
{2,
4}$&stroke attributes for grid lines; default:
\texttt{None
}\\
212 \texttt{zerolineattrs
}$^
{3,
4}$&stroke attributes for a grid line at axis value
0; default:
\texttt{()
}\\
213 \texttt{baselineattrs
}$^
{3,
4}$&stroke attributes for the axis baseline;
\newline default:
\texttt{canvas.linecap.square
}\\
214 \texttt{labeldist
}&label distance from axis (visual length); default:
\texttt{"
0.3 cm"
}\\
215 \texttt{labelattrs
}$^
{2,
4}$&text attributes for labels;
\newline default:
\texttt{((), tex.fontsize.footnotesize)
}\\
216 \texttt{labeldirection
}$^
4$&relative label direction (see below); default:
\texttt{None
}\\
217 \texttt{labelhequalize
}&set width of labels to its maximum (boolean); default:
\texttt{0}\\
218 \texttt{labelvequalize
}&set height and depth of labels to their maxima (boolean); default:
\texttt{1}\\
219 \texttt{titledist
}&title distance from labels (visual length); default:
\texttt{"
0.3 cm"
}\\
220 \texttt{titleattrs
}$^
{3,
4}$&text attributes for title; default:
\texttt{()
}\\
221 \texttt{titledirection
}$^
4$&relative title direction (see below);
\newline default:
\texttt{axispainter.paralleltext
}\\
222 \texttt{titlepos
}&title position in graph coordinates; default:
\texttt{0.5}\\
223 \texttt{fractype
}&text creation for labels (see below);
\newline default:
\texttt{axispainter.fractypeauto
}\\
224 \texttt{ratfracsuffixenum
}&write suffix at the enumerator (boolean); default:
\texttt{1}\\
225 \texttt{ratfracover
}&text for fraction line; default:
\texttt{r"
\textbackslash over"
}\\
226 \texttt{decfracpoint
}&decimal point; default:
\texttt{"."
}\\
227 \texttt{expfractimes
}&text between factor and decimal power; default:
\texttt{r"
\textbackslash cdot"
}\\
228 \texttt{expfracpre1
}&allow factor
1 before a decimal power (boolean); default:
\texttt{0}\\
229 \texttt{expfracminexp
}&minimal exponent for decimal power; default:
\texttt{4}\\
230 \texttt{suffix0
}&when a suffix is
\texttt{x
} write
\texttt{0x
} instead of
\texttt{0} (boolean); default:
\texttt{0}\\
231 \texttt{suffix1
}&when a suffix is
\texttt{x
} write
\texttt{1x
} instead of
\texttt{x
} (boolean); default:
\texttt{0}\\
236 The parameter should be a sequence, where the entries are attributes
237 for the different levels. When the level is larger then the sequence
238 length,
\verb|None| is assumed. When the parameter is not a sequence,
239 it is applied to all levels.\\
241 The parameter should be a sequence of sequences, where the entries are
242 attributes for the different levels. When the level is larger then the
243 sequence length,
\verb|None| is assumed. When the parameter is not a
244 sequence of sequences, it is applied to all levels.\\
246 The parameter should be a sequence. When the parameter is not a
247 sequence, the parameter is interpreted as a sequence with a single
250 The feature can be turned off by the value
\verb|None|. Within
251 sequences or sequences of sequences, the value
\verb|None| might be
252 used to turn off the feature for some levels selectively.
255 Relative directions for labels (
\verb|labeldirection|) and titles
256 (
\verb|titledirection|) are basically a float number in degree. The
257 text direction is calculated relatively to the baseline of the axis
258 and is added as an attribute of the text, when no direction was
259 already provided. The relative direction prevents upside down text by
260 flipping it by
180 degrees. For convenience, the two self-explanatory
261 values
\verb|axispainter.paralleltext| and
262 \verb|axispainter.orthogonaltext| are available.
264 The
\verb|fractype| parameter determines the creation of label texts.
265 There are three types available, which can be forced by providing them
266 to the
\verb|fractype| parameter. The possibilities are listed in the
271 \texttt{fractype
}&description&example\\
273 \texttt{axispainter.fractypedec
}&decimal&$
0.1$\\
274 \texttt{axispainter.fractypeexp
}&decimal with exponent&$
2\cdot 10^
4$\\
275 \texttt{axispainter.fractyperat
}&rational&$
\displaystyle{{1}\over{2}}$\\
276 \texttt{axispainter.fractypeauto
}&automatic (see below)&\\
280 For the default
\verb|axispainter.fractypeauto| the three
281 possibilities are selected depending on some simple rules:
282 \verb|axispainter.fractyperat| is used, when the axis provides a
283 suffix,
\verb|axispainter.fractypeexp| is used, when the exponent
284 exceed
\verb|expfracminexp|, and
\verb|axispainter.fractypedec| is
287 \subsection{Linked axes
}
289 Linked axes can be used whenever an axis should be repeated within a
290 single graph or even between different graphs although the intrinsic
291 meaning is to have only one axis plotted several times. The
292 constructor of
\verb|linkaxis| receives the axis it is linked to as
293 its first parameter. Additionally, the named parameter
\verb|title|
294 contains an axis title (default is
\verb|None|) and the named
295 parameter
\verb|painter| refers to an axispainter (default is
296 \verb|linkaxispainter|). This
\verb|linkedaxispainter| is a slightly
297 modified version of the standard
\verb|axispainter|. Hence it can
298 receive all the parameters as the
\verb|axispainter| and only the
299 default value of the parameter
\verb|zerolineattrs| is changed to
300 \verb|None| compared to the
\verb|axispainter| previously discussed.
301 Additionally, two parameters are added, namely
\verb|skipticklevel|
302 and
\verb|skiplabellevel|. They are used to build the tick list to be
303 plotted at the linked axis. Ticks and labels at levels equal or higher
304 as the provided values get ignored. The default is
\verb|None| (do not
305 ignore any ticks) for the ticks and
\verb|
0| (ignore all labels) for
311 \subsection{List of points
}
313 Instances of the class
\verb|data| link a
\verb|datafile| and a
314 \verb|style| (see below; default is
\verb|mark|). The link object is
315 needed in order to be able to plot several data from a singe file
316 without reading the file several times which would just be a bad
317 design. However, for easy usage, it is possible to provide a filename
318 instead of a datafile as the first argument to the constructor of the
319 class
\verb|data| hiding the underlying
\verb|datafile| instance
320 completely from view. This is the preverable solution as long as the
321 datafile gets used only once.
323 The additional parameters of the constructor of the class
\verb|data|
324 are named parameters. The values of those parameters describe data
325 columns which are linked to the names of the parameters within the
326 style. The data columns can be identified directly via their number or
327 title, or by means of mathematical expressions, as the following table
328 will show by some examples.
332 selection method&example\\
334 as in
\texttt{datafile.getcolumnno
}&
\texttt{data("test.dat", x=
1,
}\\
335 &
\texttt{\hphantom{data(
}y="result", dy="delta")
}\\
336 by mathematical expressions&
\texttt{data("test.dat", x="
0.5*\$
1",
}\\
337 &
\texttt{\hphantom{data(
}y="
0.5*result", dy="
0.5*a", a=
3)
}\\
341 Note that mathematical expressions get evaluated by
342 \verb|datafile.addcolumn| and thus the same column identifications
345 \subsection{Functions
}
347 The class
\verb|function| provides data generation out of a functional
348 expression. The default style for function plotting is
\verb|line|.
349 The constructor of
\verb|function| takes an expression as the first
350 parameter. The expression must be a string with exactly one equal sign
351 (
\verb|=|). At the left side the result axis identifier must be placed
352 and at the right side the expression must depend on exactly one
353 variable axis identifier. Hence, a valid expression looks like
354 \verb|"y=sin(x)"|. You may use the string format syntax to insert
355 external parameters,
\textit{e.g.
} \verb|"y=sin(
%f*x)" % a| where
356 \verb|a| is a float variable.
358 Additional named parameters of the constructor are:
361 \begin{tabularx
}{\linewidth}{ll>
{\raggedright\arraybackslash}X
}
362 argument name&default&description\\
364 \texttt{min
}&
\texttt{None
}&minimal value for the variable parameter; when
\texttt{None
}, the axis data range will be used\\
365 \texttt{max
}&
\texttt{None
}&as above, but for the maximum\\
366 \texttt{points
}&
\texttt{100}&number of points to be calculated\\
367 \texttt{parser
}&
\texttt{mathtree.parser()
}&parser for the mathematical expression\\
371 The expression evaluation takes place at a linear raster of the
372 variable axis. More advanced methods (detection of rapidely changing
373 functions, handling of divergencies) are likely to be added in future
376 \subsection{Parametric functions
}
378 The class
\verb|paramfunction| provides data generation out of a
379 parametric representation of a function. The default style for
380 parametric function plotting is
\verb|line|. The parameter list of the
381 constructor of
\verb|paramfunction| starts with three parameters
382 describing the function parameter. The first parameter is a string,
383 namely the variable name. It is followed by a minimal and maximal
384 value to be used for that parameter. The next parameter contains an
385 expression assigning functions to the axis identifiers in a quite
386 pythonic tuple notation. As an example, such an expression could look
387 like
\verb|"x, y = sin(k), cos(
3*k)"|.
389 Additionally, the two named parameters
\verb|points| and
\verb|parser|
390 behave like their equally named counterparts in
\verb|function|.
395 Styles are used to draw data at a graph. A style determines what is
396 painted and how it is painted. Due to this powerfull approach there
397 are already some different marker types available and the possibility
398 to introduce other styles opens even more prospects.
400 On the other hand there is not yet any support for bar graphs. This is
401 due to the fact that it might be better implemented together with some
402 specialized axes. It will be shown in the future, what solution will
403 arise out of that idea instead of an disposable implementation right
408 The class
\verb|mark| can be used to plot markers, errorbars and lines
409 configurable by parameters of the constructor. Providing
\verb|None|
410 to attributes hides the according component.
413 \begin{tabularx
}{\linewidth}{ll>
{\raggedright\arraybackslash}X
}
414 argument name&default&description\\
416 \texttt{mark
}&
\texttt{changemark.cross()
}&marker to be used (see below)\\
417 \texttt{size
}&
\texttt{"
0.2 cm"
}&size of the marker (visual length)\\
418 \texttt{markattrs
}&
\texttt{canvas.stroked()
}&draw attributes for the marker\\
419 \texttt{errorscale
}&
\texttt{0.5}&size of the errorbar caps (relative to the marker size)\\
420 \texttt{errorbarattrs
}&
\texttt{()
}&stroke attributes for the errorbars\\
421 \texttt{lineattrs
}&
\texttt{None
}&stroke attributes for the line\\
425 The parameter
\verb|mark| has to be a routine, which returns a path to
426 be drawn (e.g. stoked or filled). There are several those routines
427 already available in the class
\verb|mark|, namely
\verb|cross|,
428 \verb|plus|,
\verb|square|,
\verb|triangle|,
\verb|circle|, and
429 \verb|diamond|. Furthermore, changeable attributes might be used here
430 (like the default value
\verb|changemark.cross|), see
431 section~
\ref{graph:changeattrs
} for details.
433 The attributes are available as class variables after plotting the
434 style for outside usage. Additionally, the variable
\verb|path|
435 contains the path of the line (even when it wasn't plotted), which
436 might be used to get crossing points, fill areas, etc.
438 Valid data names to be used when providing data to markers are listed
439 in the following table. The character
\verb|X| stands for axis names
440 like
\verb|x|,
\verb|x2|,
\verb|y|, etc.
444 data name&description\\
446 \texttt{X
}&position of the marker\\
447 \texttt{Xmin
}&minimum for the errorbar\\
448 \texttt{Xmax
}&maximum for the errorbar\\
449 \texttt{dX
}&relative size of the errorbar:
\texttt{Xmin, Xmax = X-dX, X+Xd
}\\
450 \texttt{dXmin
}&relative minimum
\texttt{Xmin = X-dXmin
}\\
451 \texttt{dXmax
}&relative maximum
\texttt{Xmax = X+dXmax
}\\
457 The class
\verb|line| is inherited from
\verb|mark| and is restricted
458 to line drawing. The constructor takes only
\verb|lineattrs| and its
459 default is set to
\verb|changelinestyle()|. The other features of the
460 mark style are turned off.
462 \subsection{Rectangles
}
464 The class
\verb|rect| draws filled rectangles into a graph. The size
465 and the position of the rectangles to be plotted can be provided by
466 the same data names like for the errorbars of the class
\verb|mark|.
467 Indeed, the class
\verb|mark| reuses most of the marker code by
468 inheritance, while modifying the errorbar look into a colored filled
469 rectangle and turing off the marker itself.
471 The
color to be used for the filling of the rectangles is taken from a
472 gradient provided to the constructor by the named parameter
473 \verb|gradient| (default is
\verb|
color.gradient.Gray|). The data
474 name
\verb|
color| is used to select the
color out of this gradient.
478 Another style to be used within graphs is the class
\verb|text|, which
479 adds the output of text to the class
\verb|mark|. The text
480 position relative to the markers is defined by the two named
481 parameters
\verb|textdx| and
\verb|textdy| having a default of
482 \verb|"
0 cm"| and
\verb|"
0.3 cm"|, respectively, which are by default
483 interpreted as visual length. A further named parameter
484 \verb|textattrs| may contain a sequence of text attributes (or just a
485 single attribute). The default for this parameter is
486 \verb|tex.halign.center|. Furthermore the constructor of this class
487 allows all other attributes of the class
\verb|mark|.
491 The class
\verb|arrow| can be used to plot small arrows into a graph
492 where the size and direction of the arrows has to be given within the
493 data. The constructor of the class takes the following parameters:
496 \begin{tabularx
}{\linewidth}{ll>
{\raggedright\arraybackslash}X
}
497 argument name&default&description\\
499 \texttt{linelength
}&
\texttt{"
0.2 cm"
}&length of a the arrow line (visual length)\\
500 \texttt{arrowattrs
}&
\texttt{()
}&stroke attributes\\
501 \texttt{arrowsize
}&
\texttt{"
0.1 cm"
}&size of the arrow (visual length)\\
502 \texttt{arrowdict
}&
\texttt{\
{\
}}&attributes to be used in the
\texttt{earrow
} constructor\\
503 \texttt{epsilon
}&
1e-10&smallest allowed arrow size factor for a arrow to become plotted (avoid numerical instabilities)\\
507 The arrow allows for data names like the mark and introduces
508 additionally the data names
\verb|size| for the arrow size (as an
509 multiplicator for the sizes provided to the constructor) and
510 \verb|angle| for the arrow direction (in degree).
512 \subsection{Iterateable style attributes
}
513 \label{graph:changeattrs
}
515 The attributes provided to the constructors of styles can usually
516 handle so called iterateable attributes, which are changing itself
517 when plotting several data sets. Iterateable attributes can be easily
518 written, but there are already some iterateable attributes available
519 for the most common cases. For example a
color change is done by
520 instances of the class
\verb|colorchange|, where the constructor takes
521 a gradient. Applying this attribute to a style and using this style at
522 a sequence of data, the
color will get changed lineary along the
523 gradient from one end to the other. The class
\verb|colorchange|
524 includes inherited classes as class variables, which are called like
525 the
color gradients shown in appendix~
\ref{gradientname
}. For them the
526 default gradient is set to the appropriate
color gradient.
528 Another attribute changer is called
\verb|changesequence|. The
529 constructor takes a list of attributes and the attribute changer
530 cycles through this list whenever a new attribute is requested.
531 This attribute changer is used to implement the following attribute
536 attribute changer&description\\
538 \texttt{changelinestyle
}&iterates linestyles solid, dashed, dotted, dasheddotted\\
539 \texttt{changestrokedfilled
}&iterates
\texttt{(canvas.stroked(), canvas.filled())
}\\
540 \texttt{changefilledstroked
}&iterates
\texttt{(canvas.filled(), canvas.stroked())
}\\
544 The class
\verb|changemark| can be used to cycle throu markers and it
545 provides already various specialized classes as class variables. To
546 loop over all available markers (cross, plus, square, triangle,
547 circle, and diamond) the equal named class variables can be used. They
548 start at that marker they are named of. Thus
\verb|changemark.cross()|
549 cycles throu the sequence starting at the cross marker. Furthermore
550 there are four class variables called
\verb|squaretwice|,
551 \verb|triangletwice|,
\verb|circletwice|, and
\verb|diamondtwice|.
552 They cycle throu the four fillable markers, but returning the markers
553 twice before they go on to the next one. They are intented to be used
554 in combination with
\verb|changestrokedfilled| and
555 \verb|changefilledstroked|.
558 Sorry, there is not yet any support for graph keys.
562 The class
\verb|graphxy| draws standard x-y-graphs. It is a subcanvas
563 and can thus be just inserted into a canvas. The x-axes are named
564 \verb|x|,
\verb|x2|,
\verb|x3|,
\dots and equally the y-axes. The
565 number of axes is not limited. All odd numbered axes are plotted at
566 the bottom (for x axes) and at the left (for y axes) and all even
567 numbered axes are plotted opposite to them. The lower numbers are
570 The constructor of
\verb|graphxy| takes axes as named parameters where
571 the parameter name is an axis name as just described. Those parameters
572 refer to an axis instance as they where described in
573 section~
\ref{graph:axes
}. When no
\verb|x| or
\verb|y| is provided,
574 they are automatically set to instances of
\verb|linaxis|. When no
575 \verb|x2| or
\verb|y2| axes are given they are initialized as standard
576 linkaxis to the axis
\verb|x| and
\verb|y|. However, you can turn off
577 the automatism by setting those axes explicitly to
\verb|None|.
579 However, the constructor takes some more attributes, namely first of
580 all a tex canvas. (This ugly construction is likely to be ommited in
581 future versions of
\PyX{} once a new
\TeX{} binding becomes
582 available.) Other parameters are named and listed in the following
586 \begin{tabularx
}{\linewidth}{ll>
{\raggedright\arraybackslash}X
}
587 argument name&default&description\\
589 \texttt{xpos
}&
\texttt{"
0"
}&x position of the graph (user length)\\
590 \texttt{ypos
}&
\texttt{"
0"
}&y position of the graph (user length)\\
591 \texttt{width
}&
\texttt{None
}&width of the graph area (axes are outside of that range)\\
592 \texttt{height
}&
\texttt{None
}&as abovem, but for the height\\
593 \texttt{ratio
}&
\texttt{goldenrule
}&width/height ratio when only a width or height is provided\\
594 \texttt{backgroundattrs
}&
\texttt{None
}&background attributes for the graph area\\
595 \texttt{axisdist
}&
\texttt{"
0.8 cm"
}&distance between axis (visual length)\\
599 After a graph is constructed, data can be plotted via the
\verb|plot|
600 method. The first argument should be an instance of the data providing
601 classes described in section~
\ref{graph:data
}. This first parameter
602 can also be a list of those instances when you want to iterate the
603 style you explicitly provide as a second parameter to the plot method.
604 The plot method returns the style (or a list of styles when a data
605 list was provided) which was used for plotting. Just as an example you
606 can thus access the path of a line and fill areas with it and so on.
608 After the plot method was called once or several times, you should
609 call the method
\verb|finish|. (This is actually needed as long as a
610 tex canvas gets used for text output and the tex canvas is inserted
611 into the main canvas before the graph gets inserted.) Finishing a
612 graph allows for the access to positioning routines which can be quite
613 usefull to plot additional information into a graph.
615 Sometimes it is also nice to partly finish a graph. By that you can
616 even modify the order in which a graph performs its drawing process.
617 By default the four methods
\verb|dolayout|,
\verb|dobackground|,
618 \verb|doaxis|, and
\verb|dodata| are called in that order. The method
619 \verb|dolayout| must always be called first, but this is internally
620 ensured once you call any of the routines yourself. After
621 \verb|dolayout| gets called, the method
\verb|plot| can not be used
624 To get a position within a graph as a tuple out of some axes values,
625 the method
\verb|pos| can be used. It takes two values for a position
626 at the x and y axis. By default, the axes named
\verb|x| or
\verb|y|
627 are used, but this is changed when the named parameters
\verb|xaxis|
628 and
\verb|yaxis| are set to other axes. The graph axes are available
629 by their name using the dictionary
\verb|axes|. Each axis has a method
630 \verb|gridpath| which is set by the graph. It returns a gridpath for a
631 given position at the axis.
635 \subsection{A minimal example: plot data from a file
}
637 We plot data from the file
\verb|"graph.dat"|:
650 The following script creates the file
\verb|"graph.eps"|:
657 t = c.insert(tex.tex())
658 g = c.insert(graph.graphxy(t, width=
10))
659 g.plot(graph.data("graph.dat", x=
1, y=
2))
661 c.writetofile("graph")
665 The result looks like:
667 \includegraphics{graph1
}
670 \subsection{A more advanced function plot
}
675 from pyx.graph import *
682 mypainter=axispainter(baselineattrs=canvas.earrow.normal)
684 g = c.insert(graphxy(t, width=
10, x2=None, y2=None,
685 x=linaxis(min=
0, max=
10,
686 part=manualpart(ticks=(frac(a,
1),
690 y=linaxis(painter=mypainter,
693 line = g.plot(function("y=(x-
3)*(x-
5)*(x-
7)"))
696 pa = path.path(g.axes
["x"
].gridpath(a))
697 pb = path.path(g.axes
["x"
].gridpath(b))
698 (splita,), (splitpa,) = line.path.intersect(pa)
699 (splitb,), (splitpb,) = line.path.intersect(pb)
700 area = (pa.split(splitpa)
[0] <<
701 line.path.split(splita, splitb)
[1] <<
702 pb.split(splitpb)
[0].reversed())
703 area.append(path.closepath())
704 g.stroke(area, canvas.linewidth.THick,
705 canvas.filled(
color.gray(
0.8)))
706 t.text(g.pos(
0.5*(a+b),
0)
[0],
1,
707 r"
\int_a^b f(x)
{\rm d
}x", tex.halign.center, tex.style.math)
710 c.writetofile("graph2")
714 The result looks like:
716 \includegraphics{graph2
}