10

I need to draw arrows from text to a pgfplots graph using TikZ. For some reason, the arrow coordinates are not where I want them to be in the pgfplots graph. Here is an example:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{width=7cm}

\begin{document}

Text \tikz[remember picture] \node[circle,fill=red!50] (n1) {}; text.
\vspace{1in}

\pgfplotsset{domain=-1:1}
\begin{tikzpicture}
  \begin{axis}[remember picture,xlabel=A normal sized $x$ label]
    \addplot[smooth,blue,mark=*] {x^2};
    \node (c) at (axis cs:0,0.5) {$a$};
  \end{axis}
\end{tikzpicture}
\vspace{1in}

Text \tikz[remember picture] \node[fill=blue!50] (n2) {}; text.

\begin{tikzpicture}[remember picture,overlay]
  \draw [->,red,very thick] (n1) -- (n2);
  \draw [overlay,->,very thick] (c) -- (n2);
\end{tikzpicture}

\end{document}

Letter 'a' seems to be in the correct place at (0,0.5), but the starting point of the black arrow is not. It seems to be too much to the right.

The red arrow, which is not defined using the pgfplots graph, seems to be correct.

Is there some option, which I have missed?

Thank you for any help!

Gonzalo Medina
  • 505,128
user18778
  • 101
  • 4
  • You better provide the remember picture option to the tikzpicture env. but I can reproduce this. This can be also be seen within the same TikZ picture without overlay or remember picture. Using \draw (c) -- (0,0); results with the same output. – percusse Sep 16 '12 at 16:53

1 Answers1

13

This seems to be a bug in pgfplots, I will take care of it.

Although I do not see the mistake, I can say how to AVOID the problem until the bugfix is ready - provided you are running with pgfplots 1.6.1 (the most recent stable):

adding

\pgfplotsset{cell picture=if necessary}

to your preamble (or \begin{axis}[..., cell picture=if necessary] to selected axes) will fix it.

ATTENTION: in this case, you need to move remember picture from \begin{axis} to \begin{tikzpicture}!

You can also say cell picture=false. In this case you will receive error messages if pgfplots really needs the initial setting (which is cell picture=true as you might have guessed).


Technical explanation:

I see that it is somehow related to the fact that pgfplots creates a so-called "cell-picture" and moves it to implement its positioning choices (the anchors). A cell-picture means that it creates an additional \tikzpicture...\endtikzpicture internally just like a pgf matrix.

The initial value cell picture=true is mainly for backwards compatibility. In general, it has advantages to avoid cell pictures. They are necessary if you choose an anchor which is NOT in the list

north, north west, west, south west, south, south east, east, north east, north, center, origin, above origin, left of origin, right of origin, below origin.

For example, the choice anchor=above north requires a cell picture.


EDIT at July 2016

@ Elmar pointed out that the issue is not resolved completely and he provided the minimal example

%compile with: pdflatex -shell-escape main && make -f main.makefile && pdflatex main
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzset{external/export=false, external/mode=list and make}
\tikzexternalize
\pgfplotsset{compat=1.13}
\begin{document}
    \tikz[remember picture] \node {X};
    \tikzset{external/export=true}
    \begin{tikzpicture}
        \begin{axis}[clip=false]
            \addplot {x};
            \node (A) at (yticklabel cs:0.5) {A};
        \end{axis};
        \node at (A) {B};
    \end{tikzpicture}
\end{document}

and he has a point - there are still issues. In his example, the problem is caused by an entry in the main.aux file which refers to the "remembered position" -- it is accidentally associated with the second image.

I will look into it. For now, a workaround appears to be to not optimize the first picture away as this will result in correct internal picture identifiers when externalizing the graphics:

%compile with: pdflatex -shell-escape main && make -f main.makefile && pdflatex main
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzset{external/export=false, external/mode=list and make}
\tikzexternalize
\pgfplotsset{compat=1.13}
\begin{document}
    {\tikzset{external/optimize=false}%
    \tikz[remember picture] \node {X};
    }%
    \tikzset{external/export=true}
    \begin{tikzpicture}
        \begin{axis}[clip=false]
            \addplot {x};
            \node (A) at (yticklabel cs:0.5) {A};
        \end{axis};
        \node at (A) {B};
    \end{tikzpicture}
\end{document}
  • Thank you for the answer. I have a problem with my Miktex, which has version 1.6.1 of pgfplots. I get an error message "! Package pgfkeys Error: I do not know the key '/pgfplots/cell picture' and I am going to ignore it. Perhaps you misspelled it.". I tried both ways to use the option. What can I do now? – user18778 Oct 19 '12 at 07:17
  • Are you sure that you hae pgfplots 1.6.1 ? The .log file of your test should contain the version number... – Christian Feuersänger Oct 31 '12 at 20:47
  • @Christian, can you give an update on the state of this bug? I have pgfplots 1.7, but have been running into similar problems. I tried the temporary solution you provide, and it seems to do what it's supposed to. But I can't use it because it breaks alignment when used in a \nextgroupplot. – jja Mar 13 '13 at 20:30
  • @jja The bug is unresolved. Do I understand correctly that groupplots and cell picture produce problems? – Christian Feuersänger Mar 13 '13 at 21:01
  • @jja update: I have managed to fix the bug right now. At least for pdftex and dvips. will (probably) become part of the next stable, i.e. of pgfplots 1.8 – Christian Feuersänger Mar 13 '13 at 22:46
  • @Christian, by "the bug", do you mean the one that was dealt with in this question, or the issues with groupplots? If the latter is still unresolved I can upload an image of the faulty output so you can see exactly the kind of problem I mean (but I bet you can reproduce it on your own as well). – jja Mar 14 '13 at 00:32
  • I fixed the original issue, i.e. the case listed in the question. I haven't tried the groupplot issue so far. – Christian Feuersänger Mar 14 '13 at 22:15
  • @Christian, I posted a follow-up question on the issue with groupplots. I can delete it if this is also solved by the bug you fixed. – jja Mar 18 '13 at 20:46
  • @ChristianFeuersänger, I thinkg the original issue is not completely resolved. In the following MWE, I observe a placement-issue similar to the one described above: https://gist.github.com/anonymous/498500af1d80b8390447ce2c679be493 I'm using pgfplots 1.13 and your suggested workaround in the answer still works. – Elmar Peise Jul 19 '16 at 19:59
  • @ElmarPeise I have tested your example and I believe it works as intended (with pgfplots 1.13 and the latest PGF CVS). Can you please verify your software versions and send the .log file to my mail account (as shown in pgfplots.pdf)? – Christian Feuersänger Jul 19 '16 at 20:10
  • @ElmarPeise I have finally confirmed your issue and have edited the answer. – Christian Feuersänger Jul 28 '16 at 18:33