Debian Codegraph @

texmf-dist/tex/generic/pgf/modules/pgfmoduleplot.code.tex

raw
1 % Copyright 2019 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 Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10 \ProvidesFileRCS{pgfmoduleplot.code.tex}
11
12
13 % PGF's plotting interface works as follows:
14 %
15 % In order to plot something, two things need to be done. First, you
16 % need to provide the coordinates (obviously) of the points that
17 % should be plotted. The coordinates are given via a long stream of
18 % commands. These commands are \pgfplotstreamstart, which is
19 % given exactly once at the beginning, the commands
20 % \pgfplotstreampoint, \pgfplotstreampointoutlier,
21 % \pgfplotstreampointundefined, \pgfplotstreamnewdataset, of which
22 % there are numerous in the middle, the special \pgfplotstreamspecial,
23 % of which there may be numerous in the middle, and \pgfplotstreamend,
24 % which must be given at the end. Between these commands arbitrary
25 % other commands may be given. Here is an example:
26 %
27 % ...
28 % \pgfplotstreamstart
29 % \pgfplotstreampoint{\pgfpointxy{0}{0}}
30 % \pgfplotstreampoint{\pgfpointxy{1}{1}}
31 % \pgfplotstreamnewdataset
32 % \pgfplotstreampoint{\pgfpointxy{2}{4}}
33 % \relax
34 % \pgfplotstreampointoutlier{\pgfpointxy{3}{9}}
35 % \pgfplotstreamspecial{some handler-dependent special stuff}
36 % \pgfplotstreamend
37 %
38 % By themselves, the \pgfplotstreamxxxx commands do not do anything by
39 % default. Rather, to ``use'' such a stream, you must first install a
40 % stream handler. For example, the ``lineto'' handler will simply
41 % basically every \pgfplotstreampoint into a \pgfpathlineto.
42 %
43 % One special things is the handling of "jumps" in a stream. For
44 % instance, when a lineto handler encounters an "outlier" or a new
45 % data set, the current line should end and a new subpath should start
46 % (unless configured otherwise). For this, the special jump handler is
47 % important.
48 %
49 % Example:
50 %
51 % \pgfpathmoveto{\pgfpointorigin}
52 %
53 % \pgfplothandlerlineto
54 % \pgfplotstreamstart
55 % \pgfplotstreampoint{\pgfpointxy{0}{0}}
56 % \pgfplotstreampoint{\pgfpointxy{1}{1}}
57 % \pgfplotstreampoint{\pgfpointxy{2}{4}}
58 % \relax
59 % \pgfplotstreampoint{\pgfpointxy{3}{9}}
60 % \pgfplotstreamend
61
62
63 % The stream commands actually call their ``internal'' versions, which
64 % are set by the handlers:
65 \def\pgfplotstreamstart{\pgf@plotstreamstart}%
66 \def\pgfplotstreampoint#1{\gdef\pgfplotlastpoint{#1}\pgf@plotstreampoint{#1}}%
67 \def\pgfplotstreampointoutlier#1{\pgfplot@outliers{}{\pgf@plotstreamjump}{\pgfplotstreampoint{#1}}}%
68 \def\pgfplotstreampointundefined{\pgfplot@outliers{}{\pgf@plotstreamjump}{}}%
69 \def\pgfplotstreamnewdataset{\pgfplot@outliers{}{\pgf@plotstreamjump}{}}%
70 \def\pgfplotstreamspecial{\pgf@plotstreamspecial}%
71 \def\pgfplotstreamend{\pgf@plotstreamend}%
72
73 \def\pgfplot@ignorer#1#2#3{#1}%
74 \def\pgfplot@jumper#1#2#3{#2}%
75 \def\pgfplot@plotter#1#2#3{#3}%
76
77 \let\pgfplot@outliers\pgfplot@jumper
78 \let\pgfplot@undefined\pgfplot@jumper
79 \let\pgfplot@newdata\pgfplot@jumper
80
81 \pgfset{
82 handle outlier points in plots/.is choice,
83 handle outlier points in plots/ignore/.code=\let\pgfplot@outliers\pgfplot@ignorer,
84 handle outlier points in plots/jump/.code=\let\pgfplot@outliers\pgfplot@jumper,
85 handle outlier points in plots/plot/.code=\let\pgfplot@outliers\pgfplot@plotter,
86 handle undefined points in plots/.is choice,
87 handle undefined points in plots/ignore/.code=\let\pgfplot@undefined\pgfplot@ignorer,
88 handle undefined points in plots/jump/.code=\let\pgfplot@undefined\pgfplot@jumper,
89 handle new data sets in plots/.is choice,
90 handle new data sets in plots/ignore/.code=\let\pgfplot@newdata\pgfplot@ignorer,
91 handle new data sets in plots/jump/.code=\let\pgfplot@newdata\pgfplot@jumper,
92 }%
93
94
95 % Declares a new plot handler
96 %
97 % #1 = macro that should install the stream handler subsequently
98 % #2 = parameter list of said macro.
99 % #3 = keys as described below
100 %
101 % Description:
102 %
103 % When you declare a new plot handler, you provide, through keys,
104 % different "actions" that should be taken when the different stream
105 % commands are used:
106 %
107 % start = some code to be executed when \pgfplotstreamstart is called
108 % for this plothandler
109 % end = same for the end of the stream
110 % point = code to be executed for a point of the stream
111 % jump = code to be executed for a "jump" in the stream
112 % special = code to be executed for \pgfplotstreamspecial
113 % start macro = here and for the following keys, the to-be-executed
114 % code is stored in the given macro
115 % end macro = see above
116 % jump macro = see above
117 % point macro = see above
118 % special macro = see above
119 %
120 % Example:
121 %
122 % \pgfdeclareplothandler{\ultrasimplehandler}{}{
123 % point=\pgfpathlineto{##1}
124 % }
125
126 \def\pgfdeclareplothandler#1#2#3{%
127 \def#1#2{%
128 \pgfkeys{%
129 /pgf/plots/@handler options/.cd,
130 start=\relax,
131 end macro=\relax,
132 point macro=\pgfutil@gobble,
133 jump macro=\relax,
134 special macro=\pgfutil@gobble,%
135 #3%
136 }%
137 }%
138 }%
139
140 \pgfkeys{%
141 /pgf/plots/@handler options/.cd,
142 start/.code=%
143 \gdef\pgf@plotstreamstart{%
144 \global\pgf@plot@startedfalse%
145 \global\let\pgf@plotstreamend\pgf@plotstreamend@init%
146 \global\let\pgf@plotstreampoint\pgf@plotstreampoint@init%
147 \global\let\pgf@plotstreamjump\pgf@plotstreamjump@init%
148 \global\let\pgf@plotstreamspecial\pgf@plotstreamspecial@init%
149 #1%
150 },%
151 start macro/.code=%
152 \gdef\pgf@plotstreamstart{%
153 \global\pgf@plot@startedfalse%
154 \global\let\pgf@plotstreamend\pgf@plotstreamend@init%
155 \global\let\pgf@plotstreampoint\pgf@plotstreampoint@init%
156 \global\let\pgf@plotstreamjump\pgf@plotstreamjump@init%
157 \global\let\pgf@plotstreamspecial\pgf@plotstreamspecial@init%
158 #1%
159 },%
160 end/.code=\gdef\pgf@plotstreamend@init{#1},
161 end macro/.code=\global\let\pgf@plotstreamend@init#1,
162 point/.code=\gdef\pgf@plotstreampoint@init##1{#1},
163 point macro/.code=\global\let\pgf@plotstreampoint@init#1,
164 jump/.code=\gdef\pgf@plotstreamjump@init{#1},
165 jump macro/.code=\global\let\pgf@plotstreamjump@init#1,
166 special/.code=\gdef\pgf@plotstreamspecial@init##1{#1},
167 special macro/.code=\global\let\pgf@plotstreamspecial@init#1,
168 }%
169
170 \newif\ifpgf@plot@started
171
172
173
174
175 % Sets the action taken for the first point of a plot to a lineto.
176 %
177 % Description:
178 %
179 % For certain handlers it makes sense either the start a plot by
180 % moving to the first point of the plot or to do a lineto to that
181 % first point. Using this command this action can be set to a lineto.
182 %
183 % Example:
184 %
185 % \pgfsetlinetofirstplotpoint
186
187 \def\pgfsetlinetofirstplotpoint{\let\pgf@plot@first@action=\pgfpathlineto}%
188
189
190 % Sets the action taken for the first point of a plot to a moveto.
191 %
192 % Example:
193 %
194 % \pgfsetmovetofirstplotpoint
195
196 \def\pgfsetmovetofirstplotpoint{\let\pgf@plot@first@action=\pgfpathmoveto}%
197
198 \let\pgf@plot@first@action=\pgfpathmoveto
199
200
201
202
203 %
204 % Handlers
205 %
206
207
208 % This handler converts each plot stream command into a lineto
209 % command, except for the first, which is converted to the action that
210 % has previously been specified using \pgfsetlinetofirstplotpoint or
211 % \pgfsetmovetofirstplotpoint.
212 %
213 % Example:
214 %
215 % \pgfplothandlerlineto
216 % \pgfplotxyfile{mytable}
217
218 \pgfdeclareplothandler{\pgfplothandlerlineto}{}{
219 point macro=\pgf@plot@line@handler,
220 jump=\global\let\pgf@plotstreampoint\pgf@plot@line@handler@move%
221 }%
222
223 \def\pgf@plot@line@handler#1{%
224 \pgf@plot@first@action{#1}%
225 \global\let\pgf@plotstreampoint=\pgfpathlineto%
226 }%
227
228 \def\pgf@plot@line@handler@move#1{%
229 \pgfpathmoveto{#1}%
230 \global\let\pgf@plotstreampoint=\pgfpathlineto%
231 }%
232
233
234
235 % This handler turns creates a series of lineto commands, with the
236 % last command being a closepath, resulting in a closed path. If a
237 % jump is encountered, the current subpath is closed and a new subpath
238 % is started.
239 %
240 % Example:
241 %
242 % \pgfplothandlerpolygon
243 % \pgfplotxyfile{mytable}
244
245 \pgfdeclareplothandler{\pgfplothandlerpolygon}{}{%
246 point macro=\pgf@plot@line@handler@close,
247 jump macro=\pgf@plot@next@close@and@moveto,
248 end macro=\pgf@plot@polygon@stop
249 }%
250
251 \def\pgf@plot@line@handler@close#1{%
252 \pgfpathmoveto{#1}%
253 \global\pgf@plot@startedtrue%
254 \global\let\pgf@plotstreampoint=\pgfpathlineto%
255 }%
256
257 \def\pgf@plot@polygon@stop{%
258 \ifpgf@plot@started%
259 \pgfpathclose%
260 \fi%
261 \global\pgf@plot@startedfalse%
262 }%
263
264 \def\pgf@plot@next@close@and@moveto{%
265 \ifpgf@plot@started%
266 \pgfpathclose%
267 \fi%
268 \global\let\pgf@plotstreampoint\pgf@plot@line@handler@close%
269 }%
270
271
272 % More handlers are defined in pgflibraryplothandlers
273
274
275
276
277
278 % This handler discards the plot.
279 %
280 % Example:
281 %
282 % \pgfplothandlerdiscard
283 % \pgfplotxyfile{mytable}
284
285 \pgfdeclareplothandler{\pgfplothandlerdiscard}{}{}%
286
287
288
289 % This handler records each plot stream command to a macro. This is
290 % useful if plot commands are difficult to generate and need to be
291 % ``recycled'' later on.
292 %
293 % Example:
294 %
295 % \pgfplothandlerrecord{\myplot}
296 % \pgfplotxyfile{mytable} % stored in \myplot now
297 % \pgfplothandlerline
298 % \myplot
299 % \pgftransformxshift{1cm}
300 % \myplot
301
302 \pgfdeclareplothandler{\pgfplothandlerrecord}{#1}{%
303 start=\gdef#1{\pgfplotstreamstart},
304 point=\expandafter\gdef\expandafter#1\expandafter{#1\pgfplotstreampoint{##1}},
305 jump=\expandafter\gdef\expandafter#1\expandafter{#1\pgf@plotstreamjump},
306 special=\expandafter\gdef\expandafter#1\expandafter{#1\pgfplotstreamspecial{##1}},
307 end=\expandafter\gdef\expandafter#1\expandafter{#1\pgfplotstreamend},
308 }%
309
310
311
312 % Read a plot stream from a file and plot it.
313 %
314 % #1 = file from which to read things
315 %
316 % File format:
317 %
318 % Each line of the file should begin with two numbers separated by a
319 % space. Such a line with number #1 and #2 is converted to a
320 % \pgfplotstreampoint{\pgfpointxy{#1}{#2}}. Extra characters following
321 % on the line are ignored.
322 %
323 % Lines starting with ``%'' and ``#'' are ignored.
324 %
325 % Example:
326 %
327 % \pgfplotxyfile{tableformgnuplot.dat}
328
329 \def\pgfplotxyfile#1{%
330 \begingroup%
331 \def\b@pgfplotsxyfile@scanning@for@first{1}%
332 \pgfplotstreamstart%
333 \openin\r@pgf@reada=#1
334 \ifeof\r@pgf@reada
335 \pgferror{Plot data file `#1' not found.}
336 \else
337 \catcode`\#=14
338 \catcode`\^^M=5
339 \pgf@readxyfile%
340 \fi
341 \pgfplotstreamend%
342 \endgroup%
343 }%
344
345 \let\pgf@savedpar=\par
346 \def\pgf@partext{\par}%
347 \def\pgf@readxyfile{%
348 \pgfutil@read\r@pgf@reada to \pgf@temp%
349 \let\par=\pgf@savedpar%
350 \edef\pgf@temp{\pgf@temp}%
351 \ifx\pgf@temp\pgfutil@empty%
352 \if1\b@pgfplotsxyfile@scanning@for@first
353 \else
354 \ifeof\r@pgf@reada\else\pgfplotstreamnewdataset\fi%
355 \fi
356 \else\ifx\pgf@temp\pgf@partext%
357 \if1\b@pgfplotsxyfile@scanning@for@first
358 \else
359 \ifeof\r@pgf@reada\else\pgfplotstreamnewdataset\fi%
360 \fi
361 \else%
362 \expandafter\pgf@parsexyline\pgf@temp\pgf@stop%
363 \fi\fi%
364 \ifeof\r@pgf@reada\else\expandafter\pgf@readxyfile\fi%
365 }%
366
367 \def\pgf@parsexyline#1 #2 #3\pgf@stop{%
368 \def\b@pgfplotsxyfile@scanning@for@first{0}%
369 \edef\pgf@xyline@flag@val{#3}%
370 \ifx\pgf@xyline@flag@val\pgf@xyline@flag@undef%
371 \pgfplotstreampointundefined%
372 \else\ifx\pgf@xyline@flag@val\pgf@xyline@flag@out%
373 \pgfplotstreampointoutlier{\pgfpointxy{#1}{#2}}%
374 \else%
375 \pgfplotstreampoint{\pgfpointxy{#1}{#2}}%
376 \fi\fi%
377 }%
378
379 \edef\pgf@xyline@flag@out{o\space}%
380 \edef\pgf@xyline@flag@undef{u\space}%
381
382
383
384
385 % Read a plot stream from a file and plot it.
386 %
387 % #1 = file from which to read things
388 %
389 % File format:
390 %
391 % Like xy, except that each line contains three numbers, which are
392 % converted to xyz coordinates.
393 %
394 % Example:
395 %
396 % \pgfplotxyfile{tableformgnuplot.dat}
397
398 \def\pgfplotxyzfile#1{%
399 \begingroup%
400 \pgfplotstreamstart%
401 \openin\r@pgf@reada=#1
402 \ifeof\r@pgf@reada
403 \pgferror{Plot data file `#1' not found.}
404 \else
405 \catcode`\#=14
406 \catcode`\^^M=5
407 \pgf@readxyzfile%
408 \fi
409 \pgfplotstreamend%
410 \endgroup%
411 }%
412
413 \def\pgf@readxyzfile{%
414 \pgfutil@read\r@pgf@reada to \pgf@temp%
415 \ifx\pgf@temp\pgfutil@empty%
416 \if1\b@pgfplotsxyfile@scanning@for@first
417 \else
418 \ifeof\r@pgf@reada\else\pgfplotstreamnewdataset\fi%
419 \fi
420 \else\ifx\pgf@temp\pgf@partext%
421 \if1\b@pgfplotsxyfile@scanning@for@first
422 \else
423 \ifeof\r@pgf@reada\else\pgfplotstreamnewdataset\fi%
424 \fi
425 \else%
426 \expandafter\pgf@parsexyzline\pgf@temp\pgf@stop%
427 \fi\fi%
428 \ifeof\r@pgf@reada\else\expandafter\pgf@readxyzfile\fi%
429 }%
430
431 \def\pgf@parsexyzline#1 #2 #3 #4\pgf@stop{%
432 \def\b@pgfplotsxyfile@scanning@for@first{0}%
433 \edef\pgf@xyline@flag@val{#4}%
434 \ifx\pgf@xyline@flag@val\pgf@xyline@flag@undef%
435 \pgfplotstreampointundefined%
436 \else\ifx\pgf@xyline@flag@val\pgf@xyline@flag@out%
437 \pgfplotstreampointoutlier{\pgfpointxyz{#1}{#2}{#3}}%
438 \else%
439 \pgfplotstreampoint{\pgfpointxyz{#1}{#2}{#3}}%
440 \fi\fi%
441 }%
442
443
444
445
446 % Render a function using gnuplot.
447 %
448 % #1 = filename prefix for .gnuplot and .table files (optional,
449 % default is \jobname)
450 % #2 = gnuplot function text
451 %
452 % Description:
453 %
454 % This command will write a file called #1.gnuplot that sets up
455 % some gnuplot commands to write output to a file called
456 % #1.table. Then it calls gnuplot (using the \write18 mechanism)
457 % to execute the file. Then it reads #2.table using \pgfplotxyfile.
458 %
459 % Example:
460 %
461 % \pgfplothandlerlineto
462 % \pgfplotgnuplot[\jobname]{plot [x=0:5] x*sin(x)}
463
464 {%
465 \catcode`\%=12
466 \catcode`\"=12
467 \xdef\pgf@gnuplot@head{set table \noexpand\pgf@plottablefile@quoted; set format "%.5f"}
468 }%
469
470 \let\pgf@plotwrite=\w@pgf@writea
471 \newif\ifpgf@resample@plot
472
473 \pgfkeys{%
474 /pgf/plot/gnuplot call/.initial={gnuplot}}%
475
476 \def\pgfplotgnuplot{\pgfutil@ifnextchar[{\pgf@plotgnuplot}{\pgf@plotgnuplot[\jobname]}}%}%
477 \def\pgf@plotgnuplot[#1]#2{%
478 \pgf@resample@plottrue%
479 \pgfutilpreparefilename{#1.gnuplot}%
480 \let\pgf@plotgnuplotfile=\pgfretval
481 \pgfutilpreparefilename{#1.table}%
482 \let\pgf@plottablefile=\pgfretval
483 \let\pgf@plottablefile@quoted=\pgfretvalquoted
484 \edef\pgf@plot@code{\pgfutil@trimspaces@e{#2}}%
485 \begingroup
486 \endlinechar=-1 % suppress white space at end
487 % Check, whether it is up-to-date
488 \openin\pgfutil@inputcheck=\pgf@plotgnuplotfile\relax
489 \ifeof\pgfutil@inputcheck%
490 \else%
491 \pgfutil@read\pgfutil@inputcheck to\pgf@temp% ignored
492 \pgfutil@read\pgfutil@inputcheck to\pgf@plot@line%
493 \closein\pgfutil@inputcheck
494 \ifx\pgf@plot@code\pgf@plot@line%
495 \openin\pgfutil@inputcheck=\pgfretval\relax
496 \ifeof\pgfutil@inputcheck%
497 \else%
498 \closein\pgfutil@inputcheck
499 \pgf@resample@plotfalse%
500 \fi%
501 \fi%
502 \fi
503 \ifpgf@resample@plot%
504 \immediate\openout\pgf@plotwrite=\pgf@plotgnuplotfile\relax
505 \immediate\pgfutil@write\pgf@plotwrite{\pgf@gnuplot@head}%
506 \immediate\pgfutil@write\pgf@plotwrite{\pgf@plot@code}%
507 \immediate\closeout\pgf@plotwrite%
508 \pgfutil@shellescape{%
509 \pgfkeysvalueof{/pgf/plot/gnuplot call} \pgf@plotgnuplotfile}%
510 \fi%
511 \endgroup
512 % \let\pgf@savedparsexyline=\pgf@parsexyline%
513 % \let\pgf@parsexyline=\pgf@parsegnuplotxyline%
514 \pgfplotxyfile{\pgf@plottablefile}%
515 % \let\pgf@parsexyline=\pgf@savedparsexyline%
516 }%
517
518 % \def\pgf@parsegnuplotxyline#1 #2 #3\pgf@stop{%
519 % \edef\pgf@xyline@flag@val{#3}%
520 % \edef\pgf@xyline@flag@out{o\space}%
521 % \edef\pgf@xyline@flag@undef{u\space}%
522 % \ifx\pgf@xyline@flag@val\pgf@xyline@flag@undef%
523 % \pgfplotstreampointundefined%
524 % \else\ifx\pgf@xyline@flag@val\pgf@xyline@flag@out%
525 % \pgfplotstreampointoutlier{\pgfpointxy{#1}{#2}}%
526 % \else%
527 % \pgfplotstreampoint{\pgfpointxy{#1}{#2}}%
528 % \fi\fi%
529 % }%
530
531 % This producer handler plots a function using pgf's mathematical engine.
532 %
533 % #1 = variable
534 % #2 = domain for the variable
535 % #3 = point, typically defined in terms of the value of the variable
536 %
537 % Description:
538 %
539 % This producer will iterate the variable #1 over all variables in #2
540 % (using the \foreach statement). For each value, a plot coordinate
541 % #3 is created.
542 %
543 % Note that this command is pretty slow.
544 %
545 % Example:
546 %
547 % \pgfplothandlerlineto
548 % \pgfplotfunction{\x}{0,0.1,...,3.141}{\pgfpointxy{\x}{sin(\x)}}
549
550 \def\pgfplotfunction#1#2#3{%
551 \pgfplotstreamstart%
552 \foreach#1in{#2}%
553 {%
554 \pgf@process{#3}%
555 \edef\pgf@marshal{\noexpand\pgfplotstreampoint{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
556 \pgf@marshal%
557 }
558 \pgfplotstreamend%
559 }%
560
561
562
563
564 \endinput