我想用 将插入轴放置在父轴内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
欢迎提出评论/其他答案!