1

When plotting data from file, sometimes (for acute angles) the lines that join data points go past these points, which leads to visual discrepancies (see red circles in the figure). Is there a way to prevent that?

enter image description here

MWE:

\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}

Bibi
  • 1,532

1 Answers1

2

You can change the line join to e.g. round (or bevel) - see In TikZ when is "line join=miter" the preferrable over "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}

Graph

You can also adjust the miter limit for a more automatic appearance of the line joint. From the manual:

/tikz/miter limit=⟨factor⟩ (no default, initially 10) When you use the miter join and there is a very sharp corner (a small angle), the miter join may protrude very far over the actual joining point. In this case, if it were to protrude by more than ⟨factor⟩ times the line width, the miter join is replaced by a bevel join.