是否可以将较小的图嵌入到组图的其中一个图中?我想要实现的效果就像下图的放大区域,它应该显示两个矩形,一个较小的矩形表示放大的区域,另一个稍大一些,放大的区域可见。我试过使用\usetikzlibrary{spy}
,但它只是简单地缩放一个区域。我希望控制放大区域的 ytick 参数,以便“拉伸”重叠的潜在曲线。我想要获得的图形结构如下:
这里有一个虚拟的 MWE 来尝试一个可能的解决方案:
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgf,pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}
%\usetikzlibrary{spy}
\begin{filecontents*}{data1.dat}
t1 a1 t2 a2 t3 a3
0.142 1.000 0.154 1.000 0.139 0.995
0.284 1.000 0.308 1.000 0.279 0.990
0.426 1.000 0.461 1.000 0.418 0.989
0.569 1.000 0.615 1.000 0.558 0.989
0.711 1.000 0.769 1.000 0.697 0.989
0.853 1.000 0.923 1.000 0.836 0.989
0.995 1.000 1.077 1.000 0.976 0.989
1.137 1.000 1.230 1.000 1.115 0.989
1.279 1.000 1.384 1.000 1.255 0.989
1.422 0.995 1.538 1.000 1.394 0.986
1.564 0.990 1.692 1.000 1.534 0.981
\end{filecontents*}
\begin{filecontents*}{data2.dat}
t1 a1 t4 a4 t3 a3
3.554 0.920 3.845 0.950 3.485 0.913
3.696 0.915 3.999 0.945 3.625 0.908
3.838 0.910 4.153 0.939 3.764 0.903
3.980 0.905 4.306 0.934 3.904 0.898
4.123 0.900 4.460 0.929 4.043 0.893
4.265 0.895 4.614 0.923 4.182 0.888
4.407 0.890 4.768 0.918 4.322 0.883
4.549 0.885 4.922 0.912 4.461 0.878
4.691 0.880 5.075 0.907 4.601 0.873
4.833 0.875 5.229 0.902 4.740 0.868
4.976 0.870 5.383 0.896 4.880 0.864
5.118 0.865 5.537 0.891 5.019 0.859
5.260 0.860 5.691 0.885 5.158 0.854
\end{filecontents*}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.9]%,spy using outlines={rectangle, magnification=1.5}]
\begin{groupplot}[group style={
group name=rzrxcomp,
group size=1 by 2,
xlabels at=edge bottom,
ylabels at=edge left,
vertical sep=4em
},
xmin = 0, xmax = 2,
ymin = 0.8, ymax = 1.1,
xtick distance = 0.2,
ytick distance = 0.2,
xlabel={$time\;[s]$},
ylabel={$mass$},
grid = both,
grid style = {dotted},
minor tick num = 1,
major grid style = {lightgray!75},
minor grid style = {lightgray!75},
width = 0.85\textwidth,
height = 0.50\textwidth,
legend style={at={(0.5,-0.3)},
anchor=north,legend columns=-1},
legend cell align = {left},
]
\nextgroupplot
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data1.dat};
\addplot[smooth, Lavender, very thick] table [x = {t2}, y = {a2}] {data1.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data1.dat};
\nextgroupplot[xmin = 3, xmax = 6,
ymin = 0.7, ymax = 1,
ytick distance = 0.03, legend entries={n1,n2,n3,n4}]
\addlegendimage{black, very thick, no markers}
\addlegendimage{Lavender, very thick, no markers}
\addlegendimage{Violet, very thick, no markers}
\addlegendimage{BurntOrange, dotted, very thick, no markers}
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat};
\addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};
%\coordinate (spypoint) at (axis cs:xx,yy);
%\coordinate (spyviewer) at (axis cs:xx,yy);
%\spy[width=3cm,height=2cm] on (spypoint) in node [fill=white] at (spyviewer);
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}
我不知道是否spy
可以控制 spyviewer 的 ytick,所以我想到了嵌套的 groupplots。无论如何,如果它有帮助,您可以在 MWE 中找到相关%
部分的注释spy
。
答案1
您可以axis
在之后添加一个新环境\end{groupplot}
,并在其中绘制相同的内容。在这里,我使用calc
库来定位这个新轴。(它不是使用 明确加载的,\usetikzlibrary{calc}
但我想它是由其他库之一加载的。)
对于此axis
环境,请根据需要设置轴限制、刻度选项等。
当然,这样所有内容都会被绘制两次。对于这些数据集来说这不是什么大问题,但对于大型数据集来说可能会很明显。
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplotstable} % loads pgfplots which loads pgf
\pgfplotsset{compat=newest}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}
\begin{filecontents*}{data1.dat}
t1 a1 t2 a2 t3 a3
0.142 1.000 0.154 1.000 0.139 0.995
0.284 1.000 0.308 1.000 0.279 0.990
0.426 1.000 0.461 1.000 0.418 0.989
0.569 1.000 0.615 1.000 0.558 0.989
0.711 1.000 0.769 1.000 0.697 0.989
0.853 1.000 0.923 1.000 0.836 0.989
0.995 1.000 1.077 1.000 0.976 0.989
1.137 1.000 1.230 1.000 1.115 0.989
1.279 1.000 1.384 1.000 1.255 0.989
1.422 0.995 1.538 1.000 1.394 0.986
1.564 0.990 1.692 1.000 1.534 0.981
\end{filecontents*}
\begin{filecontents*}{data2.dat}
t1 a1 t4 a4 t3 a3
3.554 0.920 3.845 0.950 3.485 0.913
3.696 0.915 3.999 0.945 3.625 0.908
3.838 0.910 4.153 0.939 3.764 0.903
3.980 0.905 4.306 0.934 3.904 0.898
4.123 0.900 4.460 0.929 4.043 0.893
4.265 0.895 4.614 0.923 4.182 0.888
4.407 0.890 4.768 0.918 4.322 0.883
4.549 0.885 4.922 0.912 4.461 0.878
4.691 0.880 5.075 0.907 4.601 0.873
4.833 0.875 5.229 0.902 4.740 0.868
4.976 0.870 5.383 0.896 4.880 0.864
5.118 0.865 5.537 0.891 5.019 0.859
5.260 0.860 5.691 0.885 5.158 0.854
\end{filecontents*}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.9]%,spy using outlines={rectangle, magnification=1.5}]
\begin{groupplot}[group style={
group name=rzrxcomp,
group size=1 by 2,
xlabels at=edge bottom,
ylabels at=edge left,
vertical sep=4em
},
xmin = 0, xmax = 2,
ymin = 0.8, ymax = 1.1,
xtick distance = 0.2,
ytick distance = 0.2,
xlabel={$time\;[s]$},
ylabel={$mass$},
grid = both,
grid style = {dotted},
minor tick num = 1,
major grid style = {lightgray!75},
minor grid style = {lightgray!75},
width = 0.85\textwidth,
height = 0.50\textwidth,
legend style={at={(0.5,-0.3)},
anchor=north,legend columns=-1},
legend cell align = {left},
]
\nextgroupplot
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data1.dat};
\addplot[smooth, Lavender, very thick] table [x = {t2}, y = {a2}] {data1.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data1.dat};
\nextgroupplot[xmin = 3, xmax = 6,
ymin = 0.7, ymax = 1,
ytick distance = 0.03, legend entries={n1,n2,n3,n4},
]
\addlegendimage{black, very thick, no markers}
\addlegendimage{Lavender, very thick, no markers}
\addlegendimage{Violet, very thick, no markers}
\addlegendimage{BurntOrange, dotted, very thick, no markers}
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat};
\addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};
\end{groupplot}
\begin{axis}[
footnotesize, % simple way of reducing fontsizes etc. a bit
xmin = 3.5, xmax = 4.5,
ymin = 0.9, ymax = 1,
ytick distance = 0.02,
at={($(rzrxcomp c1r2.north east)+(-2mm,-2mm)$)}, % position
anchor=north east, % anchor
scale only axis,
width=2cm
]
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat};
\addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}