LaTeX forum ⇒ Graphics, Figures & TablesAperiodic Grid Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
Posts: 9
Joined: Wed Jul 18, 2012 12:21 pm

Aperiodic Grid

Postby thomgraf » Fri May 17, 2013 1:44 pm


Does anybody have an idea of how to create an aperiodic grid in the tikzpicture environment? Below is my example where I would like to have distances for the grid line of 5 horizontally and 2 vertically.

  1. \begin{tikzpicture}[domain=1:9,x=0.4cm,y=0.5cm] % Zeichenbereich
  2. \draw [color=gray!50,dashed] [step=2] (0,0) grid (20,12);
  3. \draw[very thick, -] (0,0) -- (20,0);
  4. \draw[very thick, -] (0,0) -- (0,12);
  5. \draw[very thick, -] (0,12) -- (20,12);
  6. \draw[very thick, -] (20,0) -- (20,12);
  7. \foreach \x in {0,5,...,20} \draw (\x,0) -- (\x,-0.4) node[below] {\x};
  8. \foreach \y in {0,2,...,12} \draw (0,\y) -- (-0.5,\y) node[left] {\y};
  9. % \draw[very thick, color=red] plot[smooth] (\x,{(10)/(1+exp(-0.9*(\x-8)))});
  10. \draw[thick, color=blue, fill=blue] plot[mark=*, mark options={scale=1.3}] (2,2);
  11. \end{tikzpicture}

The step=2 option create a periodic grid with equal distances horizontally and vertically.

Last edited by cgnieder on Fri May 17, 2013 11:44 pm, edited 1 time in total.


Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm
Location: Rheinbach

Postby feuersaenger » Sat May 18, 2013 10:11 am


TikZ offsers the options xstep and ystep when using its grid instruction:

  1. \tikz \draw (0,0) grid [xstep=.5,ystep=.75] (3,2);

However, your example looks as if you'd like to visualize some (data) plot.

You might be interested in the package pgfplots: it is based on tikz (and offers all of tikz's style options) but it simplifies the generation of plots and suitable axes considerably.

Here is an extract of its meta data:

PGFPlots draws high quality function plots in normal or logarithmic scaling with a user-friendly interface directly in TeX. The user supplies axis labels, legend entries and the plot coordinates for one or more plots and PGFPlots applies axis scaling, computes any logarithms and axis ticks and draws the plots. It supports line plots, scatter plots, piecewise constant plots, bar plots, area plots, mesh-- and surface plots, patch plots, contour plots, quiver plots, histogram plots, polar axes, ternary diagrams, smith charts and some more.

Your example would look as follows:

  1. \documentclass[a4paper]{article}
  3. \usepackage{pgfplots}
  4. \pgfplotsset{compat=1.7}
  6. \begin{document}
  7. \thispagestyle{empty}
  9. \begin{tikzpicture}[domain=1:9,x=0.4cm,y=0.5cm] % Zeichenbereich
  10. \draw [color=gray!50,dashed] [step=2] (0,0) grid (20,12);
  11. \draw[very thick, -] (0,0) -- (20,0);
  12. \draw[very thick, -] (0,0) -- (0,12);
  13. \draw[very thick, -] (0,12) -- (20,12);
  14. \draw[very thick, -] (20,0) -- (20,12);
  15. \foreach \x in {0,5,...,20} \draw (\x,0) -- (\x,-0.4) node[below] {\x};
  16. \foreach \y in {0,2,...,12} \draw (0,\y) -- (-0.5,\y) node[left] {\y};
  17. % \draw[very thick, color=red] plot[smooth] (\x,{(10)/(1+exp(-0.9*(\x-8)))});
  18. \draw[thick, color=blue, fill=blue] plot[mark=*, mark options={scale=1.3}] (2,2);
  19. \end{tikzpicture}
  21. \begin{tikzpicture}
  22. \begin{axis}[
  23. xmin=0,
  24. xmax=20,
  25. ymin=0,
  26. ymax=12,
  27. grid=major,
  28. xlabel=$x$ Achse,
  29. ylabel=$y$ Achse,
  30. legend entries={Scatter},
  31. ]
  32. \addplot coordinates {(2,2)};
  33. \end{axis}
  34. \end{tikzpicture}
  35. \end{document}

P.png (23.32 KiB) Viewed 2409 times

Note that the default configuration of PGFPlots results in the same tick marks as your manually tuned example. It also offers the same markers as your selection (otherwise we could have configured them using an option list \addplot[<options>]). I have also added axis labels and a legend, just to demonstrate the idea.

PGFPlots also allows to reconfigure the appearance of an axis (like dashed grid lines or outer tick marks as in your example). Perhaps a brief look into the example of the pgfplots manual helps to decide if the package suits your needs.

Disclaimer note: I am author of PGFPlots.
Last edited by feuersaenger on Sun May 19, 2013 3:15 pm, edited 1 time in total.

User avatar
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany

Postby cgnieder » Sat May 18, 2013 11:19 am

Hi Christian, hi thomgraf

I really appreciate such a comprehensive answer but we are an English forum and I think it is unfair to our other users when we have German threads. So it would be really nice if you both could provide English versions of your posts.

By the way: there are also German LaTeX forums:

chemmacros · chemformula · leadsheets · xsim

Posts: 9
Joined: Wed Jul 18, 2012 12:21 pm

Postby thomgraf » Tue May 21, 2013 10:26 am


The following line did the job for me:
  1. \draw[color=gray!50,dashed] (0,0) grid[xstep=5,ystep=2] (20,12);

Simply replace step by xstep and ystep.

Sorry for previously having posted in German.
Last edited by cgnieder on Tue May 21, 2013 6:18 pm, edited 1 time in total.

User avatar
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Sun May 26, 2013 12:14 pm

thomgraf wrote:[…] Sorry for previously having posted in German.

Since you seem either not to be willing or be able to translate your question in defiance of the moderator request, I did that. m(
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 8 guests