# How to maintain consistency with TikZ and Pgfplots?

I would like to maintain some consistency between TikZ and PGFplots, and have run into numerous difficulties. Here is an example of two of them:

1. I would like to be able to use something similar to the TikZ \coordinate in PGF. So, ideally I would like to comment out the line in the 2nd graph and replace it with the un-commented line. i.e, be able to use a defined coordinate, rather than having to enter the specific coordinate.

2. I would like to have the two circles of the same size independent of how I am doing the graph. In the 2nd example, I multiplied the \Radius by 20 and still the dot is not even close to the same size.

Perhaps there are better ways to do this, but I am newbie with these...

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\def\Radius{0.1}
\def\Label{$(1,1)$}

\begin{document}
\begin{tikzpicture}
\coordinate (Point) at (1,1);

\draw [gray] (0,0) grid (3,3);
\draw [blue,fill] (Point) circle (\Radius) node [right] {\Label};
\end{tikzpicture}

\begin{tikzpicture}
\coordinate (Point) at (1,1);

\begin{axis}[xmin=0,xmax=3, ymin=0,ymax=3]
\addplot  [blue,fill] coordinates{ (1,1) }
circle (20*\Radius) node [right] {\Label};

% would prefer to use this instead:
%\addplot  [blue,fill] coordinates{ (Point) }
%   circle (20*\Radius) node [right] {\Label};
\end{axis}
\end{tikzpicture}
\end{document}

-
For the second problem, you could use a unit when declaring the radius: \def\Radius{2pt}. –  Gonzalo Medina Apr 13 '11 at 1:41
I used your code with \def\Radius{2pt} and the point shows on both cases with the same radius. –  Gonzalo Medina Apr 13 '11 at 3:08
Ok, I wish I could delete my previous comments.. .Yes, using {2pt} works great. I had forgotten about the 20x factor that I had in the original example.... Thanks... –  Peter Grill Apr 13 '11 at 3:31
I think it would be helpful if you could provide an actual use case. Where do the coordinates come from (a datafile?)? Do you want to be able to refer to any node created outside the axis environment, or only some handpicked ones? Do you want to use them as data points in actual plots, or only to annotate a plot? –  Jake Apr 14 '11 at 8:26
@Peter: Have a look at my answer to Use macro as coordinate in pgfplots plot which should be helpful. –  Martin Scharrer Apr 18 '11 at 22:25

## 2 Answers

Referring to the original, dimensionless coordinates of a node is hard, because only the absolute coordinates with units are saved. In order to get the dimensionless values, one needs to divide the absolute coordinates of the point by the absolute lengths of the unit vectors.

These unit vectors can change in different scopes (such as inside an axis) because of the applied transformations. So we need to save the unit vectors at the time the original node was created.

The following code does two things: It appends an option to the every coordinate node style that creates an additional \coordinate (Unit <node name>) at (1,1) every time the user calls \coordinate (<node name>) at (<x>,<y>); and it provides a macro that 'imports' such a coordinate/unit vector pair into the axis environment by creating a new coordinate of the same name.

Many, many drawbacks: The original node will be overwritten; saving two values (the unit vectors) in the form of a full fledged node is wasteful, especially since this will create many unnecessary nodes if you define a lot of coordinates with the same unit vectors; the \importcoordinate macro only works inside pgfplots axes due to the axis cs coordinate system.

\documentclass{article}
\usepackage{pgfplots}

\def\Radius{2pt}
\def\Label{$(1,2)$}

\begin{document}

\newcommand{\importcoordinate}[1]{
\pgfpointanchor{Unit #1}{center}
\pgfgetlastxy{\xunit}{\yunit}
\pgfpointanchor{#1}{center}
\pgfgetlastxy{\xpoint}{\ypoint}
\pgfmathsetmacro\x{\xpoint/\xunit}
\pgfmathsetmacro\y{\ypoint/\yunit}
\coordinate (#1) at (axis cs:\x,\y);
}

\begin{tikzpicture}[every coordinate node/.append style={
append after command={node (Unit \tikzlastnode) at (1,1) {}}}
]

\draw [gray] (0,0) grid (3,3);
\coordinate (Point) at (1,2);
\draw [blue,fill] (Point) circle (\Radius) node [right] {\Label};
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=3, ymin=0,ymax=3,grid=both]
\importcoordinate{Point}
\draw [blue,fill] (Point) circle (\Radius) node [right] {\Label};
\end{axis}
\end{tikzpicture}
\end{document}


-
I don't mind adding dimensions, so if that is the standard practice, or makes life simpler, let me know. The above solution works (although seems very scary as I don't fully understand it). I don't need to be able to refer to coordinates across pictures, so moved the option to append every coordinate axis to the second picture and put back the definition of the (Point) and then can refer to (Point). –  Peter Grill Apr 14 '11 at 18:34

I believe what you might want is to set the unit vectors of the pgfplots axis explicitly to the same values as are used by TikZ.

However, there are a couple of keys which need to be set in order to match the pgfplots coordinate handling and the one of tikz:


\documentclass{article}
\usepackage{pgfplots}

\def\Radius{2pt}
\def\Label{$(1,2)$}

\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\coordinate (Point) at (1,2);

\draw [gray] (-1,-1) grid (3,3);
\draw [blue,fill] (Point) circle (\Radius) node [right] {\Label};

\end{tikzpicture}

\begin{tikzpicture}
\coordinate (Point) at (1,2);

\begin{axis}[
anchor=origin,      % tells pgfplots to "grab" the axis at its internal (0,0) coord
disabledatascaling, % tells pgfplots to use the "natural" dimensions
at={(0pt,0pt)},     % tells pgfplots to place its anchor (see above) at (0,0) (This is actually the default and can be omitted).
%
x=1cm,y=1cm,        % tells pgfplots to use the same unit vectors as tikz.
%
% 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 (\Radius) node [right] {\Label};

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

% this here will always work inside of an axis:
\draw [blue,fill] (axis cs:-1,0) circle (\Radius) node [right] {(-1,0)};
\end{axis}

\end{tikzpicture}

\end{document}



Note that I change the axis limits to (-1,-1) to demonstrate that the anchor=origin is usually necessary.

-