3

I would like get the 'east' (or 'south east') position (marked '*')of the legend of a named tikz axis plot (name = MyAxis) which has the following layout (legend is positioned right of MyAxis and the x-axis labels can have different length):

+------------+  +--------+
|            |  | legend |
|   MyAxis   |  |        |
|            |  +--------*
+------------+
   l     o
   a     t
   b     h
   e     e
   l     r
         l
         a
         b
         e
         L

I can obtain the width of the MyAxis object using MyAxis.east - MyAxis.west but I cannot find a yet to determine the position MyAxis.legend.east or MyAxis.legend.south east. Is this supported by tikz?

A second question that goes into the same direction is whether I can determine the south position of longest x-axis label of the MyAxis object (marked 'L')?

I would need this information to plot a new node (textbook) underneath that starts at (1) and ends at (2):

+------------+  +--------+
|            |  | legend |
|   MyAxis   |  |        |
|            |  +--------*
+------------+
   l     o
   a     t
   b     h
   e     e
   l     r
         l
         a
         b
         e
         L

1------------------------+
| text                   |
+------------------------2

A minimal working example is as follows

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usetikzlibrary{calc}

\begin{document}
\begin{filecontents}{testdata.csv}
  1,  2,  4,  8
  10, 20, 30, 40
  40, 60, 80, 100
  15, 35, 20, 10
\end{filecontents}

\pgfplotstableread[col sep=comma]{testdata.csv}\mytable

\begin{tikzpicture}
\begin{axis}[
name=MyAxis,
width=2\textwidth,
height=.8\textwidth,
legend pos=outer north east,
ybar = 0.05cm,
bar width = 3pt,
ymajorgrids=true,
xticklabel style={rotate=45,anchor=east},
xticklabels={group A, group B, group C, group extra},
xtick=data,
]
\addplot table[x expr=\coordindex, y index=1]{\mytable};
\addplot table[x expr=\coordindex, y index=2]{\mytable};
\addplot table[x expr=\coordindex, y index=3]{\mytable};
\legend{Threads=1,Threads=2,Threads=4,Threads=8}
\end{axis}
\path let \p1=(MyAxis.west), \p2=(MyAxis.east) in node[draw, below
  right, align=left, text=black, text width=\x2-\x1-10pt, minimum
  width=\x2-\x1]
at ($(MyAxis.south west)-(0,50pt)$) {%
Text};
\end{tikzpicture}
\end{document}

Many thanks in advance!

1 Answers1

3

As your legend is at the very edge of the picture, I would use the current bounding box instead:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usetikzlibrary{calc}

\begin{document}
\begin{filecontents}{testdata.csv}
  1,  2,  4,  8
  10, 20, 30, 40
  40, 60, 80, 100
  15, 35, 20, 10
\end{filecontents}

\pgfplotstableread[col sep=comma]{testdata.csv}\mytable

\begin{tikzpicture}
\begin{axis}[
name=MyAxis,
width=2\textwidth,
height=.8\textwidth,
legend pos=outer north east,
ybar = 0.05cm,
bar width = 3pt,
ymajorgrids=true,
xticklabel style={rotate=45,anchor=east},
xticklabels={group A, group B, group C, group extra},
xtick=data,
]
\addplot table[x expr=\coordindex, y index=1]{\mytable};
\addplot table[x expr=\coordindex, y index=2]{\mytable};
\addplot table[x expr=\coordindex, y index=3]{\mytable};
\legend{Threads=1,Threads=2,Threads=4,Threads=8}
\end{axis}
\path let \p1=(MyAxis.west), \p2=(current bounding box.south east) in node[draw, below
  right, align=left, text=black, text width=\x2-\x1-10pt, minimum
  width=\x2-\x1]
at ($(MyAxis.south west)-(0,50pt)$) {%
Text};
\end{tikzpicture}
\end{document}

enter image description here

  • That seems to work nicely. Is there a way to extract a single coordinate from `MyAxis.south west`? Then I could replace the hardcoded offset in `$(MyAxis.south west)-(0,50pt)$` by `$(MyAxis.south west {x-coordinate}, current bounding box.south east {y-coordinate}$`. – Matthias Möller Dec 14 '21 at 15:52
  • @MatthiasMöller You can shift the coordinate: `at ([yshift=-50pt]MyAxis.south west) {Text};` – samcarter_is_at_topanswers.xyz Dec 14 '21 at 16:58