[Tex/LaTex] Pgfplots: Scope inside axis environment or other relative positioning

external filesgraphicspgfplotsrelativetikz-pgf

I want to place an external figure inside a pgfplots plot. Additionally, I want to put some labels inside the external figure to mark the position.

The expected result should look something like this:

enter image description here

In my idea, the figure is placed relative to the axis coordinate system and the point and label positions are set relative to a scope or another local coordinate system of the external figure.

However, I am struggling with the relative positioning of the point and label inside the external figure.

Can yomeone give me some hints what I am doing wrong?

Here are my attempts and the problem I do have with them.


Attempt 1

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning}
\usepackage{pgfplotstable}

\begin{document}

% Attempt1:
\begin{tikzpicture}
  \begin{axis}[
    width=8cm,
    height=5cm,
  ]
    \addplot {exp(x)};
    \node[below right] (image) at (rel axis cs:0.05,0.95) {\includegraphics[width=2cm]{example-image-a}};

    \draw [fill=blue] (rel axis cs:0.1,0.85) circle (0.05cm) node (p1) {};
    \node (p1label) [above=of p1]{1};
  \end{axis}
\end{tikzpicture}
\end{document}

Result:

enter image description here

Problems:

  • Label is not visible
  • No positioning of the point and label relative to the figure

Attempt2

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning}
\usepackage{pgfplotstable}

\begin{document}

% Attempt2:
\begin{tikzpicture}
  \begin{axis}[
    width=8cm,
    height=5cm,
  ]
    \addplot {exp(x)};
  \end{axis}

  \node[below right] (image) at (rel axis cs:0.125,1.05) {\includegraphics[width=2cm]{example-image-a}};

  \begin{scope}[
    x={(image.south east)},
    y={(image.north west)},
  ]
    \draw [fill=blue] (0.0,0.85) circle (0.05cm) node (p1) {};
    \node (p1label) [above=0cm of p1.center]{1};
  \end{scope}
\end{tikzpicture}
\end{document}

Result:

enter image description here

Problems:

  • Relative positioning of figure messy

Attempt3

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning}
\usepackage{pgfplotstable}

\begin{document}

% Attempt3:
\begin{tikzpicture}[%
%   
]
  \begin{scope}[local bounding box=scope1]
    \begin{axis}[
      width=8cm,
      height=5cm,
    ]
      \addplot {exp(x)};
    \end{axis}
  \end{scope}
  \begin{scope}[
    x={(scope1.south east)},
    y={(scope1.north west)},
    local bounding box=scope2,
  ]
    \node[below right] (image) at (0.15,0.95) {\includegraphics[width=2cm]{example-image-a}};
  \end{scope}
  \begin{scope}[
    x={(scope2.south east)},
    y={(scope2.north west)},
    local bounding box=scope3,
  ]
    \draw [fill=blue] (0,0) circle (0.05cm) node (p1) {};
    \node (p1label) [above=0cm of p1.center]{1};
  \end{scope}
\end{tikzpicture}
\end{document}

Result:

enter image description here

Problems:

  • Complicated
  • Relative call to scopes seems to not be working after first scope

Edit

This question has a follow-up in this thread where the initial idea of working with a scope is realized thanks to @esdd.

Best Answer

Using the let syntax you can calculate the width of the image, and use these to get a position relative to one of the corners of the image. Note I set the inner sep of the image node to zero, otherwise the calculations are wrong.

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\begin{document}

% Attempt1:
\begin{tikzpicture}
  \begin{axis}[
    width=8cm,
    height=5cm,
  ]
    \addplot {exp(x)};
    \node[below right,inner sep=0pt] (image) at (rel axis cs:0.05,0.95) {\includegraphics[width=2cm]{example-image-a}}; % note inner sep=0pt
    \draw 
      let
      \p1=(image.north west), \p2=(image.south east),
      \n1={veclen(\x2-\x1,0)}, % width of image
      \n2={veclen(0,\y1-\y2)}  % height of image
      in
     [fill=blue] (image.north west) ++(0.1*\n1,-0.3*\n2) circle (0.05cm) node [above] {1};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Old answer

Will something like this work?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning}
\usepackage{pgfplotstable}

\begin{document}

% Attempt1:
\begin{tikzpicture}
  \begin{axis}[
    width=8cm,
    height=5cm,
  ]
    \addplot {exp(x)};
    \node[below right] (image) at (rel axis cs:0.05,0.95) {\includegraphics[width=2cm]{example-image-a}};

    \draw [fill=blue] (image.north west) ++(0.3cm,-0.6cm) circle (0.05cm) node [above] {1};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here