PGFPlots:如何按轴选择性地剪切特定标记?

PGFPlots:如何按轴选择性地剪切特定标记?

在这个 MWE 中,我需要用轴剪掉黄色方块标记,同时保持红色菱形标记完整。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0, xmax=1,
            ymin=0, ymax=1,
            domain=0:1,
            every axis plot post/.append style={
                line width=2pt,
            }
        ]
            \addplot [ mark=square*, mark size=4pt,
            mark options={fill=yellow, line width = 0.5pt}, samples = 3 ] {x};
            \addplot [ only marks,  mark=diamond*, mark size=5pt,
            mark options={fill=red, line width = 0.5pt}, samples = 2 ] {x};
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述


附录

clip marker paths使用图层时选项不起作用。 在这种情况下该如何解决?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,
layers/my layer set/.define layer set={bg,main,fg}{},
set layers=my layer set,
mark layer=like plot}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0, xmax=1,
            ymin=0, ymax=1,
            domain=0:1,
            every axis plot post/.append style={
                line width=2pt,
            }
        ]
            \addplot [ mark=square*, mark size=4pt,
            mark options={fill=yellow, line width = 0.5pt}, samples = 3, clip marker paths, on layer = bg ] {x};
            \addplot [ only marks,  mark=diamond*, mark size=5pt,
            mark options={fill=red, line width = 0.5pt}, samples = 2, on layer = fg ] {x};
        \end{axis}
    \end{tikzpicture}
\end{document}

答案1

将选项添加clip marker paths到带有黄色标记的图中。

编辑:正如所指出的,使用图层时这不起作用。但是,使用clip mode=individual似乎有效。

在此处输入图片描述

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,
layers/my layer set/.define layer set={bg,main,fg}{},
set layers=my layer set,
mark layer=like plot}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0, xmax=1,
            ymin=0, ymax=1,
            domain=0:1,
            every axis plot post/.append style={
                line width=2pt,
            }
        ]
            \addplot [ mark=square*, mark size=4pt,
            mark options={fill=yellow, line width = 0.5pt}, samples = 3, on layer = bg,
            clip mode=individual % <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ] {x};
            \addplot [ only marks,  mark=diamond*, mark size=5pt,
            mark options={fill=red, line width = 0.5pt}, samples = 2, on layer = fg ] {x};
        \end{axis}
    \end{tikzpicture}
\end{document}

相关内容