1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
==================
Plotting of graphs
==================
General aspects
===============
.. _mingraphdata:
How do I generate a graph from data as simply as possible?
----------------------------------------------------------
Suppose that you have a data file ``x.dat`` containing values for ``x`` and
``y`` in two columns. Then the following code will do the job::
from pyx import *
g = graph.graphxy(width=10)
g.plot(graph.data.file("x.dat", x=1, y=2))
g.writeEPSfile("x")
``graphxy`` creates a canvas (called ``g`` in this example) onto which the
graph will be drawn and it sets the default behavior including the axis. There
is, however, no default value for the width of the graph. In ``plot`` you have
to specify the name of the data file and the columns from which the data should
be taken. Finally, ``writeEPSfile`` will generate the postscript file ``x.eps``
which you can view or print.
A minimal example is also provided in the PyX distribution as
``examples/graphs/minimal.py``.
.. _mingraphfunc:
How do I generate a graph of a function as simply as possible?
--------------------------------------------------------------
The following example will draw a parabola::
from pyx import *
g = graph.graphxy(width=10,
x=graph.axis.linear(min=-2, max=2)
)
g.plot(graph.data.function("y(x)=x**2"))
g.writeEPSfile("x")
Most of the code has been explained in :ref:`mingraphdata`. The main
difference is that here you need to specify minimum and maximum for the
``x``-axis so that PyX knows in which range to evaluate the function.
Another, slightly more complex, example is also provided in the PyX
distribution as ``examples/graphs/piaxis.py``.
How can I stack graphs?
-----------------------
PyX always needs a canvas to draw on. One possibility therefore consists in
creating a new canvas with ::
c = canvas.canvas()
and inserting the graphs into this canvas with ``c.insert(…)``. Here, ``…`` has
to be replaced by the name of the graph. Alternatively, the canvas created with
``graph.graphxy`` for one of the graphs can be used to insert the other graphs
even if they will be positioned outside the first graph.
The second issue to address is positioning of the graphs. By specifying
``xpos`` and ``ypos`` when calling ``graphxy`` you can define the position of a
graph. Later on, the position and size of a graph ``g`` can be referred to as
``g.xpos`` ``g.ypos`` ``g.width`` and ``g.height`` even if for example the
height has never been specified explicitly but is only defined by a PyX
default.
The following example shows how to put graph ``gupper`` above graph ``glower``
on a canvas ``c``::
from pyx import *
from graph import graphxy
c = canvas.canvas()
glower = graphxy(width=10)
glower.plot(...)
c.insert(glower)
gupper = graphxy(width=10, ypos=glower.ypos+glower.height+2)
gupper.plot(...)
c.insert(gupper)
c.writeEPSfile(...)
where ``…`` has to be replaced by the appropriate information like data and
symbol specifications and the name of the output file. Here, ``c.insert`` is
used to actually insert the subcanvasses for the graphs into the main canvas
``c`` and ``c.writeEPSfile`` in the last line requests to write the contents of
this canvas to a file.
How can I plot grid data?
-------------------------
PyX offers support for plotting three-dimensional data as two-dimensional color
plots or grey-scale plots and of vector fields by providing ways to plot
rectangles and arrows in graphs.
We start by considering the task of creating a two-dimensional color plot by
plotting a number of filled rectangles. One first needs to create a data set
which consists of five entries per data point. These are the lower left corner
(*x*\ :sub:`min`, *y*\ :sub:`min`) and the upper right corner (*x*\ :sub:`max`,
*y*\ :sub:`max`) of the triangle and a value between 0 and 1 determining the
color via a PyX color palette. The following code gives an idea of how to
proceed::
g.plot(graph.data.file("datafile.dat", xmin=1, xmax=2, ymin=3, ymax=4, color=5),
[graph.style.rect(color.palette.ReverseRainbow)]
)
g.dodata()
Here, we assume that the data are stored in ``datafile.dat`` and the columns
contain *x*\ :sub:`min`, *x* :sub:`max`, *y*\ :sub:`min`, *y*\ :sub:`max`, and
the color value in this order. The columns are numbered from 1, since the 0th
column contains the line number. To determine the color, we use the
``ReverseRainbow`` palette. The last line instructs PyX to plot the rectangles
before plotting the axes. Otherwise, the axes might be covered partially by the
rectangles and, in particular, the ticks might not be visible. Gray-scale plots
can easily be generated by specifying the palette ``Gray`` or ``ReverseGray``
(cf. appendix C of the manual for a list of predefined palettes).
At first sight, it seems surprising that plotting of grid data requires the
specification of four coordinates for the rectangle. The reason is that this
allows to draw rectangles of varying sizes which may help to reduce the size of
the postscript file by combining rectangles of the same color in horizontal or
vertical direction. For example, it may be sufficient to plot a grey-scale
image in a small number of grey shades and then combining rectangles may be
appropriate. Note, though, that this step is part of the data creation and not
preformed by PyX. Another advantage of fully specifying each rectangle is that
it is straightforward to leave parts of the graph blank.
The same ideas as for the color plot can be applied to plot vector fields where
each data point is represented by an arrow. In this case a data point is
specified by the position of the arrow, its size and its direction as indicated
in the following code snippet::
g.plot(graph.data.file("datafile.dat"), x=1, y=2, size=3, angle=4),
[graph.style.arrow()]
)
Complete code examples can be found in ``examples/graphs/mandel.py`` and
``examples/graphs/arrows.py`` .
.. _problemcoord:
How can I access points in problem coordinates of a graph?
----------------------------------------------------------
Sometimes it may be necessary to add graphical elements to a graph in addition
to the data or function(s) which have been plotted as described in
:ref:`mingraphdata` and :ref:`mingraphfunc`. For a graph instance
``g`` the positioning can easily be done in canvas coordinates by making
use of the origin (``g.xpos``, ``g.ypos``) and the width
(``g.width``) and height (``g.height``) of the graph.
Occasionally, it may be more convenient to specify the position of the
additional material in terms of problem coordinates. However, this requires
that the mapping from problem coordinates to canvas coordinates is known. By
default this is not the case before the content of the canvas is written to the
output which is too late for our purpose. One therefore needs to explicitly
instruct PyX to determine this mapping. One possibility is to ask PyX to finish
the graph by means of ``g.finish()``. Now, problem coordinates can be used to
insert additional material which will end up in front of the graph. If this is
not desired, one should only fix the layout of the graph by means of
``g.dolayout()``. Then, the additional material can be put onto the canvas
before the graph is drawn and it will therefore appear behind the graph.
The conversion of problem coordinates (``px``, ``py``) to canvas coordinates
(``x``, ``y``) is performed as follows::
x, y = g.pos(px, py)
By default, the problem coordinates will refer to the ranges of the *x* and *y*
axes. If several axes with different ranges exist, the instances of the desired
axes should be passed to the ``pos`` method by means of the keyword arguments
``xaxis`` and ``yaxis``.
We remark that the drawing of lines parallel to one of the axes at specific
problem coordinates can also be done by adapting the method described in
:ref:`zeroline`.
I would like a key for only some of my data sets. How do I do that?
-------------------------------------------------------------------
.. todo::
This still needs to be answered.
Axis properties
===============
How do I specify the tick increment?
------------------------------------
In the partition of a linear axis, the increments associated with ticks,
subticks etc. can be specified as argument of ``parter.linear``. In the
following example, ticks will be drawn at even values while subticks will be
drawn at all integers::
from pyx.graph import axis
tg = graph.graphxy(width=10,
x=axis.linear(min=1, max=10,
parter=axis.parter.linear(tickdists=[2,1]))
)
.. _zeroline:
How do I plot the zero line?
----------------------------
PyX releases before 0.6 offered the possibility to stroke a zero line by
specifying ``zeropathattrs`` in the painter constructor. In more recent
releases, one proceeds as follows. First one has to fix the layout information
of the graph by means of the ``finish`` or ``dolayout`` method (see
:ref:`problemcoord` for a more detailed explanation). Then, the
``xgridpath`` or ``ygridpath`` method of a graph will return a grid
path parallel to the *y* or *x* axis, respectively, at the specified *y* value.
As an example, a zero line in *x* direction can be drawn as follows::
g.finish()
g.stroke(g.ygridpath(0))
How can I add grid lines to a graph?
------------------------------------
Specifying ``gridattrs`` for the painter of an axis will generate grid lines
orthogonal to this axis. At least an empty list is needed like in ::
g = graph.graphxy(width=10,
x=graph.axis.linear(painter=graph.axis.painter.regular(gridattrs=[])),
y=graph.axis.linear()
)
where grid lines in vertical direction are drawn in default style.
Occassionally, one might want to draw grid lines corresponding to ticks and
subticks in a different style. This can be achieved by specifiying changeable
attributes using ``changelist``. The following code ::
my_xpainter = graph.axis.painter.regular(gridattrs=
[attr.changelist([style.linestyle.solid, style.linestyle.dashed])]
)
my_ypainter = graph.axis.painter.regular(gridattrs=
[attr.changelist([color.rgb.red, color.rgb.blue])]
)
g = graph.graphxy(width=10,
x=graph.axis.linear(painter=my_xpainter),
y=graph.axis.linear(painter=my_ypainter)
)
will create vertical solid and dashed grid lines for ticks and subticks,
respectively. The horizontal grid lines will be red for ticks and blue for
subticks. The changeable attributes are applied in a cyclic manner. Therefore,
in this example grid lines at subsubticks would be plotted in the same style as
for ticks. If this is not desired, the list of attributes should be extended by
an appropriate third style. The keyword ``None`` will switch off the respective
level of grid lines in case you want to draw them only e.g. for ticks but not
subticks.
Data properties
===============
How do I choose the symbol and its attributes?
----------------------------------------------
Suppose a graph called ``g`` has been initialized, e.g. by using
``graph.graphxy``. Then, data and the style of their representation in the
graph are defined by calling ``g.plot`` like in the following example in which
filled circles are requested::
g.plot(graph.data.file("test.dat"),
[graph.style.symbol(graph.style.symbol.circle, symbolattrs=[deco.filled])]
)
As another example, if the linewidth of the symbol is too thin for your
purposes, you could use something like::
[graph.style.symbol(graph.style.symbol.plus, symbolattrs=[style.linewidth.Thick])]
How do I choose the color of the symbols?
-----------------------------------------
Colors are not properties of the symbol as such and can therefore not be
specified in ``symbolattrs`` directly. The color is rather related to the
plotting of the symbol as defined by ``deco.stroked`` or ``deco.filled``.
With ::
graph.style.symbol(graph.style.symbol.circle,
symbolattrs=[deco.stroked([color.rgb.red]),
deco.filled([color.rgb.green])]
)
you will obtain a circle filled in green with a red borderline.
How do I choose the line style?
-------------------------------
If you do not want to use symbols, you can set the line style as in this
example ::
g.plot(graph.data.file("test.dat"),
[graph.style.line([style.linewidth.Thin])]
)
where the linewidth is set.
If you also want to use symbols, you can combine the symbol and the
line style as in ::
g.plot(graph.data.file("test.dat"),
[graph.style.line(lineattrs=[style.linewidth.Thin,
style.linestyle.dashed]),
graph.style.symbol(graph.style.symbolline.circle,
symbolattrs=[deco.filled])
]
)
to plot the symbols on top of a thin, dashed line. You may alter the
order of the styles to plot the line on top of the symbols.
How can I change the color of symbols or lines according to a palette?
----------------------------------------------------------------------
If several data sets should be plotted in different colors, one can specify in
``symbolattrs`` and/or ``lineattrs`` a palette like ``color.palette.Rainbow``.
Equidistant colors are chosen spanning the palette from one end to the other.
For example, for three data sets the colors are chosen from the palette at 0,
0.5, and 1. For the rainbow palette, this would correspond to red, green, and
blue, respectively.
In the following example, symbols vary in form and change their color according
to the rainbow palette at the same time as the connecting lines::
mystyle = [graph.style.symbol(graph.style.symbol.changecircle,
symbolattrs=[color.palette.Rainbow]),
graph.style.line(lineattrs=[color.palette.Rainbow])]
See question :ref:`changelist` for a more complete example demonstrating how to
use this style definition and for a comment on the necessity of defining
``mystyle`` (you are of course free to choose a different name).
.. _changelist:
How can I specify changing colors (or other attributes) for symbols or lines?
-----------------------------------------------------------------------------
In ``symbolattrs`` and/or ``lineattrs`` so-called changelist can be used. As an
example ::
mystyle = graph.style.symbol(symbolattrs=
[attr.changelist([color.rgb.red, color.rgb.green])])
g.plot(graph.data.file("x.dat", x=1, y=2), [mystyle])
g.plot(graph.data.file("y.dat", x=1, y=2), [mystyle])
g.plot(graph.data.file("z.dat", x=1, y=2), [mystyle])
will switch between red and green symbols each time a new data set is plotted.
Several changelists can be specified. They are cycled independently and need
not be of the same length. It should be noted that the definition of
``mystyle`` in this example ensures that there is only one instance of the
definition of ``symbolattrs``. Putting an explicit definition of
``symbolattrs`` in each call to ``plot`` would not lead to the desired result
because each time a new instance would be created which then starts with the
first item in the changelist.
It may be necessary to repeat attributes in order that several changelists
cooperate to produce the desired result. A common situation is that one would
like to cycle through a list of symbols which should be used in alternating
colors. This can be achieved with the following code::
mystyle = graph.style.symbol(
graph.style.symbol.changetriangletwice,
symbolattrs=[attr.changelist([color.rgb.red, color.rgb.green])])
which will produce a red triangle, a green triangle, a red circle, a green
circle and so on for diamond and square because ``changetriangletwice`` lists
each symbol twice. If instead of changing between colors one would like to
change between filled and open symbols, one can make use of a predefined
changelist ::
mystyle = graph.style.symbol(
graph.style.symbol.changetriangletwice,
symbolattrs=[graph.style.symbol.changefilledstroked])
|