Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

multi-resolution dot grid

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.

share|improve this question
    
How did you create this image? Can you post the code? –  percusse Apr 2 at 15:06
    
How did you compute the coordinates in MATLAB? –  student Apr 2 at 15:38

3 Answers 3

up vote 11 down vote accepted

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}{
    \fill[black!30] (\x,\y) circle[radius=0.5pt];
    }}
    \foreach \x in {-2.4,-1.8,...,2.4}{
    \foreach \y in {-2.4,-1.8,...,2.4}{
    \fill[black!80] (\x,\y) circle[radius=0.8pt];
    }}
  \end{tikzpicture}
\end{document}

enter image description here

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}{
    \fill[black!30] (axis cs:\x,\y) circle[radius=0.5pt];
    }}
    \foreach \x in {-2.4,-1.8,...,2.4}{
    \foreach \y in {-2.4,-1.8,...,2.4}{
    \fill[black!80] (axis cs:\x,\y) circle[radius=0.8pt];
    }}
    \end{scope}
    },]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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)} ;
    }}
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
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 at 16:06
    
@hugovdberg I know but, I preferred not to mix up. Hence the manual axis. Thanks for noting. –  Harish Kumar Apr 2 at 16:07
1  
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 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 at 16:50
    
@Bloops, why foreach should be to blame here ? –  Tarass Apr 2 at 16:53

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

enter image description here

\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}{
    \fill[black!30] (axis cs:\x,\y) circle[radius=0.5pt];
    }}
    \foreach \x in {-2,...,2}{
    \foreach \y in {-2,...,2}{
    \fill[black!80] (axis cs:\x,\y) circle[radius=0.8pt];
    }}\end{scope}
    }]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
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 at 17:54
    
Sorry, Too bad ;-) –  Tarass Apr 2 at 18:09
1  
+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 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 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} {%
            \def\radius{0.1mm}%
            \ifdim\x pt=0pt
                \ifdim\y pt =0pt
                    \def\radius{0.3mm}%
                \fi
            \fi
            \pgfpathcircle{\pgfqpoint{\x cm}{\y cm}}{\radius}%
            \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}

enter image description here

Note that this is entirely unrelated with pgfplots.

share|improve this answer
    
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 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 at 18:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.