在groupplot
环境中,我需要使用fill between
两条路径,其中一条路径在轴上有标记。我想剪掉这些标记,但这会导致问题fill between
,因为第一条路径被视为空的。
我如何剪辑标记并使用fill between
?
下面给出了一个 MWE。为了清楚地看到问题,您可以打开clip marker paths=true,
和关闭注释行。如果未注释,则问题就在这里。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, pgfplots.groupplots}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 1}, scale only axis,
% clip marker paths=true,
axis on top=true]
% Plot main figure (a)
\nextgroupplot[scale only axis,
xmin=0,
xmax=100,
ymin=0,
ymax=550]
\addplot[name path = pathA, color=blue, mark=square*] coordinates {(0,250)(100,450)};
\path[name path = pathAxis] (axis cs:0,0) -- (axis cs:100,0);
\addplot [gray!30] fill between[of = pathA and pathAxis, soft clip = {domain=20:50}];
\nextgroupplot[scale only axis, restrict y to domain=190:240]
\end{groupplot}
\end{tikzpicture}
\end{document}
答案1
这只是一种解决方法。您需要添加两次情节,一次是不可见的,以便有可用于填充的内容,一次“真实”。(我已经尝试了一些剪辑等,但没有一个弹出的内容比这个解决方法更优雅,所以我发布了它。显然,我确实希望有一个更优雅的解决方案。)
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, pgfplots.groupplots}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 1}, scale only axis,
axis on top=true]
% Plot main figure (a)
\nextgroupplot[scale only axis,clip marker paths=true,
xmin=0,
xmax=100,
ymin=0,
ymax=550]
\addplot[color=blue, mark=square*] coordinates {(0,250)(100,450)};
\addplot[name path = pathA,draw=none,no marks,forget plot] coordinates {(0,250)(100,450)};
\path[name path = pathAxis] (axis cs:0,0) -- (axis cs:100,0);
\addplot [gray!30] fill between[of = pathA and pathAxis, soft clip = {domain=20:50}];
\nextgroupplot[scale only axis, restrict y to domain=190:240]
\end{groupplot}
\end{tikzpicture}
\end{document}