3

I would like to know if is possible to access the standard nodes in pgfplots such as ylabel, xlabel, title. I am pretty sure this is quite easy, since each node should have a name by which it can be called, but I don't know how to figure this name out and the name, I would expect them to have, does not exist.

MWE

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[title={Some title}, ylabel={ylabel}, xlabel={xlabel}]
            \addplot+[smooth] coordinates {(0,0) (1,2) (2,3)};
            % Does not work
            \draw (ylabel.center) -> (title.center);
            \draw (ylabel.center) -> (xlabel.west);
        \end{axis}
    \end{tikzpicture}
\end{document}
4

The nodes aren't named by default, but you can assign names yourself using title style={name=title}, for instance. The title and labels are generated at the very end of the axis environment, so if you want to refer to these nodes, you'll have to mode your TikZ commands outside the axis environment.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
          title={Some title}, title style={name=title},
          ylabel={ylabel}, ylabel style={name=ylabel},
          xlabel={xlabel}, xlabel style={name=xlabel}
        ]
        \addplot+[smooth] coordinates {(0,0) (1,2) (2,3)};
        \end{axis}

        \draw (ylabel.center) -> (title.center);
        \draw (ylabel.center) -> (xlabel.west);
    \end{tikzpicture}
\end{document}
| improve this answer | |
  • thanks for the quick answer, too bad that the nodes cannot be accessed inside the axis-environment, not even with after end axis/.code. I would have expected them to be defined earlier. – maetra Jul 30 '13 at 16:05
  • @maetra: Yeah, I was a bit surprised by that as well. What are you trying to do? Maybe there's a different approach? – Jake Jul 30 '13 at 16:06
  • I am trying to draw a line from the left to the right of the whole graph, which I use for a custom legend. The point most far to the left, is determined by the ylabel and for the right side I use (rel axis cs:1,0). Apart from the axis environment (rel axis cs:1,0) is not correct anymore, but shifted to the left. This not a big problem, since I split the drawing in two parts, but for the legend text this is a pain. – maetra Jul 30 '13 at 16:31
  • @maetra: In that case, a better approach would be to use \draw (current axis.outer west) -- (current axis.outer east); outside the axis environment. That approach is more robust, since it will also work in cases where you don't have a ylabel, for example. – Jake Jul 30 '13 at 16:35
  • that is good hint, I'll try that approach – maetra Jul 30 '13 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.