pgfplots 中的分层/多色标记

pgfplots 中的分层/多色标记

是否可以在 pgfplots 中创建多层/堆叠标记?我想要堆叠的标记具有多种 (3+) 颜色。我想要的效果可以通过多个图来实现,但这不会显示在图例中。请参阅下面的最小示例

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
    \addplot [draw=none, mark=*, mark options = {fill=yellow,draw=black}] {x*x};
    \addplot [mark=x, mark options = {draw=red}] {x*x};
    \legend{$x^2$}
\end{axis}
\end{tikzpicture}
\end{document}

代码输出

答案1

这相当简单。您可以定义一个新的绘图标记并将多种颜色存储在 pgf 键中。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\tikzset{multi/.style={mark=multi,multi mark={#1}},
multi mark/.code={\tikzset{/tikz/multi/.cd,#1}},
multi/.cd,outer/.initial=black,inner/.initial=black,fill/.initial=white}
\pgfdeclareplotmark{multi}{%
   \pgfsetfillcolor{\pgfkeysvalueof{/tikz/multi/fill}}%
   \pgfpathcircle{\pgfpoint{0pt}{0pt}}{\pgfplotmarksize}%
   \pgfusepathqfill%
   \pgfsetstrokecolor{\pgfkeysvalueof{/tikz/multi/inner}}%
   \pgfpathmoveto{\pgfqpointpolar{45}{\pgfplotmarksize}}%
   \pgfpathlineto{\pgfqpointpolar{-135}{\pgfplotmarksize}}%
   \pgfpathmoveto{\pgfqpointpolar{-45}{\pgfplotmarksize}}%
   \pgfpathlineto{\pgfqpointpolar{135}{\pgfplotmarksize}}%
   \pgfusepathqstroke%
   \pgfsetstrokecolor{\pgfkeysvalueof{/tikz/multi/outer}}%
   \pgfpathcircle{\pgfpoint{0pt}{0pt}}{\pgfplotmarksize}%
   \pgfusepathqstroke%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    \addplot [multi={inner=red,outer=black,fill=yellow}] {x*x};
    \legend{$x^2$}
\end{axis}
\begin{axis}[xshift=8cm]
    \addplot [multi={inner=blue,outer=red,fill=orange}] {x*x};
    \legend{$x^2$}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容