# Dashed lines ovelay with pgfplots

When trying to plot multiple dashed lines over the same dashed line in pgfplots, the dashing mismatches, and draws a full line, is there any way to circumvent this?

MVE left; drawn with pgfplots, right; drawn with tikz (I want the tikz effect with the easiness of pgfplots)

Returns this

\begin{filecontents}{test.txt}
a       b
1       0
0       1
0       0
1       0
0       1
1       0
\end{filecontents}
\documentclass[border=5mm]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[mark={}]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\draw[dashed] (1,0) -- (0,1) -- (0,0) -- (1,0) -- (1,0) -- (0,1) --(1,0);
\end{tikzpicture}
\end{document}

• It's just a matter of scale and coincidence, I suppose. Set width=7.1cm (for example) for the axis, and you don't get full overlap, but add [scale=1.201] for the second tikzpicture and you do get the overlap there. You could also add dash phase=1pt to the \addplot options in the pgfplots case to remove (most of) the overlap. But none of these are general solutions. Nov 14 '17 at 16:27
• Is there a way to find the optimal dash phase? I want to plot a unit cell (no the MWE), and i have 2 or three overlapping lines, with the overlapping being different for all cases (although theres a 3d distance of 1 between all points). I tried designing my own dashdotted line, but still get some overlap. Nov 14 '17 at 16:34
• Tried setting the dash phase=2pt for my unit cell, and it looks great, thanks a lot. Would however love for some way of doing this automaticly. Nov 14 '17 at 16:40

While not a general, automatic solution, it's possible to play around with the dash phase or custom dash patterns, to avoid (at least most of) the overlapping.

\begin{filecontents}{test.txt}
a       b
1       0
0       1
0       0
1       0
0       1
1       0
\end{filecontents}
\documentclass[border=5mm]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[mark={}]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[mark={}]
\addplot [dash pattern=on 0pt off 1pt on 5.68pt] table [x=a,y=b]{test.txt};
\end{axis}
\end{tikzpicture}
\end{document}

• Thanks a lot for the answer, I will accept it for now, but should someone come with a answer that works for the general case I will unaccept your answer and accept theirs. Nov 14 '17 at 21:24
• @ThorbjørnE.K.Christensen Of course. Nov 14 '17 at 21:27
• I was just being precarious; I would feel bad if i unaccepted your answer without letting you know why, and I thought that I might forget to make the comment if I ever saw a general answer. Nov 14 '17 at 21:37
• Oh wait, just noticed the dash pattern you made starts with 0pt what does this do? Nov 14 '17 at 21:38
• @ThorbjørnE.K.Christensen Ehm, just something weird I happened to try. Looking more closely at the result, I'm not entirely sure exactly what it does ... Nov 14 '17 at 21:53