How to Make Dotted Grid Lines in pgfplots
At a glance
- 1. Load pgfplots
Use the pgfplots package and create an axis environment.
- 2. Enable the grid
Choose major, minor, or both grid types.
- 3. Set the line style
Apply dotted or densely dotted to the relevant grid style.
- 4. Add minor ticks
Configure minor tick counts when you want a finer dotted grid.
A pgfplots grid can use any TikZ line style, including dotted and densely dotted. The usual solution is to add grid style={dotted} to the axis options.
You can control major and minor grids separately. Use grid=major for the normal grid, grid=minor for only the minor grid, or grid=both for both types.
Basic dotted grid
This complete example draws a function with dotted major grid lines:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xmin = -3, xmax = 3,
ymin = -1, ymax = 10,
grid = major,
grid style = {dotted},
]
\addplot[blue, thick] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
The grid style key is passed to the TikZ paths used for the grid. Therefore, standard TikZ styles such as dotted, densely dotted, thin, gray, and color specifications can be used there.
For more closely spaced dots, replace dotted with densely dotted:
grid style = {gray, densely dotted},
Showing both major and minor dotted grids
A major grid is associated with the major tick marks. A minor grid requires minor ticks to be present. Set grid=both and specify how many minor ticks should occur between major ticks.
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 5,
ymin = 0, ymax = 25,
grid = both,
major grid style = {gray, dotted},
minor grid style = {gray!60, densely dotted},
minor tick num = 1,
]
\addplot[red, thick, domain=0:5] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
Here, minor tick num=1 places one minor tick between each pair of neighboring major ticks on each axis. Since grid=both is active, pgfplots draws both the major and minor grid lines.
The two grid types can have different appearances:
major grid style = {black, dotted},
minor grid style = {gray!50, loosely dotted},
The major and minor styles accept ordinary TikZ path options. For example, you can combine a color, line width, and dot pattern:
major grid style = {blue!50, thin, densely dotted},
Controlling the horizontal and vertical grids separately
If you want dotted vertical lines but no horizontal lines, enable only the x grid:
xmajorgrids,
grid style = {dotted},
Similarly, use ymajorgrids for horizontal major grid lines. The following example gives separate styles to the two directions:
\begin{axis}[
xmajorgrids,
ymajorgrids,
x grid style = {red!60, dotted},
y grid style = {blue!60, densely dotted},
]
The direction-specific styles are useful when the two axes have different roles or when one set of grid lines should be visually lighter. For minor grids, use xminorgrids and yminorgrids. Combine x grid style and y grid style with major grid style and minor grid style when you need to control both direction and major/minor appearance.
Grid points rather than dotted lines
A dotted grid line consists of repeated short dots along each horizontal or vertical line. If you mean isolated dots only at grid intersections, that is a different visual effect. grid style={dotted} does not create a separate marker at every intersection; it styles the grid paths as dotted lines.
For a genuine point lattice, one option is to add a separate plot consisting of point markers. For example:
\begin{axis}[
xmin=0, xmax=4,
ymin=0, ymax=3,
xtick={0,1,2,3,4},
ytick={0,1,2,3},
axis lines=box,
]
\addplot[
only marks,
mark=* ,
mark size=0.8pt,
black!50,
samples=1,
domain=0:0,
] coordinates {
(0,0) (1,0) (2,0) (3,0) (4,0)
(0,1) (1,1) (2,1) (3,1) (4,1)
(0,2) (1,2) (2,2) (3,2) (4,2)
(0,3) (1,3) (2,3) (3,3) (4,3)
};
\end{axis}
For most plots, however, dotted grid lines are simpler and scale more naturally when the axis limits change.
Common mistakes
Forgetting to enable the grid
grid style={dotted} changes the appearance of a grid, but it does not necessarily turn a grid on. Include grid=major, grid=minor, or grid=both, or use a directional key such as xmajorgrids.
Expecting minor grids automatically
grid=both requests both grid types, but minor grid lines require minor ticks. Add, for example:
minor tick num=1,
grid=both,
Styling the plot instead of the grid
This changes the curve, not the grid:
\addplot[dotted] {x^2};
Put the style in the axis options instead:
grid style={dotted},
Using an unsuitable compatibility setting
The exact appearance of some pgfplots defaults can vary between versions. Setting a compatibility level such as \pgfplotsset{compat=1.18} makes the document’s behavior more predictable when that version is installed.
For the ordinary “make my pgfplots grid dotted” case, the essential option is simply:
grid=major,
grid style={dotted},References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.