从我的开始老问题我修改了一些参数,
我一直不明白原因,因为间谍的中心不在那里,$(4,3.5)$
但它更接近该点$(3.2,3)$
(这个值是我在不同的编译后给出的)。
\spy[green!70!black,size=2cm] on (3.2,3) in node [fill=white] at (8,1);
接下来是主要问题:
是否可以使用 TikZ 库,\usetikzlibrary{spy}
将一系列不同的矩形缩放到同一方向的此图像中(取自 JAMES STEWART 第 7 版的 CALCULUS 一书),
哪里可以有缩放标签问题(用户@Tobi 的回答非常棒)?
在这里我添加了我的 MWE 和相关屏幕截图。
\documentclass{article}
\usepackage{tikz,amsmath,xcolor}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\usetikzlibrary{spy}
\begin{document}
\begin{tikzpicture}[spy using outlines={circle=.5cm, magnification=3, size=.5cm, connect spies}]
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt
}
\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}
\makeatother
\begin{axis}[
xmin=-4,xmax=4,
xlabel={},
ymin=-3,ymax=3,
axis on top,
legend style={legend cell align=right,legend plot pos=right}]
\begin{scope}
\spy[green!70!black,size=2cm] on (3.2,3) in node [fill=white] at (8,1);
\end{scope}
\addplot[color=gray,domain=-4:4,samples=100] {x^3};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您链接到的答案似乎表明它们重复绘图、缩放和裁剪。pfgfplots
这最终可能会导致性能问题(尽管不是在如此简单的图形中),因此可能需要使用\savebox
es 和path picture
s 来得出
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\newsavebox\Axis
\begin{document}
\savebox\Axis{
\begin{tikzpicture}[declare function={f(\x)=pow(\x-1.5,3)-\x+3.5;}]
\begin{axis}[
xmin=-1,xmax=4,xlabel={$x$},xtick=\empty,
ymin=-1,ymax=4,ylabel={$y$},ytick=\empty,
axis lines=middle]
\addplot[color=cyan,thick,domain=-4:4,samples=51,smooth] {f(x)};
\node[circle,fill,inner sep=1.5pt,cyan] at (2.2,{f(2.2)}){};
\end{axis}
\end{tikzpicture}}
\begin{tikzpicture}
\node{\usebox\Axis};
\node[draw,minimum size=0.5cm,purple] (s1) at (1,0.2){};
\node[draw,minimum size=1cm,purple,
path picture={\path (-1*2,-0.2*2) node[scale=2]{\usebox\Axis};}] (s1')
at (2,0.2){};
\node[draw,minimum size=0.5cm,purple] (s2) at (2,0.2){};
\node[draw,minimum size=1cm,purple,
path picture={\path (-1*3,-0.2*3) node[scale=3]{\usebox\Axis};}] (s2')
at (3.5,0.2){};
\foreach \X in {1,2}
{\draw[dashed,purple] (s\X.north west) -- (s\X'.north west)
(s\X.south west) -- (s\X'.south west);}
\end{tikzpicture}
\end{document}
请注意,这可以变得更加用户友好。但是,由于我不得不或多或少完全放弃您的代码,以便获得一些让我想起我认为的目标输出的东西,因此我将其添加为版本 0.1。
嗯,似乎不是你想要的,但是也不太容易看到你想要的东西。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\newsavebox\Axis
\begin{document}
\savebox\Axis{
\begin{tikzpicture}[declare function={f(\x)=pow(\x-1.5,3)-\x+3.5;}]
\begin{axis}[
xmin=-1,xmax=4,xlabel={$x$},xtick=\empty,
ymin=-1,ymax=4,ylabel={$y$},ytick=\empty,
axis lines=middle]
\addplot[color=cyan,thick,domain=-4:4,samples=51,smooth] {f(x)};
\path (0,0) coordinate (O) (2.2,{f(2.2)}) node[circle,fill,inner
sep=1.5pt,cyan] (a){};
\end{axis}
\end{tikzpicture}}
\begin{tikzpicture}[spy node/.style={draw=purple,minimum size=#1*0.5cm,
path picture={{\path (-#1*\myx,-#1*\myy) node[scale=#1]{\usebox\Axis};}}}]
\node[inner sep=0pt,outer sep=0pt]{\usebox\Axis};
\path let \p1=($(a.center)-(current axis.center)$) in \pgfextra{\xdef\myx{\x1}\xdef\myy{\y1}}
(\p1) coordinate (a');
\node[draw,minimum size=0.5cm,purple] (s1) at (a'){};
\node[spy node=2] (s1') at ($(a')+(1,0)$){};
\node[spy node=4] (s4') at ($(a')+(3,0)$){};
\end{tikzpicture}
\end{document}