10

We can set the appearance of the grid like follows:

\pgfplotsset{
  grid style = {
    dash pattern = on 0.05mm off 1mm,
    line cap = round,
    black,
    line width = 0.5pt
  }
}

The result may look like this:

enter image description here

At the intersection of the x- and y-grid-lines, this looks not very nice. What I like to see is somewhat like this (only recognize how the dash pattern is chosen differently in x- and y-direction to fit nicely):

enter image description here

This will produce somewhat like the first image:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[greek,english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\pgfplotsset{
  grid style = {
    dash pattern = on 0.05mm off 1mm,
    line cap = round,
    black,
    line width = 0.5pt
  }
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[grid = major]
\addplot[color=red,mark=x] coordinates {
(2,-2.8559703)
(3,-3.5301677)
(4,-4.3050655)
(5,-5.1413136)
(6,-6.0322865)
(7,-6.9675052)
(8,-7.9377747)
};
\end{axis}
\end{tikzpicture}

\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
DaPhil
  • 3,365
  • Your example code doesn't work for me when I switch on the grid using grid=both in the axis options, and I'd be surprised if it did, because line style is not a standard PGFPlots or TikZ style. The correct way to get dashed or dotted lines is to just say grid style=dashed or grid style=dotted (or grid style={red, dashed} if you want more than one option. You can find the styles that are available in the PGF manual. – Jake Feb 04 '13 at 12:33
  • You are correct, that's a typo. And explains why it doesn't work. Additional: Is there dotted pattern where the space is adapted for each axes? I will reformulate my question with an image. – DaPhil Feb 04 '13 at 12:40
  • have you seen Change the appearance of grids in pgfplots I'd be tempted to call this a duplicate, let us know if it is different – cmhughes Feb 04 '13 at 16:42
  • This is actually what I meant with the first question and figured out it was a wrong command I used. Now I am searching for a line style that fits my needs. – DaPhil Feb 04 '13 at 16:55
  • I've changed the title slightly to make it more obvious what the problem is as there is no explicit mention in the body of the question. – percusse Feb 08 '13 at 13:28

1 Answers1

6

One way to do this is to explicitly set the unit vector length of the plot to an integer multiple of the dash pattern period. If additionally the left and bottom bounds of the plot are set to a value corresponding to an integer multiple of the dash pattern period, the dash pattern will line up at the intersections:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{
  grid style = {
    dash pattern = on 0.025mm off 0.95mm on 0.025mm off 0mm, % start with half a dot to get correct centering of the pattern
    line cap = round,
    black,
    line width = 0.5pt
  }
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[grid = major, xmin=2.2, ymin=-8.2, x=10mm, y=10mm]
\addplot[color=red,mark=x] coordinates {
(2,-2.8559703)
(3,-3.5301677)
(4,-4.3050655)
(5,-5.1413136)
(6,-6.0322865)
(7,-6.9675052)
(8,-7.9377747)
};
\end{axis}
\end{tikzpicture}

\end{document}
Jake
  • 232,450
  • Unfortunately, this doesn't work while using 'enlarge x limits', which I will use in all plots because I will plot data. Also, it is a not a very general solution, I would have to fix every plot. So I guess there is not built in solution for that? – DaPhil Feb 08 '13 at 14:08
  • 1
    @DaPhil: It does work with enlarge x limits if you use it with an absolute value (like enlarge x limits={abs=0.2}). No, there is no built-in solution. You could try opening a feature request. I would recommend not using dashed grid lines at all, though, because they introduce a lot of visual clutter. Simply using very light lines is typically a more elegant approach. – Jake Feb 08 '13 at 14:14