Graphics, Figures & TablesAperiodic Grid

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

Aperiodic Grid

Post by thomgraf »

Hello,

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.

Code: Select all

\begin{tikzpicture}[domain=1:9,x=0.4cm,y=0.5cm] % Zeichenbereich
  \draw [color=gray!50,dashed]  [step=2] (0,0) grid (20,12);
  \draw[very thick, -] (0,0) -- (20,0);
  \draw[very thick, -] (0,0) -- (0,12);
  \draw[very thick, -] (0,12) -- (20,12);
  \draw[very thick, -] (20,0) -- (20,12);
  \foreach \x in {0,5,...,20} \draw (\x,0) -- (\x,-0.4) node[below] {\x};
  \foreach \y in {0,2,...,12} \draw (0,\y) -- (-0.5,\y) node[left] {\y};
%  \draw[very thick, color=red] plot[smooth] (\x,{(10)/(1+exp(-0.9*(\x-8)))});
  \draw[thick, color=blue, fill=blue] plot[mark=*, mark options={scale=1.3}] (2,2);
\end{tikzpicture}
The step=2 option create a periodic grid with equal distances horizontally and vertically.

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

Aperiodic Grid

Post by feuersaenger »

Hi,

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

Code: Select all

\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:

Code: Select all

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}

\begin{document}
\thispagestyle{empty}

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

\begin{tikzpicture}
  \begin{axis}[
    xmin=0,
    xmax=20,
    ymin=0,
    ymax=12,
    grid=major,
    xlabel=$x$ Achse,
    ylabel=$y$ Achse,
    legend entries={Scatter},
  ]
    \addplot coordinates {(2,2)};
  \end{axis}
\end{tikzpicture}
\end{document}
P.png
P.png (23.32 KiB) Viewed 8080 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
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Aperiodic Grid

Post by cgnieder »

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: Regards
site moderator & package author
thomgraf
Posts: 9
Joined: Wed Jul 18, 2012 12:21 pm

Aperiodic Grid

Post by thomgraf »

Hi:

The following line did the job for me:

Code: Select all

\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
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Aperiodic Grid

Post by localghost »

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(
Post Reply