曲线上的标记与图例不兼容

曲线上的标记与图例不兼容

我在 pgfplots 中定义了一种新的星形样式。曲线上星形的位置是正确的,但图例中的位置是错误的。

代码和图如下。图例中的星星位置不对。如何修复?

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings}

\tikzset{
    dashstar/.style={
        dash pattern=on 5pt off 5pt,
        postaction={decorate, decoration={markings,
                mark=between positions 7.5pt and 1 step 10pt with {
                    \node {\pgftransformscale{0.6}\pgfuseplotmark{star}};}}}},
}

\pgfplotscreateplotcyclelist{lines}{
    {green,solid, very thick},
    {red, dashdotted, very thick},
    {black, dashstar, very thick},
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain=-3:3,
legend pos=outer north east,
legend cell align={left},
legend entries={$x^2$,$2x^2$,$4x^2$},
cycle list name=lines]
\addplot{x^2};
\addplot{2*x^2};
\addplot{4*x^2};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

如果您对星星使用较低级别的装饰命令,则不会出现此问题。首先对您的代码进行最小程度的损坏修复。

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings}

\tikzset{
    dashstar/.style={
        dash pattern=on 5pt off 5pt,
        postaction={decorate, decoration={markings,
                mark=between positions 7.5pt and 1 step 10pt with {
                    \begin{pgfscope}
                    \pgftransformscale{0.6}\pgflowlevelsynccm\pgfuseplotmark{star}
                    \end{pgfscope}}}}},
}

\pgfplotscreateplotcyclelist{lines}{
    {green,solid, very thick},
    {red, dashdotted, very thick},
    {black, dashstar, very thick},
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain=-3:3,
legend pos=outer north east,
legend cell align={left},
legend entries={$x^2$,$2x^2$,$4x^2$},
cycle list name=lines]
\addplot{x^2};
\addplot{2*x^2};
\addplot{4*x^2};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

\pgflowlevelsynccm并非绝对必要,但它使缩小的星星看起来仍然像星星。如果将其移除,您仍会得到正确的位置,但星星看起来更像真正的星星,即圆形。

在此处输入图片描述

我个人也建议选择不同的方式来声明dashstar模式,因为根据我自己的经验,这种方式似乎更为稳健。

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations}

\pgfdeclaredecoration{stars}{initial}{\state{initial}[width=6pt,next state=star1]
{
}
\state{star1}[width=4pt,next state=gap]
{  
   \begin{pgfscope}
   \pgftransformscale{0.6}
   \pgflowlevelsynccm
   \pgfuseplotmark{star}
   \end{pgfscope}
  }
\state{gap}[width=4pt,next state=star1]
{
  }
\state{final}
{
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
}  

}

\tikzset{
dashstar/.style={dash pattern=on 4pt off 4pt,postaction={decorate,decoration=stars}}
}

\pgfplotscreateplotcyclelist{lines}{
    {green,solid, very thick},
    {red, dashdotted, very thick},
    {black, dashstar, very thick},
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain=-3:3,
legend pos=outer north east,
legend cell align={left},
legend entries={$x^2$,$2x^2$,$4x^2$},
legend image code/.code={%
                    \draw[#1] (0cm,0.01cm) -- (0.7cm,0.01cm);
                }, 
cycle list name=lines]
\addplot{x^2};
\addplot{2*x^2};
\addplot{4*x^2};
\end{axis}
\end{tikzpicture}
\end{document}

但这当然完全取决于你。

相关内容