1

I want to place my legend centered above the axis and colorbar, but I can't find the correct bounding box.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\pgfplotstableread{
x y z
5 5 1
6 6 2
7 7 3
8 8 4
}\mytable

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    colorbar,
    colorbar style={ytick={1,2,3,4},ylabel={z}},
    legend style={at={(current bounding box.north)},anchor={south}}
]

\addplot [scatter,scatter src=\thisrow{z},only marks,mark=*] table {\mytable};

\legend{loooooooooooooooooooooooooong entry}

\end{axis}
\end{tikzpicture}

\end{document}
1

How about

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\pgfplotstableread{
x y z
5 5 1
6 6 2
7 7 3
8 8 4
}\mytable

\begin{document}

\begin{tikzpicture}
\begin{scope}[local bounding box=plot]
\begin{axis}[legend to name=named,
    colorbar,
    colorbar style={ytick={1,2,3,4},ylabel={z}},
   % legend style={at={(current bounding box.north)},anchor={south}}
]

\addplot [scatter,scatter src=\thisrow{z},only marks,mark=*] table {\mytable};

\legend{loooooooooooooooooooooooooong entry}

\end{axis}
\end{scope}
\node[anchor=south] at (plot.north) {\ref{named}};
\end{tikzpicture}

\end{document}

enter image description here

  • 1
    Should be use with \ref*{named} if the hyperref package is loaded – Ben Apr 23 at 18:56
  • @ben Yes, good point. (The MWE does not load hyperref. ;-) – marmot Apr 23 at 18:59

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.