平滑选项有时会在 PGFplots 中产生错误的箭头提示

平滑选项有时会在 PGFplots 中产生错误的箭头提示

似乎smooth应用于线条的选项有时会影响箭头尖端并产生不正确的结果。垂直线没有问题,但其他任何线似乎都有问题。这是一个已知的错误,还是有理由smooth不应该将其应用于直线?

更新:显示问题不会发生在 TikZ 上,并且如果smooth应用选项,曲线也会发生问题。

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\tikzstyle{MyStyle}      =[->, black, ultra thick,       ]
\tikzstyle{MyStyleSmooth}=[->, black, ultra thick, smooth]

\begin{tikzpicture}
\begin{axis}
    % 'smooth' option seems to have no effect on lines '1' and '2'
    \addplot [MyStyle      , green ] coordinates{(0,0) (0,1)} node [above] {1};
    \addplot [MyStyleSmooth, blue  ] coordinates{(1,0) (1,1)} node [below] {2};
    %
    % But, can't seem to have the 'smooth' on lines, '4', '5' and '6'
    \addplot [MyStyle      , orange] coordinates{(2,0)   (3,0)   } node [right] {3};
    \addplot [MyStyleSmooth, red   ] coordinates{(4,0)   (2,0.5) } node [below] {4};
    \addplot [MyStyleSmooth, red   ] coordinates{(2,1)   (3,1)   } node [left ] {5};
    \addplot [MyStyleSmooth, red   ] coordinates{(3,0.8) (2,0.8) } node [right] {6};

    % Similar problem with curves: "non-smooth" is ok, "smooth" is not
    \addplot[MyStyle,       mark=none, domain=0:1,samples=50, blue] (x,x*x+1);
    \addplot[MyStyleSmooth, mark=none, domain=0:1,samples=50, red ] (x,x*x+2);

\end{axis}
\end{tikzpicture}

\begin{tikzpicture} % These are all fine
    \draw [MyStyle      , green ] (0,0)   -- (0,1)    node [above] {1};
    \draw [MyStyleSmooth, blue  ] (1,0)   -- (1,1)    node [above] {2};
    %
    \draw [MyStyle      , orange] (2,0)   -- (3,0)    node [right] {3};
    \draw [MyStyleSmooth, red   ] (4,0)   -- (2,0.5)  node [below] {4};
    \draw [MyStyleSmooth, red   ] (2,1)   -- (3,1)    node [right] {5};
    \draw [MyStyleSmooth, red   ] (3,0.8) -- (2,0.8)  node [left ] {6};
\end{tikzpicture}
\end{document}

答案1

问题不在于 PGFplots,而在于 PGFplot命令(PGFplots 使用该命令绘制图表):

\tikz\draw [smooth,thick,->] plot coordinates {(0,0) (0.5,0.1) (1,0)};

产量错误的箭头提示导致绘图不流畅

发生的事情如下:设置smooth命令\draw plot会启动\pgfplothandlercurveto例程(这不是 PGFplots,而是 PGF)pgflibraryplothanders.code.tex。此例程使用命令绘制图中的最后一段

\pgfpathcurveto{\pgf@plot@curveto@first@support}{\pgf@plot@curveto@second}{\pgf@plot@curveto@second}%

这是一个 PGF 曲线命令,它使用两个支撑点将曲线从当前位置延伸到目的地。在这种情况下,第二个支撑点和目的地是同一个点 ( \pgf@plot@curveto@second),这确保曲线可以从任何方向进入最后一个点。然而,它的缺点是,该段的最后一部分总是会退化(从 A 到 A... 的一条线),这被解释为指向上方。这最后一部分用于定位箭头尖端。

为了防止使用最后一部分,我们可以重新定义有问题的函数来使用\pgfpathcurvebetweentimecontinue,它只绘制最后一行的一部分。将\pgfpathcurveto上面的命令替换为

\pgfpathcurvebetweentimecontinue{0}{0.995}{\pgf@plot@curveto@first}{\pgf@plot@curveto@first@support}{\pgf@plot@curveto@second}{\pgf@plot@curveto@second}%

只绘制了线条的最后 99.5%,这排除了退化部分,但在视觉上是相同的,特别是因为箭头尖端覆盖了线条的末端。我不确定这个值是否适用于所有情况,但它确实适用于您的测试用例:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\makeatletter
\def\pgf@plot@curveto@handler@finish{%
  \ifpgf@plot@started%
    \pgfpathcurvebetweentimecontinue{0}{0.995}{\pgf@plot@curveto@first}{\pgf@plot@curveto@first@support}{\pgf@plot@curveto@second}{\pgf@plot@curveto@second}%
  \fi%
}
\makeatother

\tikzstyle{MyStyle}      =[->, black, ultra thick,       ]
\tikzstyle{MyStyleSmooth}=[->, black, ultra thick, smooth]

\begin{tikzpicture}
\begin{axis}
    % 'smooth' option seems to have no effect on lines '1' and '2'
    \addplot [MyStyle      , green ] coordinates{(0,0) (0,1)} node [above] {1};
    \addplot [MyStyleSmooth, blue  ] coordinates{(1,0) (1,1)} node [below] {2};
    %
    % But, can't seem to have the 'smooth' on lines, '4', '5' and '6'
    \addplot [MyStyle      , orange] coordinates{(2,0)   (3,0)   } node [right] {3};
    \addplot [MyStyleSmooth, red   ] coordinates{(4,0)   (2,0.5) } node [below] {4};
    \addplot [MyStyleSmooth, red   ] coordinates{(2,1)   (3,1)   } node [left ] {5};
    \addplot [MyStyleSmooth, red   ] coordinates{(3,0.8) (2,0.8) } node [right] {6};

    % Similar problem with curves: "non-smooth" is ok, "smooth" is not
    \addplot[MyStyle,       mark=none, domain=0:1,samples=50, blue] (x,x*x+1);
    \addplot[MyStyleSmooth, mark=none, domain=0:1,samples=50, red ] (x,x*x+2);

\end{axis}
\end{tikzpicture}

\begin{tikzpicture} % These are all fine
    \draw [MyStyle      , green ] (0,0)   -- (0,1)    node [above] {1};
    \draw [MyStyleSmooth, blue  ] (1,0)   -- (1,1)    node [above] {2};
    %
    \draw [MyStyle      , orange] (2,0)   -- (3,0)    node [right] {3};
    \draw [MyStyleSmooth, red   ] (4,0)   -- (2,0.5)  node [below] {4};
    \draw [MyStyleSmooth, red   ] (2,1)   -- (3,1)    node [right] {5};
    \draw [MyStyleSmooth, red   ] (3,0.8) -- (2,0.8)  node [left ] {6};
\end{tikzpicture}
\end{document}

带有更正箭头的图

相关内容