
親軸内にインセット軸を配置したいと思いますpgplots
。インセット軸の位置、幅、高さは、親軸のデータ座標を使用して指定する必要があります。これまでのところ、次のようになっています。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=\textwidth,name=mainplot]
\addplot {x};
\coordinate (insetSW) at (axis cs:-4,2); % south west corner of inset
\coordinate (insetNE) at (axis cs:-2,4); % north east corner of inset
\end{axis}
\begin{axis}[at={(insetSW)},anchor=south west,name=inset]
\addplot {x};
\end{axis}
\end{tikzpicture}
\end{document}
インセットの南西の角は正しい位置にあります。インセットの北東の角が になるように、インセットの幅と高さを計算するにはどうすればよいでしょうかinsetNE
?
または、 に 2 つの座標を指定して軸の寸法を指定することは可能ですか\begin{axis}[..]
?
答え1
1つの解決策としては、ステートメント内で指定された座標によって幅と高さを計算することですlet
。計算結果をマクロに保存するインセット軸を作成するときにこのマクロを使用します。以下は MWE です。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfkeys{/tikz/savenumber/.code 2 args={\global\edef#1{#2}}}
\begin{axis}[width=200pt,name=mainplot,xtick={-4,0},ytick={2,6},grid]
\addplot {x};
\coordinate (insetSW) at (axis cs:-4,2); % south west corner of inset
\coordinate (insetNE) at (axis cs:0,6); % north east corner of inset
\path
let
\p1 = (insetSW),
\p2 = (insetNE),
\n1 = {(\x2 - \x1)},
\n2 = {(\y2 - \y1)}
in
[savenumber={\insetwidth}{\n1},savenumber={\insetheight}{\n2}];
\end{axis}
\begin{axis}[
at={(insetSW)}, anchor=south west, name=inset, width=\insetwidth,
height=\insetheight, xticklabels={}, yticklabels={}, scale only axis]
\addplot {x};
\end{axis}
\end{tikzpicture}
% DEBUG
inset width: \insetwidth,
inset height: \insetheight,
\end{document}
それによって
インセットは、左下隅が(-4,2)
、右上隅が で、目的の位置にあります(0,6)
。インセット軸の目盛りラベルは、エラーを防ぐために削除されています。軸が目的の寸法と正確に一致するように、インセット軸にwidth/height is too small...
キーを指定する必要があります。scale only axis
コメント/代替回答は大歓迎です!