Pgfplots:如何确保线条不会超出实际数据点太远?

Pgfplots:如何确保线条不会超出实际数据点太远?

绘制文件中的数据时,有时(对于锐角)连接数据点的线会越过这些点,从而导致视觉差异(参见图中的红色圆圈)。 有办法防止这种情况吗?

在此处输入图片描述

梅威瑟:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
   \begin{axis}[height=4cm, width=10cm, enlarge x limits=false, title={How to make sure the line do not go past data points?}]
      \addplot[blue, semithick, mark=*, mark size=0.5pt] table[col sep=comma, x expr=\coordindex, y=data]{
      data,
      0.0521857531705709,
      0.112117298120705,
      -0.0229895182246986,
      -0.0640218587649306,
      0.109484232841688,
      0.0919374953256851,
      0,
      -0.0845573880280623,
      -0.133531392624523,
      -0.134732593970157,
      0.126293725324293,
      -0.0257524961024149,
      0.0913497785882278,
      0.11247798342669,
      -0.0434851119397388,
      -0.0769610411361281,
      0.175632568643158,
      0.131852131104803,
      0,
      -0.0732034040232952,
      -0.172245904805213,
      -0.154150679827258,
      0.205443974214808,
      0.0350913198112703,
      0.0339015516756813,
      0.17114825619583,
      -0.0880333494853227,
      0.0537442760066904,
      0.0342890734786323,
      0.111521274432407,
      0,
      -0.0783690671155064,
      -0.127339422376602,
      -0.103989713524047,
      0.128381166648207,
      0.0296757681461166,
      0.0512932943875501,
      0.0697333380146752,
      -0.0641931576390595,
      0.0109891215755953,
      0.175008909947667,
      0.0535842461341067,
      0.0508584172334912,
      -0.146603474191876,
      -0.090060823918181,
      -0.104778951233177,
      0.120363682249875,
      0.0102565001671895,
      0,
      0.185717145795093,
      -0.00424629088145156,
      -0.025863510589919,
      0.0593394397863083,
      0.0828876598057677,
      0.0298529631496818,
      -0.137741925160866,
      -0.116202007659065,
      -0.158901282585856,
      0.110348057168865,
      0.0148150857851403,
      -0.0816780310142668,
      0.22314355131421,
      -0.034635496662756,
      };
      \draw[red] (axis cs:10,0.126293725324293) circle(3pt);
      \draw[red] (axis cs:22,0.205443974214808,) circle(3pt);
   \end{axis}
\end{tikzpicture}

\end{document}

答案1

您可以将更改为line join例如round(或bevel) - 请参阅在 TikZ 中,何时“line join=miter”比“line join=round”更可取?

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}

\begin{tikzpicture}
   \begin{axis}[height=4cm, width=10cm, enlarge x limits=false, title={How to make sure the line do not go past data points?}]
      \addplot[blue, semithick, mark=*, mark size=0.5pt, line join=round] table[col sep=comma, x expr=\coordindex, y=data]{
      data,
      0.0521857531705709,
      0.112117298120705,
      -0.0229895182246986,
      -0.0640218587649306,
      0.109484232841688,
      0.0919374953256851,
      0,
      -0.0845573880280623,
      -0.133531392624523,
      -0.134732593970157,
      0.126293725324293,
      -0.0257524961024149,
      0.0913497785882278,
      0.11247798342669,
      -0.0434851119397388,
      -0.0769610411361281,
      0.175632568643158,
      0.131852131104803,
      0,
      -0.0732034040232952,
      -0.172245904805213,
      -0.154150679827258,
      0.205443974214808,
      0.0350913198112703,
      0.0339015516756813,
      0.17114825619583,
      -0.0880333494853227,
      0.0537442760066904,
      0.0342890734786323,
      0.111521274432407,
      0,
      -0.0783690671155064,
      -0.127339422376602,
      -0.103989713524047,
      0.128381166648207,
      0.0296757681461166,
      0.0512932943875501,
      0.0697333380146752,
      -0.0641931576390595,
      0.0109891215755953,
      0.175008909947667,
      0.0535842461341067,
      0.0508584172334912,
      -0.146603474191876,
      -0.090060823918181,
      -0.104778951233177,
      0.120363682249875,
      0.0102565001671895,
      0,
      0.185717145795093,
      -0.00424629088145156,
      -0.025863510589919,
      0.0593394397863083,
      0.0828876598057677,
      0.0298529631496818,
      -0.137741925160866,
      -0.116202007659065,
      -0.158901282585856,
      0.110348057168865,
      0.0148150857851403,
      -0.0816780310142668,
      0.22314355131421,
      -0.034635496662756,
      };
      \draw[red] (10,0.126293725324293) circle[radius=3pt];
      \draw[red] (22,0.205443974214808) circle[radius=3pt];
   \end{axis}
\end{tikzpicture}

\end{document}

图形

您还可以调整miter limit以使线条连接的外观更加自动化。摘自手册:

/tikz/miter limit=⟨factor⟩(无默认值,初始值为 10)当您使用斜角连接并且有一个非常尖锐的角(一个小角度)时,斜角连接可能会突出实际连接点很远。在这种情况下,如果它突出的距离超过线宽的 ⟨factor⟩ 倍,斜角连接将被斜面连接取代。

相关内容