| 1 |
% Copyright 2018 by Till Tantau |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Free Documentation License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
|
| 11 |
\section{Creating Plots} |
| 12 |
\label{section-plots} |
| 13 |
|
| 14 |
This section describes the |plot| module. |
| 15 |
|
| 16 |
\begin{pgfmodule}{plot} |
| 17 |
This module provides a set of commands that are intended to make it |
| 18 |
reasonably easy to plot functions using \pgfname. It is loaded |
| 19 |
automatically by |pgf|, but you can load it manually if you have only |
| 20 |
included |pgfcore|. |
| 21 |
\end{pgfmodule} |
| 22 |
|
| 23 |
|
| 24 |
\subsection{Overview} |
| 25 |
|
| 26 |
There are different reasons for using \pgfname\ for creating plots rather than |
| 27 |
some more powerful program such as \textsc{gnuplot} or \textsc{mathematica}, as |
| 28 |
discussed in Section~\ref{section-why-pgname-for-plots}. So, let us assume that |
| 29 |
-- for whatever reason -- you wish to use \pgfname\ for generating a plot. |
| 30 |
|
| 31 |
\pgfname\ (conceptually) uses a two-stage process for generating plots. First, |
| 32 |
a \emph{plot stream} must be produced. This stream consists (more or less) of a |
| 33 |
large number of coordinates. Second a \emph{plot handler} is applied to the |
| 34 |
stream. A plot handler ``does something'' with the stream. The standard handler |
| 35 |
will issue line-to operations to the coordinates in the stream. However, a |
| 36 |
handler might also try to issue appropriate curve-to operations in order to |
| 37 |
smooth the curve. A handler may even do something else entirely, like writing |
| 38 |
each coordinate to another stream, thereby duplicating the original stream. |
| 39 |
|
| 40 |
Both for the creation of streams and the handling of streams different sets of |
| 41 |
commands exist. The commands for creating streams start with |\pgfplotstream|, |
| 42 |
the commands for setting the handler start with |\pgfplothandler|. |
| 43 |
|
| 44 |
|
| 45 |
\subsection{Generating Plot Streams} |
| 46 |
|
| 47 |
\subsubsection{Basic Building Blocks of Plot Streams} |
| 48 |
|
| 49 |
A \emph{plot stream} is a (long) sequence of the following commands: |
| 50 |
% |
| 51 |
\begin{enumerate} |
| 52 |
\item |\pgfplotstreamstart|, |
| 53 |
\item |\pgfplotstreampoint|, |
| 54 |
\item |\pgfplotstreampointoutlier|, |
| 55 |
\item |\pgfplotstreampointundefined|, |
| 56 |
\item |\pgfplotstreamnewdataset|, |
| 57 |
\item |\pgfplotstreamspecial|, and |
| 58 |
\item |\pgfplotstreamend|. |
| 59 |
\end{enumerate} |
| 60 |
% |
| 61 |
Between calls of these commands arbitrary other code may be called. Obviously, |
| 62 |
the stream should start with the first command and end with the last command. |
| 63 |
Here is an example of a plot stream: |
| 64 |
% |
| 65 |
\begin{codeexample}[code only] |
| 66 |
\pgfplotstreamstart |
| 67 |
\pgfplotstreampoint{\pgfpoint{1cm}{1cm}} |
| 68 |
\newdimen\mydim |
| 69 |
\mydim=2cm |
| 70 |
\pgfplotstreampoint{\pgfpoint{\mydim}{2cm}} |
| 71 |
\advance \mydim by 3cm |
| 72 |
\pgfplotstreampoint{\pgfpoint{\mydim}{2cm}} |
| 73 |
\pgfplotstreamend |
| 74 |
\end{codeexample} |
| 75 |
|
| 76 |
Streams are \emph{global}, meaning that they are not influenced by \TeX\ |
| 77 |
groups. |
| 78 |
|
| 79 |
\begin{command}{\pgfplotstreamstart} |
| 80 |
This command signals that a plot stream starts. The effect of this command |
| 81 |
is to call the internal command |\pgf@plotstreamstart|, which is set by the |
| 82 |
current plot handler to do whatever needs to be done at the beginning of |
| 83 |
the plot. It will also reset the meaning of the internal commands like |
| 84 |
|\pgf@plotstreampoint| to the initial setting for the plot handler (what |
| 85 |
this means will be explained in a moment). |
| 86 |
\end{command} |
| 87 |
|
| 88 |
\begin{command}{\pgfplotstreampoint\marg{point}} |
| 89 |
This command adds a \meta{point} to the current plot stream. The effect of |
| 90 |
this command is to call the internal command |\pgf@plotstreampoint|, which |
| 91 |
is also set by the current plot handler. This command should now ``handle'' |
| 92 |
the point in some sensible way. For example, a line-to command might be |
| 93 |
issued for the point. |
| 94 |
|
| 95 |
When a plot handler is installed, it will setup the internal command |
| 96 |
|\pgf@plotstreampoint| in some way. It is permissible to change the meaning |
| 97 |
of this internal command during a stream. For instance, a handler might |
| 98 |
setup |\pgf@plotstreampoint| in some sensible way for the first point and |
| 99 |
then redefine it so that subsequent points are handled in some other way. |
| 100 |
|
| 101 |
As mentioned earlier, the |\pgfplotstreamstart| will always reset the |
| 102 |
definition of the internal command to the initial meaning it had when the |
| 103 |
handler was installed. This is true for the other commands mentioned in the |
| 104 |
following. |
| 105 |
\end{command} |
| 106 |
|
| 107 |
\begin{command}{\pgfplotstreampointoutlier\marg{point}} |
| 108 |
An \emph{outlier} is a point that is ``out of bounds'' in some way. For |
| 109 |
instance, it might have very large coordinates or the coordinates might |
| 110 |
just be outside some specified range. Nevertheless, an outlier is still a |
| 111 |
well-defined point. This command is issued, for instance, by |
| 112 |
\textsc{gnuplot} when a value is outside the specified range. |
| 113 |
|
| 114 |
You can configure how outliers are treated using the following key: |
| 115 |
% |
| 116 |
\begin{key}{/pgf/handle outlier points in plots=\meta{how} (initially jump)} |
| 117 |
\keyalias{tikz} |
| 118 |
You can set \meta{how} to one of the following values: |
| 119 |
% |
| 120 |
\begin{itemize} |
| 121 |
\item |plot| This will cause the outlier to be drawn normally, just |
| 122 |
as if |\pgfplotstreampoint| had been used rather than this |
| 123 |
command. |
| 124 |
\item |ignore| The outlier will be completely ignored, just as if |
| 125 |
the command had not been used at all. |
| 126 |
\item |jump| This causes the internal macro |\pgf@plotstreamjump| |
| 127 |
to be called. A ``jump'' in a stream is a position where a |
| 128 |
``gap'' is introduced. For instance, a simple line-to plot |
| 129 |
handler will stop the current subpath at a jump position and |
| 130 |
begin with a move-to operation at the next normal point of the |
| 131 |
stream. |
| 132 |
|
| 133 |
The net effect of this setting is that at outlier points plots |
| 134 |
get interrupted and ``restarted'' when the points are no longer |
| 135 |
outliers. This is usually the behavior you will be looking for. |
| 136 |
\end{itemize} |
| 137 |
\end{key} |
| 138 |
\end{command} |
| 139 |
|
| 140 |
\begin{command}{\pgfplotstreampointundefined} |
| 141 |
This command indicated that the stream contains an ``undefined'' point like |
| 142 |
a point where some coordinate results for a division by zero. Such a point |
| 143 |
cannot be plotted, which is why it is not given as a parameter. However, |
| 144 |
such a point \emph{can} result in a jump in the plot, depending on the |
| 145 |
setting of the following key: |
| 146 |
% |
| 147 |
\begin{key}{/pgf/handle undefined points in plots=\meta{how} (initially jump)} |
| 148 |
\keyalias{tikz} |
| 149 |
You can set \meta{how} to one of the following values: |
| 150 |
% |
| 151 |
\begin{itemize} |
| 152 |
\item |ignore| The undefined point will be completely ignored, just |
| 153 |
as if the command had not been used at all. |
| 154 |
\item |jump| This causes the internal macro |\pgf@plotstreamjump| |
| 155 |
to be called. |
| 156 |
\end{itemize} |
| 157 |
\end{key} |
| 158 |
\end{command} |
| 159 |
|
| 160 |
\begin{command}{\pgfplotstreamnewdataset} |
| 161 |
This command indicated that in the stream a ``new data set'' starts. So, |
| 162 |
the stream does not end, but there is a logical break in the data. For |
| 163 |
example, when a table is read from a file, empty lines are interpreted as |
| 164 |
indicating new data sets. What happens when a new data set is encountered |
| 165 |
is governed by the following key: |
| 166 |
% |
| 167 |
\begin{key}{/pgf/handle new data sets in plots=\meta{how} (initially jump)} |
| 168 |
\keyalias{tikz} |
| 169 |
You can set \meta{how} to one of the following values: |
| 170 |
% |
| 171 |
\begin{itemize} |
| 172 |
\item |ignore| The command will be completely ignored, just as if |
| 173 |
the command had not been used at all. |
| 174 |
\item |jump| This causes the internal macro |\pgf@plotstreamjump| |
| 175 |
to be called. |
| 176 |
\end{itemize} |
| 177 |
\end{key} |
| 178 |
\end{command} |
| 179 |
|
| 180 |
\begin{command}{\pgfplotstreamspecial\marg{text}} |
| 181 |
This command causes |\pgf@plotstreamspecial| to be called with \meta{text} |
| 182 |
as its parameter. This allows handler-specific information to be passed to |
| 183 |
the handler. All normal handlers ignore this command. |
| 184 |
\end{command} |
| 185 |
|
| 186 |
\begin{command}{\pgfplotstreamend} |
| 187 |
This command signals that a plot stream ends. It calls |
| 188 |
|\pgf@plotstreamend|, which should now do any necessary ``cleanup''. |
| 189 |
\end{command} |
| 190 |
|
| 191 |
Note that plot streams are not buffered, that is, the different points are |
| 192 |
handled immediately. However, using the recording handler, it is possible to |
| 193 |
record a stream. |
| 194 |
|
| 195 |
|
| 196 |
\subsubsection{Commands That Generate Plot Streams} |
| 197 |
\label{section-plot-jumps} |
| 198 |
|
| 199 |
Plot streams can be created ``by hand'' as in the earlier example. However, |
| 200 |
most of the time the coordinates will be produced internally by some command. |
| 201 |
For example, the |\pgfplotxyfile| reads a file and converts it into a plot |
| 202 |
stream. |
| 203 |
|
| 204 |
\begin{command}{\pgfplotxyfile\marg{filename}} |
| 205 |
This command will try to open the file \meta{filename}. If this succeeds, |
| 206 |
it will convert the file contents into a plot stream as follows: A |
| 207 |
|\pgfplotstreamstart| is issued. Then, for each empty line a |
| 208 |
|\pgfplotstreamnewdataset| is produced. Other lines in the file should |
| 209 |
start with two numbers separated by a space, such as |0.1 1| or |100 -.3|. |
| 210 |
The numbers may be followed by some text, which will be ignore |
| 211 |
\emph{except} if it is exactly ``|u|'' or ``|o|''. For ``|u|'' the point |
| 212 |
is considered to be undefined and |\pgfplotstreampointundefined| is called. |
| 213 |
For ``|o|'' the point is considered to be an outlier and |
| 214 |
|\pgfplotstreampointoutlier| is called. Otherwise, each pair \meta{x} and |
| 215 |
\meta{y} of numbers is converted into one plot stream point in the |
| 216 |
$xy$-coordinate system. Thus, a line like |
| 217 |
% |
| 218 |
\begin{codeexample}[code only, tikz syntax=false] |
| 219 |
0 Nan u |
| 220 |
1 1 some text |
| 221 |
2 4 |
| 222 |
3 9 |
| 223 |
|
| 224 |
4 16 o |
| 225 |
5 25 oo |
| 226 |
\end{codeexample} |
| 227 |
% |
| 228 |
is turned into |
| 229 |
% |
| 230 |
\begin{codeexample}[code only] |
| 231 |
\pgfplotstreamstart |
| 232 |
\pgfplotstreampointundefined |
| 233 |
\pgfplotstreampoint{\pgfpointxy{1}{1}} |
| 234 |
\pgfplotstreampoint{\pgfpointxy{2}{4}} |
| 235 |
\pgfplotstreampoint{\pgfpointxy{3}{9}} |
| 236 |
\pgfplotstreamnewdataset |
| 237 |
\pgfplotstreampointoutlier{\pgfpointxy{4}{16}} |
| 238 |
\pgfplotstreampoint{\pgfpointxy{5}{25}} |
| 239 |
\pgfplotstreamend |
| 240 |
\end{codeexample} |
| 241 |
% |
| 242 |
(Note that the last line is not an outlier because |oo| is not the same as |
| 243 |
|o|). |
| 244 |
|
| 245 |
The two characters |%| and |#| are also allowed in a file and they are both |
| 246 |
treated as comment characters. Thus, a line starting with either of them is |
| 247 |
treated as empty. |
| 248 |
|
| 249 |
When the file has been read completely, |\pgfplotstreamend| is called. |
| 250 |
\end{command} |
| 251 |
|
| 252 |
\begin{command}{\pgfplotxyzfile\marg{filename}} |
| 253 |
This command works like |\pgfplotxyfile|, only \emph{three} numbers are |
| 254 |
expected on each non-empty line. They are converted into points in the |
| 255 |
$xyz$-coordinate system. Consider, the following file: |
| 256 |
% |
| 257 |
\begin{codeexample}[code only, tikz syntax=false] |
| 258 |
% Some comments |
| 259 |
# more comments |
| 260 |
2 -5 1 first entry |
| 261 |
2 -.2 2 o |
| 262 |
2 -5 2 third entry |
| 263 |
\end{codeexample} |
| 264 |
% |
| 265 |
It is turned into the following stream: |
| 266 |
% |
| 267 |
\begin{codeexample}[code only] |
| 268 |
\pgfplotstreamstart |
| 269 |
\pgfplotstreamnewdataset |
| 270 |
\pgfplotstreamnewdataset |
| 271 |
\pgfplotstreampoint{\pgfpointxyz{2}{-5}{1}} |
| 272 |
\pgfplotstreampointoutlier{\pgfpointxyz{2}{-.2}{2}} |
| 273 |
\pgfplotstreampoint{\pgfpointxyz{2}{-5}{2}} |
| 274 |
\pgfplotstreamend |
| 275 |
\end{codeexample} |
| 276 |
% |
| 277 |
\end{command} |
| 278 |
|
| 279 |
Currently, there is no command that can decide automatically whether the |
| 280 |
$xy$-coordinate system should be used or whether the $xyz$-system should be |
| 281 |
used. However, it would not be terribly difficult to write a ``smart file |
| 282 |
reader'' that parses coordinate files a bit more intelligently. |
| 283 |
|
| 284 |
\begin{command}{\pgfplotfunction\marg{variable}\marg{sample list}\marg{point}} |
| 285 |
This command will produce coordinates by iterating the \meta{variable} over |
| 286 |
all values in \meta{sample list}, which should be a list in the |\foreach| |
| 287 |
syntax. For each value of \meta{variable}, the \meta{point} is evaluated |
| 288 |
and the resulting coordinate is inserted into the plot stream. |
| 289 |
% |
| 290 |
\begin{codeexample}[] |
| 291 |
\begin{tikzpicture}[x=3.8cm/360] |
| 292 |
\pgfplothandlerlineto |
| 293 |
\pgfplotfunction{\x}{0,5,...,360}{\pgfpointxy{\x}{sin(\x)+sin(3*\x)}} |
| 294 |
\pgfusepath{stroke} |
| 295 |
\end{tikzpicture} |
| 296 |
\end{codeexample} |
| 297 |
|
| 298 |
\begin{codeexample}[] |
| 299 |
\begin{tikzpicture}[y=3cm/360] |
| 300 |
\pgfplothandlerlineto |
| 301 |
\pgfplotfunction{\y}{0,5,...,360}{\pgfpointxyz{sin(2*\y)}{\y}{cos(2*\y)}} |
| 302 |
\pgfusepath{stroke} |
| 303 |
\end{tikzpicture} |
| 304 |
\end{codeexample} |
| 305 |
|
| 306 |
Be warned that if the expressions that need to evaluated for each point are |
| 307 |
complex, then this command can be very slow. |
| 308 |
\end{command} |
| 309 |
|
| 310 |
\begin{command}{\pgfplotgnuplot\oarg{prefix}\marg{function}} |
| 311 |
This command will ``try'' to call the \textsc{gnuplot} program to generate |
| 312 |
the coordinates of the \meta{function}. In detail, the following happens: |
| 313 |
|
| 314 |
This command works with two files: \meta{prefix}|.gnuplot| and |
| 315 |
\meta{prefix}|.table|. If the optional argument \meta{prefix} is not |
| 316 |
given, it is set to |\jobname|. |
| 317 |
|
| 318 |
Let us start with the situation where none of these files exists. Then |
| 319 |
\pgfname\ will first generate the file \meta{prefix}|.gnuplot|. In this |
| 320 |
file it writes |
| 321 |
% |
| 322 |
\begin{codeexample}[code only, tikz syntax=false] |
| 323 |
set table "#1.table"; set format "%.5f" |
| 324 |
\end{codeexample} |
| 325 |
% |
| 326 |
where |#1| is replaced by \meta{prefix}. Then, in a second line, it writes |
| 327 |
the text \meta{function}. |
| 328 |
|
| 329 |
Next, \pgfname\ will try to invoke the program |gnuplot| with the argument |
| 330 |
\meta{prefix}|.gnuplot|. This call may or may not succeed, depending on |
| 331 |
whether the |\write18| mechanism (also known as shell escape) is switched |
| 332 |
on and whether the |gnuplot| program is available. |
| 333 |
|
| 334 |
Assuming that the call succeeded, the next step is to invoke |
| 335 |
|\pgfplotxyfile| on the file \meta{prefix}|.table|; which is exactly the |
| 336 |
file that has just been created by |gnuplot|. |
| 337 |
% |
| 338 |
\begin{codeexample}[] |
| 339 |
\begin{tikzpicture} |
| 340 |
\draw[help lines] (0,-1) grid (4,1); |
| 341 |
\pgfplothandlerlineto |
| 342 |
\pgfplotgnuplot[plots/pgfplotgnuplot-example]{plot [x=0:3.5] x*sin(x)} |
| 343 |
\pgfusepath{stroke} |
| 344 |
\end{tikzpicture} |
| 345 |
\end{codeexample} |
| 346 |
|
| 347 |
The more difficult situation arises when the |.gnuplot| file exists, which |
| 348 |
will be the case on the second run of \TeX\ on the \TeX\ file. In this case |
| 349 |
\pgfname\ will read this file and check whether it contains exactly what |
| 350 |
\pgfname\ ``would have written'' into this file. If this is not the case, |
| 351 |
the file contents is overwritten with what ``should be there'' and, as |
| 352 |
above, |gnuplot| is invoked to generate a new |.table| file. However, if |
| 353 |
the file contents is ``as expected'', the external |gnuplot| program is |
| 354 |
\emph{not} called. Instead, the \meta{prefix}|.table| file is immediately |
| 355 |
read. |
| 356 |
|
| 357 |
As explained in Section~\ref{section-tikz-gnuplot}, the net effect of the |
| 358 |
above mechanism is that |gnuplot| is called as seldom as possible and that |
| 359 |
when you pass along the |.gnuplot| and |.table| files with your |.tex| file |
| 360 |
to someone else, that person can \TeX\ the |.tex| file without having |
| 361 |
|gnuplot| installed and without having the |\write18| mechanism switched |
| 362 |
on. |
| 363 |
|
| 364 |
\begin{key}{/pgf/plot/gnuplot call=\meta{gnuplot invocation} (initially gnuplot)} |
| 365 |
This key can be used to change the way gnuplot is called. |
| 366 |
|
| 367 |
Some portable MiK\TeX{} distribution needs something like the |
| 368 |
following. |
| 369 |
% |
| 370 |
\begin{codeexample}[code only] |
| 371 |
\pgfkeys{/pgf/plot/gnuplot call="/Programs/gnuplot/binary/gnuplot"} |
| 372 |
\end{codeexample} |
| 373 |
\end{key} |
| 374 |
\end{command} |
| 375 |
|
| 376 |
|
| 377 |
\subsection{Plot Handlers} |
| 378 |
\label{section-plot-handlers} |
| 379 |
|
| 380 |
A \emph{plot handler} determines what ``should be done'' with a plot stream. |
| 381 |
You must set the plot handler before the stream starts. The following commands |
| 382 |
install the most basic plot handlers; more plot handlers are defined in the |
| 383 |
file |pgflibraryplothandlers|, which is documented in |
| 384 |
Section~\ref{section-library-plothandlers}. |
| 385 |
|
| 386 |
All plot handlers work by setting or redefining the following three macros: |
| 387 |
|\pgf@plotstreamstart|, |\pgf@plotstreampoint|, and |\pgf@plotstreamend|. |
| 388 |
|
| 389 |
\begin{command}{\pgfplothandlerlineto} |
| 390 |
This handler will issue a |\pgfpathlineto| command for each point of the |
| 391 |
plot, \emph{except} possibly for the first. What happens with the first |
| 392 |
point can be specified using the two commands described below. |
| 393 |
% |
| 394 |
\begin{codeexample}[] |
| 395 |
\begin{pgfpicture} |
| 396 |
\pgfpathmoveto{\pgfpointorigin} |
| 397 |
\pgfplothandlerlineto |
| 398 |
\pgfplotstreamstart |
| 399 |
\pgfplotstreampoint{\pgfpoint{1cm}{0cm}} |
| 400 |
\pgfplotstreampoint{\pgfpoint{2cm}{1cm}} |
| 401 |
\pgfplotstreampoint{\pgfpoint{3cm}{2cm}} |
| 402 |
\pgfplotstreampoint{\pgfpoint{1cm}{2cm}} |
| 403 |
\pgfplotstreamend |
| 404 |
\pgfusepath{stroke} |
| 405 |
\end{pgfpicture} |
| 406 |
\end{codeexample} |
| 407 |
% |
| 408 |
\end{command} |
| 409 |
|
| 410 |
\begin{command}{\pgfsetmovetofirstplotpoint} |
| 411 |
Specifies that the line-to plot handler (and also some other plot handlers) |
| 412 |
should issue a move-to command for the first point of the plot instead of a |
| 413 |
line-to. This will start a new part of the current path, which is not |
| 414 |
always, but often, desirable. This is the default. |
| 415 |
\end{command} |
| 416 |
|
| 417 |
\begin{command}{\pgfsetlinetofirstplotpoint} |
| 418 |
Specifies that plot handlers should issue a line-to command for the first |
| 419 |
point of the plot. |
| 420 |
% |
| 421 |
\begin{codeexample}[] |
| 422 |
\begin{pgfpicture} |
| 423 |
\pgfpathmoveto{\pgfpointorigin} |
| 424 |
\pgfsetlinetofirstplotpoint |
| 425 |
\pgfplothandlerlineto |
| 426 |
\pgfplotstreamstart |
| 427 |
\pgfplotstreampoint{\pgfpoint{1cm}{0cm}} |
| 428 |
\pgfplotstreampoint{\pgfpoint{2cm}{1cm}} |
| 429 |
\pgfplotstreampoint{\pgfpoint{3cm}{2cm}} |
| 430 |
\pgfplotstreampoint{\pgfpoint{1cm}{2cm}} |
| 431 |
\pgfplotstreamend |
| 432 |
\pgfusepath{stroke} |
| 433 |
\end{pgfpicture} |
| 434 |
\end{codeexample} |
| 435 |
% |
| 436 |
\end{command} |
| 437 |
|
| 438 |
\begin{command}{\pgfplothandlerpolygon} |
| 439 |
This handler works like the line-to plot handler, only the line is closed |
| 440 |
at the end using |\pgfpathclose|, resulting in a polygon. |
| 441 |
\end{command} |
| 442 |
|
| 443 |
\begin{command}{\pgfplothandlerdiscard} |
| 444 |
This handler will simply throw away the stream. |
| 445 |
\end{command} |
| 446 |
|
| 447 |
\begin{command}{\pgfplothandlerrecord\marg{macro}} |
| 448 |
When this handler is installed, each time a plot stream command is called, |
| 449 |
this command will be appended to \meta{macro}. Thus, at the end of the |
| 450 |
stream, \meta{macro} will contain all the commands that were issued on the |
| 451 |
stream. You can then install another handler and invoke \meta{macro} to |
| 452 |
``replay'' the stream (possibly many times). |
| 453 |
% |
| 454 |
\begin{codeexample}[] |
| 455 |
\begin{pgfpicture} |
| 456 |
\pgfplothandlerrecord{\mystream} |
| 457 |
\pgfplotstreamstart |
| 458 |
\pgfplotstreampoint{\pgfpoint{1cm}{0cm}} |
| 459 |
\pgfplotstreampoint{\pgfpoint{2cm}{1cm}} |
| 460 |
\pgfplotstreampoint{\pgfpoint{3cm}{1cm}} |
| 461 |
\pgfplotstreampoint{\pgfpoint{1cm}{2cm}} |
| 462 |
\pgfplotstreamend |
| 463 |
\pgfplothandlerlineto |
| 464 |
\mystream |
| 465 |
\pgfplothandlerclosedcurve |
| 466 |
\mystream |
| 467 |
\pgfusepath{stroke} |
| 468 |
\end{pgfpicture} |
| 469 |
\end{codeexample} |
| 470 |
% |
| 471 |
\end{command} |
| 472 |
|
| 473 |
|
| 474 |
\subsection{Defining New Plot Handlers} |
| 475 |
|
| 476 |
You can define new plot handlers using the following command: |
| 477 |
|
| 478 |
\begin{command}{\pgfdeclareplothandler\marg{macro}\marg{arguments}\marg{configuration}} |
| 479 |
This command creates a new plot handler that can subsequently be called |
| 480 |
using the macro \meta{macro}. This macro take the arguments given in |
| 481 |
\meta{arguments}, which can be a list like |#1#2| if \meta{macro} should be |
| 482 |
invoked with two arguments. Here is a typical example: |
| 483 |
% |
| 484 |
\begin{codeexample}[code only] |
| 485 |
\pgfdeclareplothandler{\myhandler}{#1}{...} |
| 486 |
... |
| 487 |
\myhandler{foo} |
| 488 |
\pgfplotstreamstart |
| 489 |
... |
| 490 |
\pgfplotstreamend |
| 491 |
\end{codeexample} |
| 492 |
|
| 493 |
The \meta{configuration} is used to define the behavior of the handler. It |
| 494 |
is a list of key--value pairs, where the following keys are allowed: |
| 495 |
% |
| 496 |
\begin{itemize} |
| 497 |
\item |start=|\meta{code}. The \meta{code} will be executed whenever |
| 498 |
|\pgfplotstreamstart| is used while the handler \meta{macro} is |
| 499 |
selected. Inside the \meta{code}, you can use |#1|, |#2|, and so on |
| 500 |
to refer to the parameters that were given to \meta{macro}: |
| 501 |
% |
| 502 |
\begin{codeexample}[width=6cm] |
| 503 |
\pgfdeclareplothandler{\myhandler}{#1}{ |
| 504 |
start = Hi #1., |
| 505 |
end = Bye #1., |
| 506 |
} |
| 507 |
\myhandler{foo} |
| 508 |
\pgfplotstreamstart |
| 509 |
\pgfplotstreamend |
| 510 |
\myhandler{bar} |
| 511 |
\pgfplotstreamstart |
| 512 |
\pgfplotstreamend |
| 513 |
\end{codeexample} |
| 514 |
% |
| 515 |
\item |end=|\meta{code} Works just like |start|. |
| 516 |
\item |point=|\meta{code}. The \meta{code} will be executed whenever |
| 517 |
|\pgfplotstreampoint| is used while the handler \meta{macro} is in |
| 518 |
force. Inside the \meta{code}, you can use |#1|, |#2|, and so on to |
| 519 |
refer to the arguments give to \meta{macro}, while you can use |
| 520 |
|##1| to refer to the argument given to |\pgfplotstreampoint| |
| 521 |
itself (this will be the coordinate). |
| 522 |
% |
| 523 |
\begin{codeexample}[] |
| 524 |
\pgfdeclareplothandler{\myhandler}{#1}{ |
| 525 |
point=\pgfpathcircle{##1}{#1} % ##1 is the coordinate, |
| 526 |
% #1 the parameter for \myhandler |
| 527 |
} |
| 528 |
\begin{pgfpicture} |
| 529 |
\myhandler{1pt} |
| 530 |
\pgfplotstreamstart |
| 531 |
\pgfplotstreampoint{\pgfpoint{0pt}{0pt}} |
| 532 |
\pgfplotstreampoint{\pgfpoint{3pt}{3pt}} |
| 533 |
\pgfplotstreampoint{\pgfpoint{6pt}{3pt}} |
| 534 |
\pgfplotstreampoint{\pgfpoint{9pt}{0pt}} |
| 535 |
\pgfplotstreamend |
| 536 |
\pgfusepath{stroke} |
| 537 |
\myhandler{3pt} |
| 538 |
\pgfplotstreamstart |
| 539 |
\pgfplotstreampoint{\pgfpoint{0pt}{0pt}} |
| 540 |
\pgfplotstreampoint{\pgfpoint{9pt}{0pt}} |
| 541 |
\pgfplotstreamend |
| 542 |
\pgfusepath{stroke} |
| 543 |
\end{pgfpicture} |
| 544 |
\end{codeexample} |
| 545 |
% |
| 546 |
The \meta{code} will also be called for |
| 547 |
|\pgfplotstreampointoutlier| when this command has been configured |
| 548 |
to |plot| the outliers. |
| 549 |
\item |jump=|\meta{code} The \meta{code} will be called whenever a jump |
| 550 |
has been requested indirectly via an outlier point, and undefined |
| 551 |
point, or a new data set (for each of which the command needs to be |
| 552 |
configured to |jump|). As always, inside the \meta{code} you can |
| 553 |
access |#1| and so on. |
| 554 |
\item |special=|\meta{code} Causes \meta{code} to be executed whenever |
| 555 |
|\pgfplotstreamspecial|\marg{something} is used. Inside the |
| 556 |
\meta{code}, you can access \meta{something} via |##1| and the |
| 557 |
parameters of \meta{macro} as |#1|, |#2|, and so on. |
| 558 |
\end{itemize} |
| 559 |
|
| 560 |
In addition to the above keys, there exist also ``code macro versions'' of |
| 561 |
them: |
| 562 |
% |
| 563 |
\begin{itemize} |
| 564 |
\item |point macro=|\meta{some macro}. Causes |\pgfplotstreampoint| to |
| 565 |
call \meta{some macro} directly (actually, |\pgf@plotstreampoint| |
| 566 |
is set to be equal to \meta{some macro}). Inside the \meta{some |
| 567 |
macro} you can use |#1| to access the coordinate passed to |
| 568 |
|\pgfplotstreampoint| and you can no longer access the parameters |
| 569 |
passed to the original call to \meta{macro} that installed the |
| 570 |
handler. So, \meta{some macro} must take exactly one argument, |
| 571 |
namely |#1|. |
| 572 |
\item |special macro=|\meta{some macro}. As |point macro|, only for |
| 573 |
specials. |
| 574 |
\item |start macro=|\meta{some macro}. Causes \meta{some macro} to be |
| 575 |
executed at the start. This macro, like the below ones, may not |
| 576 |
take any parameters and will not have access to the parameters |
| 577 |
passed to the original \meta{macro}. |
| 578 |
\item |end macro=|\meta{some macro}. As above. |
| 579 |
\item |jump macro=|\meta{some macro}. As above. |
| 580 |
\end{itemize} |
| 581 |
\end{command} |
| 582 |
|
| 583 |
|
| 584 |
%%% Local Variables: |
| 585 |
%%% mode: latex |
| 586 |
%%% TeX-master: "pgfmanual" |
| 587 |
%%% End: |