4

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.

1 Answer 1

3

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

6
  • Well, almost, I do have a similar solution to create the expected result. But ideally, I want to place the point and label in a relative coordinate system of the figure (x and y axis of the figure from 0 to 1 instead of ++(0.3cm,-0.6cm)). Thats why I initially tried a scope environment. But this gives weird results.
    – raedma
    May 31, 2016 at 12:46
  • @krtek Yes of course, sorry. I updated my answer with a possible improvement. May 31, 2016 at 13:00
  • Thanks. Since I have additional labels to put on the external picuture I used the savenumber key from tex.stackexchange.com/questions/27836/… to store the values of \n1 and \n2.
    – raedma
    May 31, 2016 at 15:22
  • Still one question: Is it impossible to use a scope environment to achieve the relative positioning? All of my efforts resulted in weird results.
    – raedma
    May 31, 2016 at 15:34
  • @krtek I don't know, actually. May 31, 2016 at 18:17

You must log in to answer this question.

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