我正在尝试在环境中淡出自定义路径axis
。到目前为止,我正在使用该nfold
库创建具有定义偏移量的新路径,然后填充两个相邻路径之间的区域。
以下是 MWE:
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{nfold}
\usetikzlibrary{spy}
\makeatletter
\tikzset{offset/.code=\tikz@addmode{\pgfgetpath\tikz@temp\pgfsetpath\pgfutil@empty\pgfoffsetpath\tikz@temp{#1}},
}
\makeatother
\begin{document}
\begin{tikzpicture}[spy using outlines={circle, magnification=4, size=2cm, connect spies}]
\begin{axis}[
ymode=log,
xmin=0, xmax=200,
ymin=1, ymax=10000,
grid=both,
]
\pgfmathsetmacro{\nsteps}{50}
\pgfmathsetmacro{\depth}{5}
\pgfplotsinvokeforeach {1,...,\nsteps}{
\pgfmathsetlengthmacro{\offsetA}{-\depth/\nsteps*#1}
\pgfmathsetlengthmacro{\offsetB}{-\depth/\nsteps*(#1-1)}
\pgfmathsetmacro{\opa}{#1/\nsteps}
\path[save path=\mypath](52,10000) .. controls (198,207) .. (199,5) -- (159,5) .. controls (159,166) .. (50,5371) -- cycle;
\edef\tmp{
\noexpand\path[use path=\noexpand\mypath, offset=\offsetA, save path=\noexpand\mypathA];
\noexpand\path[use path=\noexpand\mypath, offset=\offsetB, save path=\noexpand\mypathB];
\noexpand\fill[even odd rule, use path=\noexpand\mypathA \noexpand\mypathB, opacity=\opa];}
\tmp
}
\coordinate (spypoint) at (axis cs:52,7000);
\coordinate (magnifyglass) at (axis cs:50,10);
\end{axis}
\spy [blue, size=2.5cm] on (spypoint)
in node[fill=white] at (magnifyglass);
\end{tikzpicture}
\end{document}
答案1
根据查看器和缩放级别,接触区域将呈现伪影。这是由于舍入和抗锯齿造成的。不要让区域接触,只需让它们以适当的不透明度重叠即可。
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{nfold}
\usetikzlibrary{spy}
\makeatletter
\tikzset{offset/.code=\tikz@addmode{\pgfgetpath\tikz@temp\pgfsetpath\pgfutil@empty\pgfoffsetpath\tikz@temp{#1}},
}
\makeatother
\begin{document}
\begin{tikzpicture}[spy using outlines={circle, magnification=4, size=2cm, connect spies}]
\begin{axis}[
ymode=log,
xmin=0, xmax=200,
ymin=1, ymax=10000,
grid=both,
]
\pgfmathsetmacro{\nsteps}{50}
\pgfmathsetmacro{\depth}{5}
\pgfplotsinvokeforeach {\nsteps,...,1}{
\pgfmathsetlengthmacro{\offsetA}{-\depth}
\pgfmathsetlengthmacro{\offsetB}{-\depth/\nsteps*(#1-1)}
\path[save path=\mypath](52,10000) .. controls (198,207) .. (199,5) -- (159,5) .. controls (159,166) .. (50,5371) -- cycle;
\edef\tmp{
\noexpand\path[use path=\noexpand\mypath, offset=\offsetA, save path=\noexpand\mypathA];
\noexpand\path[use path=\noexpand\mypath, offset=\offsetB, save path=\noexpand\mypathB];
\noexpand\fill[even odd rule, use path=\noexpand\mypathA \noexpand\mypathB, opacity=2/\nsteps];}
\tmp
}
\coordinate (spypoint) at (axis cs:52,7000);
\coordinate (magnifyglass) at (axis cs:50,10);
\end{axis}
\spy [blue, size=2.5cm] on (spypoint) in node[fill=white] at (magnifyglass);
\end{tikzpicture}
\end{document}