# Grid dots in pgfplots

How can I generate a multi-resolution dot grid (see image) in pgfplots? The dot grid does not necessarily coincide with the axis ticks.

edit: The above plot was created using

\addplot[only marks,mark=*] plot table[row sep=crcr,]{ high res. coordinates }
\addplot[only marks,mark=*] plot table[row sep=crcr,]{ low res. coordinates }


with huge tables of MATLAB-computed coordinates. What I'm looking for in this question, is a way to algorithmically create the dot grid in pgfplots.

-
How did you create this image? Can you post the code? – percusse Apr 2 '14 at 15:06
How did you compute the coordinates in MATLAB? – student Apr 2 '14 at 15:38

With tikz

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (-2.6,-2.6) rectangle (2.6,2.6);
\foreach \x in {-2,-1,...,2}{
\draw (\x,-2.4) -- (\x,-2.6)node[below,font=\footnotesize] {\x};
\draw (\x,2.4) -- (\x,2.6);
}
\foreach \y in {-2,-1,...,2}{
\draw (-2.4,\y) -- (-2.6,\y)node[left,font=\footnotesize] {\y};
\draw (2.6,\y) -- (2.4,\y);
}
\foreach \x in {-2.4,-2.2,...,2.4}{
\foreach \y in {-2.4,-2.2,...,2.4}{
}}
\foreach \x in {-2.4,-1.8,...,2.4}{
\foreach \y in {-2.4,-1.8,...,2.4}{
}}
\end{tikzpicture}
\end{document}


Using pgfplots.

I took Tarass's answer and added the fots to the back ground so that plots come front.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{backgrounds}

\begin{document}

\begin{tikzpicture}

\begin{axis}[xmin=-2.6,xmax=2.6,
ymin=-2.6,ymax=2.6,
after end axis/.code={%
\begin{scope}[on background layer]
\foreach \x in {-2.4,-2.2,...,2.4}{
\foreach \y in {-2.4,-2.2,...,2.4}{
}}
\foreach \x in {-2.4,-1.8,...,2.4}{
\foreach \y in {-2.4,-1.8,...,2.4}{
}}
\end{scope}
},]
\end{axis}
\end{tikzpicture}
\end{document}


This is some what slower than Christian Feuersänger's approach though.

With \addplot (still slower)

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}

\begin{axis}[xmin=-2.6,xmax=2.6,
ymin=-2.6,ymax=2.6,
]
\foreach \x in {-2.4,-2.2,...,2.4}{
\foreach \y in {-2.4,-2.2,...,2.4}{
\addplot[only marks,mark size=0.5pt,black!30] coordinates {(\x,\y)} ;
}}
\foreach \x in {-2.4,-1.8,...,2.4}{
\foreach \y in {-2.4,-1.8,...,2.4}{
\addplot[only marks,mark size=0.8pt,black!80] coordinates {(\x,\y)} ;
}}
\end{axis}
\end{tikzpicture}
\end{document}


-
If you want to use pgfplots axis you can always combine them, \draw works inside the axis environment as well, perhaps you should then use the axis cs to position the dots. – hugovdberg Apr 2 '14 at 16:06
@hugovdberg I know but, I preferred not to mix up. Hence the manual axis. Thanks for noting. – Harish Kumar Apr 2 '14 at 16:07
You're right, I need pgfplots as I will add more plots to the grid. Unfortunately I can't get your code working in pgfplots (I assume \foreach is to blame here). Can you update your example for a working pgfplots version, please? – Bloops Apr 2 '14 at 16:49
As I undersand, @Bloops wants to use pgfplot and he wants that black dots coincide with axis ticks. But I'm maybe wrong ? – Tarass Apr 2 '14 at 16:50
@Bloops, why foreach should be to blame here ? – Tarass Apr 2 '14 at 16:53

Finally with black dots on the coordinate (essentilly done by Harish Kumar):

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{backgrounds}

\begin{document}

\begin{tikzpicture}

\begin{axis}[xmin=-2.6,xmax=2.6,
ymin=-2.6,ymax=2.6,
after end axis/.code={%
\begin{scope}[on background layer]
\foreach \x in {-2.6,-2.4,...,2.6}{
\foreach \y in {-2.6,-2.4,...,2.6}{
}}
\foreach \x in {-2,...,2}{
\foreach \y in {-2,...,2}{
}}\end{scope}
}]
\end{axis}
\end{tikzpicture}
\end{document}

-
When using after end axis, the dots are drawn in front of any plot that is added to the axis. But I'd like the dots to be placed behind additional plots. And before end axis seems to not like \foreach. – Bloops Apr 2 '14 at 17:54
Sorry, Too bad ;-) – Tarass Apr 2 '14 at 18:09
+1, I used your answer ;) (you were faster and I was sleeping. It was night for me ). And you were very close. Missed it by a whisker. :) – Harish Kumar Apr 2 '14 at 22:58
I use part of yours first ;-) I thought about layers and background but I didn't know exactly how it works. As night came to me to, it was your turn. Thank's for your lights. ;-) – Tarass Apr 3 '14 at 4:33

Since you explicitly stated that the dots do not need to match up with the coordinate system, you can simply use a fill pattern for the background.

This is the most efficient way to get what you want - it scales to arbitrary sizes without enlarging the resulting pdf. It will also be faster when displaying it.

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{patterns}

\pgfdeclarepatternformonly{pattern}
{\pgfpoint{-0.3mm}{-0.3mm}}%% BB lower left
{\pgfpoint{1cm}{1cm}}% BB upper right
{\pgfpoint{1cm}{1cm}}% tile size
{
\foreach \x in {0,0.3333333,0.66666} {%
\foreach \y in {0,0.333333,0.6666666} {%
\ifdim\x pt=0pt
\ifdim\y pt =0pt
\fi
\fi
\pgfusepath{fill}%
}%
}%
%\pgfpathrectangle\pgfpointorigin{\pgfqpoint{1cm}{1cm}}\pgfusepath{stroke}
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=-2,xmax=2,
ymin=-2,ymax=2,
axis background/.style={pattern=pattern},
]
\end{axis}
\end{tikzpicture}

\end{document}


Note that this is entirely unrelated with pgfplots.

-
Huh, that is kind of intimidating :-) I might not had make my self clear enough: The dots need not match up with the ticks but they need to match up with the coordinate system (for example, there is a black dot at (0,0) and the interval between the dots is also important (it is specified in axis coordinate system units). – Bloops Apr 2 '14 at 18:37
A pity :) I had hoped that this kind of pattern might actually be useful after I came up with it for a use-case around an electronic course... It is a pity that one cannot easily align pattern with the coordinate system, otherwise it would be a relatively powerful solution. – Christian Feuersänger Apr 2 '14 at 18:46