summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/pgfplots/pgfplots.reference.tikzinteroperability.tex
blob: 22c2132ac45693ccc505c74796c2a3fee4e89aef (plain)
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
\subsection{\tikzname\ Interoperability}
\label{pgfplots:tikz:interoperability}

\PGFPlots\ is built on top of \Tikz/\pgfname, and it inherits most of power to visualize plots. However, their coordinate systems do not match up -- for good reason: \PGFPlots\ operates on logical (data) coordinates whereas \Tikz\ operates on image coordinates. 

Occasionally, one may want to synchronize both in order to generate a graphic -- and the question arises how to match the coordinates from \Tikz\ to \PGFPlots\ and vice--versa. This section explains how to match coordinates and it discusses the necessary restrictions.

There are a couple of keys in \PGFPlots\ which control the mapping of coordinates. The purpose of these keys is to implement visualization techniques, but they do things different than \Tikz\ (and they should). To match coordinates with \Tikz, one needs the following aspects:
\begin{enumerate}
	\item Restrict your visualization type: a logarithmic axis simply may not fit into \Tikz\ (to be more precise: it may fit, but a \Tikz\ unit will correspond to a log--unit in \PGFPlots).

	\item Configure matching unit vectors by means of the |x| and |y| keys. The default configuration of \Tikz\ is to use |x=1cm,y=1cm,z={(0,0)}|. Note that these settings are usually overridden by \PGFPlots\ in order to respect |width| and |height| (and |view| for three--dimensional axes).
	\item Disable the data scaling by means of |disabledatascaling|: \PGFPlots\ will internally apply linear coordinate transformations in order to provide the data range required for floating point arithmetics (using approximately floating point precision). Disabling the data scaling means to restrict yourself to the (small) data range supported by \Tikz---but that's probably what you want in that case.
	\item Define |anchor| and position of the |axis|, probably using |anchor=origin,at={(0,0)}|. The |at={(0,0)}| configures \PGFPlots\ to place the axis at the \Tikz\ position |(0,0)| whereas |anchor=origin| means that \PGFPlots\ will place its data origin $(0,0,0)$ at the place designated by |at| (see Section~\ref{pgfplots:sec:align} for details).

	\item Make sure that the \PGFPlots\ axis contains the data origin $(0,0,0)$ in the displayed data range (i.e.\ configure |xmin|, |xmax|, |ymin|, and |ymax| appropriately).

	Without this, the |anchor=origin| key required in the previous item will be truncated to the next coordinate which is part of the displayed range.
\end{enumerate}

\noindent Here is a simple example, first with \Tikz:
\begin{codeexample}[]
\begin{tikzpicture}
\coordinate (Point) at (1,2);

\draw [gray] (-1,-1) grid (3,3);
\draw [blue,fill] (Point) circle (2pt)
	node [right] {(1,2)};
\end{tikzpicture}
\end{codeexample}
\noindent it displays a grid with $x,y\in[-1,3]$ and shows a node inside of it.
Now, we apply the keys discussed above to match this setting in \PGFPlots:
\begin{codeexample}[]
\begin{tikzpicture}
    \coordinate (Point) at (1,2);

\begin{axis}[
	% tell pgfplots to "grab" the axis at its 
	% internal (0,0) coord:
    anchor=origin,      
	% tell pgfplots to place its anchor at (0,0):
	% (This is actually the default and can
	% be omitted)
    at={(0pt,0pt)},
	% tell pgfplots to use the "natural" dimensions:
    disabledatascaling,
	% tell pgfplots to use the same unit vectors
	% as tikz:
    x=1cm,y=1cm,
    %
    % this is just as usual in pgfplots. I guess 
	% it is only useful if (0,0) is part of the 
	% range... try it out.
    xmin=-1,xmax=3, ymin=-1,ymax=3,grid=both] 
% this uses the point defined OUTSIDE of the axis
\draw [blue,fill] (Point) circle (2pt) 
	node [right] {(1,2)};

% this uses a TIKZ coordinate (2,0) in the axis:
\draw [blue,fill] (2,0) circle (2pt) 
	node [right] {(2,0)};

% this here will always work inside of an axis:
\draw [blue,fill] (axis cs:-1,0) circle (2pt) 
	node [right] {(-1,0)};
\end{axis}
\end{tikzpicture}
\end{codeexample}
	\noindent The example demonstrates several things: first, it defines a coordinate in the enclosing |tikzpicture| and uses it inside of the |axis| (at the correct position). Second, it uses the standard \Tikz\ coordinate |(2,0)| inside of the |axis|, and it is placed at the expected position. Third, it uses the approach provided by \PGFPlots\ by using the |axis cs| to designate a coordinate (this last approach does also work without the coordinate matching).

Here is an example which inserts a \PGFPlots\ graphics correctly into a |tikzpicture|:
% \usepgfplotslibrary{patchplots}
\begin{codeexample}[]
% requires \usepgfplotslibrary{patchplots}
\begin{tikzpicture}
\begin{axis}[
	% tell pgfplots to "grab" the axis at its internal (0,0) coord:
    anchor=origin,      
	% tell pgfplots to place its anchor at (0,0):
	% (This is actually the default and can be omitted)
    at={(0pt,0pt)},
	% tell pgfplots to use the "natural" dimensions:
    disabledatascaling,
	% tell pgfplots to use the same unit vectors as tikz:
    x=1cm,y=1cm,
	%
	hide axis,
]
\addplot[patch,patch type=coons,
	shader=interp,point meta=explicit] 
coordinates {
	(0,0)   [0] % first corner
	(1,-1)  [0] % bezier control point between (0) and (3)
	(4,0.7) [0] % bezier control point between (0) and (3)
	%
	(3,2)   [1] % second corner
	(4,3.5) [1] % bezier control point between (3) and (6)
	(7,2)   [1] % bezier control point between (3) and (6)
	%
	(7,1)      [2] % third corner
	(6,0.6)    [2] % bezier control point between (6) and (9)
	(4.5,-0.5) [2] % bezier control point between (6) and (9)
	%
	(5,-2)   [3] % fourth corner
	(4,-2.5) [3] % bezier control point between (9) and (0)
	(-1,-2)  [3] % bezier control point between (9) and (0)
};
\end{axis}

% this requires pgf 2.10 
\begin{scope}[every node/.style={circle,inner sep=2pt,fill=black}]
\node[pin=140:first] at (0,0) {};
\node[pin=second]    at (3,2) {};
\node[pin=45:third]  at (7,1) {};
\node[pin=0:fourth]  at (5,-2) {};
\end{scope}
\end{tikzpicture}
\end{codeexample}
\noindent The example employs one of the |patch| plots of the |patchplots| library. Since these graphical elements typically require depth information (|z buffer|ing) and color data (|point meta|), they are only available inside of \PGFPlots. However, the configuration above ensures that coordinates match one-to-one between \PGFPlots\ and \Tikz. The |hide axis| flag disables anything of \PGFPlots, so only the visualized |patch| plot remains\footnote{Note that the $(0,0,0)$ coordinate of \PGFPlots\ is part of the data range here.}.