4

I just started using matlab2tikz for the graphics in my thesis and got stuck with a probably simple problem: I have a plot of a function and an inset (zoom-in) on it. Both the main plot and the inset are aligned to the origin, so that the axis overlap. I need a way to define the position of the inset manually. Here is the code for the inset:

\begin{axis}[%
width=3cm,
height=2.1cm,
scale only axis,
xmin=-0.6235,
xmax=-0.6186,
xtick={-0.623, -0.621, -0.619},
ymin=0.1651,
ymax=0.1655,
ytick={0.1651, 0.1653, 0.1655},
]
\addplot [color=blue,solid,forget plot]
table[row sep=crcr]{
-0.6236 0.165085    \\
-0.6232 0.165117    \\
-0.6228 0.165149    \\
-0.6224 0.165181    \\
-0.622  0.165212    \\
-0.6216 0.165244    \\
-0.6212 0.165276    \\
-0.6208 0.165308    \\
-0.6204 0.16534 \\
-0.62   0.165372    \\
-0.6196 0.165404    \\
-0.6192 0.165436    \\
-0.6188 0.165468    \\
-0.6184 0.1655  \\
};
\end{axis}

I tried to add something like:

pos={at={(1.5,0.6)}, anchor=north west}

but it does not work. Can't find any solution in the pgf-manual, so hope someone can help.

  • Welcome to the site! It sounds like you might want axis cs – cmhughes Mar 13 '14 at 20:57
  • You need to look at pgfplots manual not at pgfmanual – Ignasi Mar 14 '14 at 8:52
6

One option: the inset is saved in a box and then placed at the desired position using a \node and the axis coordinate system for the main plot (See Section 4.17.1 Accessing Axis Coordinates in Graphical Elements):

\documentclass{article}
\usepackage{pgfplots}

\newsavebox\mybox

\begin{document}

% the box is filled with the inset
\savebox\mybox{%
\begin{tikzpicture}
\begin{axis}[%
width=3cm,
height=2.1cm,
scale only axis,
xmin=-0.6235,
xmax=-0.6186,
xtick={-0.623, -0.621, -0.619},
ymin=0.1651,
ymax=0.1655,
ytick={0.1651, 0.1653, 0.1655},
]
\addplot [color=blue,solid,forget plot]
table[row sep=crcr] {
-0.6236 0.165085    \\
-0.6232 0.165117    \\
-0.6228 0.165149    \\
-0.6224 0.165181    \\
-0.622  0.165212    \\
-0.6216 0.165244    \\
-0.6212 0.165276    \\
-0.6208 0.165308    \\
-0.6204 0.16534 \\
-0.62   0.165372    \\
-0.6196 0.165404    \\
-0.6192 0.165436    \\
-0.6188 0.165468    \\
-0.6184 0.1655  \\
};
\end{axis}%
\end{tikzpicture}
}

% the main plot; a \node is used to place the inset
\begin{tikzpicture}
\begin{axis}[%
width=9cm,
height=7cm,
scale only axis,
]
\addplot [color=red,forget plot]
coordinates { (0,0) (1,3) (2,1) (3,-1) (4,3) (5,5) (6,6) (10,10)};
\node at (axis cs:8,2) {\usebox\mybox};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Is savebox a pgf specific command or standard LaTeX? Great solution! – Dr. Manuel Kuehner Mar 27 '16 at 1:23
3

this can be done without saveboxes, too. Just put the axis in the same tikzpicture and shift it:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
width=9cm,
height=7cm,
scale only axis,
]
\addplot [color=red,forget plot]
coordinates { (0,0) (1,3) (2,1) (3,-1) (4,3) (5,5) (6,6) (10,10)};
\end{axis}
\begin{axis}[%
width=3cm,
height=2.1cm,
scale only axis,
xmin=-0.6235,
xmax=-0.6186,
xtick={-0.623, -0.621, -0.619},
ymin=0.1651,
ymax=0.1655,
ytick={0.1651, 0.1653, 0.1655},
xshift=5.25cm,yshift=0.75cm % <-
]
\addplot [color=blue,solid,forget plot]
table[row sep=crcr] {
    -0.6236 0.165085    \\
    -0.6232 0.165117    \\
    -0.6228 0.165149    \\
    -0.6224 0.165181    \\
    -0.622  0.165212    \\
    -0.6216 0.165244    \\
    -0.6212 0.165276    \\
    -0.6208 0.165308    \\
    -0.6204 0.16534 \\
    -0.62   0.165372    \\
    -0.6196 0.165404    \\
    -0.6192 0.165436    \\
    -0.6188 0.165468    \\
    -0.6184 0.1655  \\
};
\end{axis}%
\end{tikzpicture}
\end{document}

xshift=5.25cm,yshift=0.75cm is the key to success here.

result

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.