1

i am using pgfplots to plot some two-dimensional data that depend on three parameters. I'm trying to use a box at the top of the plot that contains the fixed parameter (see MWE). I want the box to be centered at the x-axis, but this fails and the box is shifted. How can this be fixed such that the box is centered?

\documentclass[]{standalone}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[sb]{libertine}
\usepackage[T1]{eulervm}
\usepackage{pgfplots}\pgfplotsset{compat=newest} 


\begin{document}
\begin{tikzpicture}[remember picture]
\begin{axis}[
  xlabel=Speed factor $SF$,
  ylabel=Time,
  x dir = reverse,
  legend pos=outer north east,
  xtick = data,
  ]
\addplot table [y=T3, x=SF]{data.dat};
\addlegendentry{PF = 3\phantom{.0}}
\addplot table [y=T25, x=SF]{data.dat};
\addlegendentry{PF = 2.5}
\addplot table [y=T15, x=SF]{data.dat};
\addlegendentry{PF = 1.5}
\end{axis}

\node [draw,fill=white, anchor=center] at (rel axis cs: -0.5,1.05) {$TF=15$};
\end{tikzpicture}

\end{document}

Content of data.dat:

SF T3 T25 T15
1 183 214 341
0.9 197 232 370
0.8 239 282 450
0.7 267 314 504

enter image description here

2 Answers 2

1

In your code you are using rel axis cs: outside the axis environment which should not work (as expected). But you had the right idea to use a "relative" coordinate system to position the box. I suggest using axis description cs: to position it, because this is independent of xdir value.

For sure, both (rel axis cs: and axis description cs:) are better than the normal axis cs: because this always is dependent on the data you show. I personally only use it if I have to annotate a point in that (shown) coordinate system, but not for "general" annotations.

(I am sure there was already a question about this topic in the near past, but couldn't find it after a quick search.)

% used PGFPlots v1.14
    \begin{filecontents*}{data.dat}
        SF T3 T25 T15
        1 183 214 341
        0.9 197 232 370
        0.8 239 282 450
        0.7 267 314 504
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=Speed factor $SF$,
        ylabel=Time,
        x dir=reverse,
        legend pos=outer north east,
        xtick=data,
        % use this option to allow pure TikZ commands to not be clipped
        % (that is in this example the box is not clipped at the top axis
        %  margin)
        clip mode=individual,
    ]
        \addplot table [y=T3, x=SF] {data.dat};
            \addlegendentry{PF = 3\phantom{.0}}
        \addplot table [y=T25, x=SF] {data.dat};
            \addlegendentry{PF = 2.5}
        \addplot table [y=T15, x=SF] {data.dat};
            \addlegendentry{PF = 1.5}

        % I moved the node *inside* the `axis' environment and replaced
        % the `rel axis cs:' by `axis description cs:'
        \node [draw,fill=white, anchor=north] at
            (axis description cs: 0.5,1.02) {$TF=15$};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

1
  • Works like a charme for me! The fact that the positioning is independent of the data shown is really nice.
    – didda
    Feb 21, 2017 at 17:47
0

The box is centered relative to the entire picture. The code below works.

enter image description here

\documentclass[]{standalone}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[sb]{libertine}
\usepackage[T1]{eulervm}
\usepackage{filecontents}
\usepackage{pgfplots}\pgfplotsset{compat=newest} 


\begin{filecontents}{data.dat}
    SF T3 T25 T15
    1 183 214 341
    0.9 197 232 370
    0.8 239 282 450
    0.7 267 314 504
\end{filecontents}

\begin{document}
    \begin{tikzpicture}[remember picture]
    \begin{axis}[
    xlabel=Speed factor $SF$,
    ylabel=Time,
    x dir = reverse,
    legend pos=outer north east,
    xtick = data,
    ]
    \addplot table [y=T3, x=SF]{data.dat};
    \addlegendentry{PF = 3\phantom{.0}}
    \addplot table [y=T25, x=SF]{data.dat};
    \addlegendentry{PF = 2.5}
    \addplot table [y=T15, x=SF]{data.dat};
    \addlegendentry{PF = 1.5}
        \node [draw,fill=white, anchor=north] at (axis cs:0.85,530) {$TF=15$};
    \end{axis}
    \end{tikzpicture}

\end{document}

You must log in to answer this question.

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