
我想將插入軸放置在父軸內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
?
或者,是否可以透過向 提供兩個座標來指定軸尺寸\begin{axis}[..]
?
答案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
必須為插入軸提供鍵,以便該軸與所需尺寸完全匹配。
非常歡迎評論/替代答案!