4

This could be a second part to Positioning pgfplots axis in tikzpicture , I guess ; consider the following MWE:

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{positioning}

\pagecolor{yellow!15}
\begin{document}
\begin{tikzpicture}

\tikzstyle{nn} = [rectangle,draw,minimum width=4cm,minimum height=2cm,line width=1pt,inner sep=0pt]
\tikzstyle{ni} = [rectangle,draw,minimum width=2cm,minimum height=2cm,draw=red]

\node[nn,anchor=south west] (nodeOne) at (0,0) {One};
\node[nn,rotate=90] (nodeTwo) [right=6cm of nodeOne] {\emph{Two}} ;

\node[ni]
  (nodeIndicator) [above right=0cm and 1.5cm of nodeOne] {} ;

\begin{axis}[%
%   at={(nodeOne)}, % passes
%   at={above right=0cm and 1.5cm of nodeOne}, % tikz Error: Cannot parse this coordinate.
  above right=0cm and 1.5cm of nodeOne, % passes, but wrong?
  inner sep=0pt,
  width=4cm,
  height=3cm,
]
\addplot coordinates { (0,0) (1,1) } ;
\end{axis}

% graphical rulers in tikz - via grid:

% x ruler:
\draw[red] (0,0) grid[step=1cm] ({current bounding box.east|-(0cm,0.5cm);});
% y ruler:
\draw[red] (0,0) grid[step=1cm] ({current bounding box.north-|(0.5cm,0cm);});


\end{tikzpicture}
\end{document}

It produces the following output:

test01.png

It can be seen that the syntax above right=0cm and 1.5cm of nodeOne, due to the positioning library, works as expected in the case of nodeIndicator.

However, if I use the same construct in the {axis} environment, it is completely ignored! So my question is - how can I use positioning library syntax with the pgfplots {axis} environment?

Second - I just noticed this on this MWE - you can see that even if I specify width=4cm,height=3cm for the {axis} environment, the rendered plot is much smaller (that's why the rulers are added). What sort of size should the width and height refer to in the case of {axis} environment? (It's not the inner plot; neither it is inner plot+axes tick labels ... also related here: How to properly scale a TikZ/PGF picture which has a `\begin{axis}...\end{axis}`)

4
  • 1
    About the size issue: By default, PGFPlots leaves space for the axis labels, that's why the plot area is smaller than the specified width and height. If you don't want that (because you don't have labels, for instance), use scale only axis.
    – Jake
    Jun 25, 2014 at 17:44
  • 1
    1cm gets translated at 28.3pt which is the same as 28.3 to pgfmath. So it could just be off the page. Jun 26, 2014 at 4:11
  • 1
    Anyway, in tex.stackexchange.com/questions/172814/… I would name coordinates before \end{axis} and use them after. Jun 26, 2014 at 4:19
  • 2
    Here is some insight regarding the "why": it seems that the positioning library is plain incompatible with the placement of pgfplots axes (a pgfplots axis is a PGF node, not a TikZ node -- and the positioning library affects only TikZ nodes). Please refer to the answer of @ Torbjørn for a suitable replacement. Jun 21, 2015 at 18:12

1 Answer 1

2

I do not know why that syntax doesn't work, but you can use the syntax of the calc library. While not exactly the same, it does allow you to place axes relative to other nodes/coordinates. Note that the default anchor for the axis is south west, change that if desirable.

In the code below I used at={($(nodeOne)+(0cm,1.5cm)$)}. When no anchor for the node is supplied (e.g. nodeOne.west), the center anchor is assumed. As you can see, the lower left corner (anchor=south west) of the axis is 1.5cm above the center of nodeOne.

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{positioning,calc}

\pagecolor{yellow!15}
\begin{document}
\begin{tikzpicture}

\tikzset{
nn/.style={rectangle,draw,minimum width=4cm,minimum height=2cm,line width=1pt,inner sep=0pt},
ni/.style={rectangle,draw,minimum width=2cm,minimum height=2cm,draw=red}}

\node[nn,anchor=south west] (nodeOne) at (0,0) {One};
\node[nn,rotate=90] (nodeTwo) [right=6cm of nodeOne] {\emph{Two}} ;

\node[ni]
  (nodeIndicator) [above right=0cm and 1.5cm of nodeOne] {} ;

\begin{axis}[%
  at={($(nodeOne)+(0cm,1.5cm)$)},
  anchor=south west,
  inner sep=0pt,
  width=4cm,
  height=3cm,
  scale only axis
]
\addplot coordinates { (0,0) (1,1) } ;
\end{axis}

% graphical rulers in tikz - via grid:

% x ruler:
\draw[red] (0,0) grid[step=1cm] ({current bounding box.east|-(0cm,0.5cm);});
% y ruler:
\draw[red] (0,0) grid[step=1cm] ({current bounding box.north-|(0.5cm,0cm);});


\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .