我有一张 Tikz 图片,我想缩放图的一部分,但只在 x 方向上。我尝试使用 进行相同的操作spy
,这会在两个轴上缩放图。
但是,我希望只在 x 方向上进行缩放,这样 x 值介于 0 到 4 之间,y 值与原始图保持一致。这是我用来生成此图像的 MWE 代码
\documentclass{article}
\usepackage{pgfplots}
\usepackage{spy}
\begin{document}
\begin{tikzpicture}[spy using outlines={rectangle, magnification=2.0,connect spies}]
\definecolor{color0}{rgb}{0.5,0,1}
\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmajorgrids,
xmin=-1.13873194890019, xmax=23.913370926904,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymajorgrids,
ymin=-0.188281565159559, ymax=0.820231422036886,
ytick style={color=black}
]
\addplot [semithick, color0]
table {%
0 -0
0.392666189275928 0.146234676241875
0.785332378551855 0.357346773147583
1.17799856782778 0.489268004894257
1.57066475710371 0.54331648349762
1.96333094637964 0.54446005821228
2.35599713565557 0.515433371067047
2.74866332493149 0.471822828054428
3.14132951420742 0.424137800931931
3.53399570348335 0.376261025667191
3.92666189275928 0.331027746200562
4.3193280820352 0.286558628082275
4.71199427131113 0.245650976896286
5.10466046058706 0.209256753325462
5.49732664986299 0.177961841225624
5.88999283913891 0.150137931108475
6.28265902841484 0.127043500542641
6.67532521769077 0.105005994439125
7.0679914069667 0.0844438523054123
7.46065759624262 0.0641868710517883
7.85332378551855 0.0455127842724323
8.24598997479448 0.0290546175092459
8.63865616407041 0.0163740608841181
9.03132235334633 0.00487521616742015
9.42398854262226 -0.00621854094788432
9.81665473189819 -0.0153924785554409
10.2093209211741 -0.0204757731407881
10.60198711045 -0.0223412904888391
10.994653299726 -0.0231655612587929
11.3873194890019 -0.0239122491329908
11.7799856782778 -0.023980425670743
12.1726518675538 -0.0230171401053667
12.5653180568297 -0.0240400228649378
12.9579842461056 -0.026451837271452
13.3506504353815 -0.0317398309707642
13.7433166246575 -0.0386448465287685
14.1359828139334 -0.0461360029876232
14.5286490032093 -0.0533639974892139
14.9213151924852 -0.0605357810854912
15.3139813817612 -0.0656379461288452
15.7066475710371 -0.0704119727015495
16.099313760313 -0.0740573108196259
16.491979949589 -0.0768312588334084
16.8846461388649 -0.0780506506562233
17.2773123281408 -0.0803856551647186
17.6699785174167 -0.0832169875502586
18.0626447066927 -0.0862331837415695
18.4553108959686 -0.0881431698799133
18.8479770852445 -0.0910823196172714
19.2406432745205 -0.0945243760943413
19.6333094637964 -0.0998468399047852
20.0259756530723 -0.105189554393291
20.4186418423482 -0.110977195203304
20.8113080316242 -0.116975866258144
21.2039742209001 -0.124253623187542
21.596640410176 -0.130234345793724
21.9893065994519 -0.136100903153419
22.3819727887279 -0.140050023794174
22.7746389780038 -0.142440065741539
};
\coordinate (spypoint) at (axis cs:1,0.6);
\coordinate (spyviewer) at (axis cs:20,0.36);
\spy[width=1.2cm,height=2.5cm] on (spypoint) in node [fill=white] at (spyviewer);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我不知道如何使用间谍本身来实现这一点,但我们可以通过添加插入来模仿它。我认为您可以自己根据需要修改此解决方案。
% used PGFPlots v1.18.1
% (solution based on <https://tex.stackexchange.com/a/358296/95441>)
\begin{filecontents*}{data.txt}
0 -0
0.392666189275928 0.146234676241875
0.785332378551855 0.357346773147583
1.17799856782778 0.489268004894257
1.57066475710371 0.54331648349762
1.96333094637964 0.54446005821228
2.35599713565557 0.515433371067047
2.74866332493149 0.471822828054428
3.14132951420742 0.424137800931931
3.53399570348335 0.376261025667191
3.92666189275928 0.331027746200562
4.3193280820352 0.286558628082275
4.71199427131113 0.245650976896286
5.10466046058706 0.209256753325462
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this `compat` level or higher so `axis cs:` is also used for TikZ coordinates
\pgfplotsset{compat=1.18}
% declare some layers and their order
% these are needed if you want to have a background (color) behind the whole
% inset plot (including the `ticklabels' etc.) and not only the background
% of the "box" of the inset plot.
% You want to do at least one of these to avoid that also in the inset plot
% the grid lines of the "main" plot are shown/visible
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\begin{document}
\begin{tikzpicture}[
% list all options here that should be used for the main and the inset axis
every axis/.append style={
no markers,
smooth,
},
% declare some variables and store the axis limits of the magnification
% (so we don't need to repeat ourselves)
/pgf/declare function={
InsetXmin=1.0;
InsetXmax=3.0;
InsetYmin=0.5;
InsetYmax=0.65;
},
]
% list all stuff here that should be shown in both axis
\newcommand*{\AxisCommands}{
\addplot table {data.txt};
}
% on the "lowest" layer, draw the normal plot
\begin{pgfonlayer}{background}
\begin{axis}[
xmin=-1,
xmax=25,
ymin=0,
ymax=0.8,
grid=major,
]
% call the above created command
\AxisCommands
% imitate the spy and store a coordinate at the border
\draw (InsetXmin,InsetYmin) -| (InsetXmax,InsetYmax)
coordinate [pos=0.75] (SpyBorder)
-| cycle
;
% store a coordinate where the inset should be plotted at
\coordinate (inset) at (axis description cs:0.9,0.9);
\end{axis}
\end{pgfonlayer}
% the inset plot should be plotted on the "top" most layer
\begin{pgfonlayer}{foreground}
% this is the inset plot ...
\begin{axis}[
% ... which should be plotted at the stored coordinate ...
at={(inset)},
% ... with this `anchor'
anchor=north east,
% use this predefined style (it is predefined by PGFPlots itself)
small,
%
% now state the options which should be used for the inset plot
width=5cm,
height=4cm,
xmin=InsetXmin,
xmax=InsetXmax,
ymin=InsetYmin,
ymax=InsetYmax,
% use this key to fill the background of the axis only
axis background/.style={
fill=white,
},
% name this axis so it can later be used to fill the "background" of the
% whole plot including the labels
name=insetAxis,
]
% of course call the command here too
\AxisCommands
\end{axis}
\end{pgfonlayer}
% on the "medium" layer we draw the background of the whole inset plot
% including the labels
\begin{pgfonlayer}{main}
% (I fill it with a gray here, so one can see the difference to the
% `axis background' result in the inset plot itself)
\fill [black!10]
([shift={(-2pt,-2pt)}] insetAxis.outer south west) |-
([shift={(+5pt,+5pt)}] insetAxis.outer north east)
% store a coordinate at the border here too
coordinate [pos=0.25] (InsetBorder)
|- cycle
;
\end{pgfonlayer}
% draw a connecting line between the spy and the inset
\draw (SpyBorder) -- (InsetBorder);
\end{tikzpicture}
\end{document}