# Overlaying or nesting graphs in tikzpicture with different axis scales

I need overlay or nest a figure in a graph. I try with \pic, but the results aren't good for figure scale, according to graph scale.

I try also to define the figure in nodes, to place it in the graph, but I can't.

\documentclass[11pt]{report}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
\usepackage{calc}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\tikzset{m2/.pic={
\draw (0,0)--(0,.4)--(1.575,.4)--(1.575,7.8)--(0,7.8)--(0,8.2)--(3.4,8.2)--(3.4,7.8)--(1.825,7.8)--(1.825,.4)--(3.4,.4)--(3.4,0)--cycle;
}}

\begin{figure}[!hbp]
\centering
\begin{minipage}[c]{0.4\linewidth}
\centering
\begin{tikzpicture}
\begin{axis}[ymin=0,xmin=-4000,x=.0035mm,y=1cm, axis x line=bottom,axis y line=left,clip=false]
\end{axis}
\end{tikzpicture}
\end{minipage}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{minipage}[c]{0.4\linewidth}
\centering
\begin{tikzpicture}
\begin{axis}[ymin=0,xmax=5000,x=.0035mm,y=1cm, axis x line=bottom,hide y axis,clip=false]
\node at (axis cs: 2610,0.5) [font=\scriptsize, right] {$\sigma 2$};
\pic [scale=50] at (500,400) {m2};
\end{axis}
\end{tikzpicture}
\end{minipage}
\end{figure}

\end{document}

Another trick is to define coordinates inside the axis environment then use them later. Note: One can also use relative coordinates instead of xshift and yshift.

\documentclass[11pt]{report}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
\usepackage{calc}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[!hbp]
\centering
\begin{minipage}[c]{0.4\linewidth}
\centering
\begin{tikzpicture}
\begin{axis}[ymin=0,xmin=-4000,x=.0035mm,y=1cm, axis x line=bottom,axis y line=left,clip=false]
\end{axis}
\end{tikzpicture}
\end{minipage}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{minipage}[c]{0.4\linewidth}
\centering
\begin{tikzpicture}
\begin{axis}[ymin=0,xmax=5000,x=.0035mm,y=1cm, axis x line=bottom,hide y axis,clip=false]
\node at (axis cs: 2610,0.5) [font=\scriptsize, right] {$\sigma 2$};
\coordinate (bar) at (axis cs: 500,4);% save location for error bar
\end{axis}
%draw error bar
\pgfpointanchor{bar}{center}
\pgfgetlastxy{\tempx}{\tempy}
\begin{scope}[xshift=\tempx, yshift=\tempy, scale=0.3]
\draw (0,0)--(0,.4)--(1.575,.4)--(1.575,7.8)--(0,7.8)--(0,8.2)--(3.4,8.2)--(3.4,7.8)--(1.825,7.8)--(1.825,.4)--(3.4,.4)--(3.4,0)--cycle;
\end{scope};
\end{tikzpicture}
\end{minipage}
\end{figure}

\end{document}
• Using \scope works for captions and subcaptions also. – Isai Sep 1 '14 at 15:28

You need to use axis cs: for specifying the coordinates of the point. With axis cs: 500,4 in

\pic [scale=50] at (axis cs: 500,4) {m2};

and x=.006mm (it is too campy), you get

• But, what happen with the scale? I need a uniformly scaled figure inside the graph. – Isai Aug 31 '14 at 18:33
• @Isai It is already scaled. To test remove [scale=50] and see what happens. – user11232 Aug 31 '14 at 23:51
• No it wasn't, I solved the problem with picture. I posted an answer. – Isai Sep 1 '14 at 6:36

Based on egreg answer, here, I used picture to overcome the problem.

The problem with \pic was the scale, the inserted picture was not originally scaled, it fit with the graph scale in x and y. Furthermore, it was difficult to handle minipages using \pic.

The difference between them is depicted in the picture:

\documentclass[11pt]{report}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
\usepackage{calc}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{graphicx,picture,calc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{patterns}

\begin{document}

\begin{figure}[htp]
\begin{minipage}[c]{0.4\linewidth}
\sbox0{
\begin{tikzpicture}[scale=.3]
\draw (0,0)--(0,.4)--(1.575,.4)--(1.575,7.8)--(0,7.8)--(0,8.2)--(3.4,8.2)--(3.4,7.8)--(1.825,7.8)--(1.825,.4)--(3.4,.4)--(3.4,0)--cycle;
\end{tikzpicture}
}
\sbox2{
\begin{tikzpicture}
\begin{axis}[ymin=0,xmax=5000,x=.0035mm,y=1cm, axis x line=bottom,hide y axis,clip=false]
\node at (axis cs: 2610,0.5) [font=\scriptsize, right] {$\sigma 2$};
%   \pic [scale=50] at (axis cs: 500,4) {m2};
\end{axis}
\end{tikzpicture}
}
\begin{picture}(\wd0, \ht0 )
\put(0,0){\usebox0}
%% Adjust the -0.7cm and -0.5cm shifts
\put(\wd0 - \wd2 - 0.5cm,\ht0 - \ht2 - 0.1cm){\usebox{2}}

\end{picture}
\end{minipage}
\begin{minipage}[c]{0.4\linewidth}
\sbox0{
\begin{tikzpicture}[scale=.3]
\draw (0,0)--(0,.4)--(1.575,.4)--(1.575,7.8)--(0,7.8)--(0,8.2)--(3.4,8.2)--(3.4,7.8)--(1.825,7.8)--(1.825,.4)--(3.4,.4)--(3.4,0)--cycle;
\end{tikzpicture}
}
\sbox2{
\begin{tikzpicture}
\begin{axis}[ymin=0,xmax=5000,x=.0035mm,y=1cm, axis x line=bottom,hide y axis,clip=false]
\node at (axis cs: 2610,0.5) [font=\scriptsize, right] {$\sigma 2$};
%   \pic [scale=50] at (axis cs: 500,4) {m2};
\end{axis}
\end{tikzpicture}
}
\begin{picture}(\wd0, \ht0 )
\put(0,0){\usebox0}
%% Adjust the -0.7cm and -0.5cm shifts
\put(\wd0 - \wd2 - 0.5cm,\ht0 - \ht2 - 0.1cm){\usebox{2}}

\end{picture}
\end{minipage}
\end{figure}

\end{document}