1

How can I start with colour a grid box? I want to draw below picture.

enter image description here

I had tried below. It is very hard to guess the node number.

\documentclass{article}
\usepackage{tikz}
\usepackage{subcaption}
\begin{document}

\begin{figure} \centering \captionsetup[subfigure]{skip=0pt} % to reduce space above subcaption \begin{tikzpicture}

\draw[step=1cm,gray,very thin] (-2,-2) grid (2,2); \node at (0,1,1) {6}; \node at (0,1,4) {6};

\end{tikzpicture} \caption{stuff} \end{figure} \end{document}

enter image description here

aan
  • 2,663

3 Answers3

2

I have no idea what you mean by "guess the node number". You are working in 2D here, so all coordinates should be given with only two numbers (x,y). Here I start the 4 by 4 grid with lower left corner at (0,0) - all else follows naturally:

\documentclass{article}
\usepackage{tikz}
\begin{document}  
\begin{tikzpicture}    
\fill[yellow!20] (0,0) rectangle (2,2);
\fill[red!20] (0,2) rectangle (2,4);
%...
\draw[gray,very thin] (0,0) grid (4,4);
\node at (0.5,0.5) {1};
\node at (1.5,0.5) {2};
\node at (2.5,0.5) {3};
\node at (3.5,0.5) {4};
\node at (0.5,1.5) {3};
%...
\draw[-stealth,ultra thick] (5,2) --node[above] {max pool} (7,2);
\draw[gray,very thin] (8,1) grid (10,3);
\end{tikzpicture}
\end{document}

Grid with a few numbers

  • @hperkristiansen, thanks. How to fill the colour for the right hand side box? I couldn't figure out the meaning for \fill[red!20] (0,0) rectangle (2,2); – aan Aug 12 '20 at 01:10
  • 1
    In this example, the right 2 by 2 grid has lower left corner at (8,1) and upper right corner at (10,3). Try: \fill[yellow!20] (8,1) rectangle (9,2); – hpekristiansen Aug 12 '20 at 01:48
  • @hperkristiansen, thanks. Brillant! How to draw a circle like in the first diagram? – aan Aug 12 '20 at 05:58
  • 1
    Search for "tikz circle". Example: \draw (2,2) circle (3cm); – hpekristiansen Aug 12 '20 at 12:01
  • @hperkristiansen, thanks. Yours is much better. I used this \draw[] (-1.5,0.5) arc (10:300:-0.75cm) and is weird. Thanks – aan Aug 13 '20 at 01:48
1

The operative part of the grid coloring and putting data is as under --

{
\fill[yellow!20] (0,0) rectangle (2,2);%<----- the 0,0 corresponds to the coordinate 
                                               %system on the grid itself and 2,2 is 
                                               %the right upper corner of the 
                                               %rectangle 
%The green!20 says 20% green mixed with white color
%Bonus you can now create circles at any desired location also 
%Since each step is 1cm the circle radius as 1cm with red fill at coordinate 3,2

\fill[red!20] (3,2) circle (1cm); \fill[green] (-2,-2) rectangle (0,0); \fill[blue!20] (-2,0) rectangle (0,2); \node at (0.5,0.5) {1}; \node at (-0.5,-0.5) {4}; }

The grid coordinates can now easily be read off from the grid itself with the excellent preamble created by @AndrewStacey at ---

https://tex.stackexchange.com/a/39698/197451

I think it was written to scare members!!

You could combine this with the excellent answer by @hpekristiansen for connecting with the right hand grid

enter image description here

\documentclass{article}
%\url{}
\usepackage{tikz}
\usetikzlibrary{arrows, fit, matrix, positioning, shapes, backgrounds,}

\makeatletter \def\grd@save@target#1{% \def\grd@target{#1}} \def\grd@save@start#1{% \def\grd@start{#1}} \tikzset{ grid with coordinates/.style={ to path={% \pgfextra{% \edef\grd@@target{(\tikztotarget)}% \tikz@scan@one@point\grd@save@target\grd@@target\relax \edef\grd@@start{(\tikztostart)}% \tikz@scan@one@point\grd@save@start\grd@@start\relax \draw[minor help lines] (\tikztostart) grid (\tikztotarget); \draw[major help lines] (\tikztostart) grid (\tikztotarget); \grd@start \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm} \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm} \grd@target \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm} \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm} \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step}} \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}} \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb} \node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}}; \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb} \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}}; } } }, minor help lines/.style={ help lines, step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step} }, major help lines/.style={ help lines, line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width}, step=\pgfkeysvalueof{/tikz/grid with coordinates/major step} }, grid with coordinates/.cd, minor step/.initial=.2, major step/.initial=1, major line width/.initial=2pt, } \makeatother \begin{document} \begin{tikzpicture} \draw[help lines,step=1cm] (-2,-2) grid (7,4); \draw[help lines,line width=.6pt,step=1] (-2,-2) grid (7,4); \foreach \x in {-2,-1,0,1,2,3,4,5,6,7} \node[anchor=north] at (\x,-2) {\x}; \foreach \y in {-2,-1,0,1,2,3,4} \node[anchor=east] at (-2,\y) {\y}; \scoped[on background layer] { \fill[yellow!20] (0,0) rectangle (2,2); \fill[red!20] (3,2) circle (1cm); \fill[green] (-2,-2) rectangle (0,0); \fill[blue!20] (-2,0) rectangle (0,2); \node at (0.5,0.5) {1}; \node at (-0.5,-0.5) {4}; } \end{tikzpicture} %
% \begin{tikzpicture} % \draw (-2,-2) to[grid with coordinates] (7,4); % \end{tikzpicture} \end{document}

js bibra
  • 21,280
1

You could use matrix nodes instead of grids:

\documentclass[tikz, border=2mm]{standalone}

\usetikzlibrary{matrix, positioning, arrows.meta}

\begin{document}

\begin{tikzpicture}

\matrix[matrix of nodes, column sep=-\pgflinewidth, row sep=-\pgflinewidth, nodes={anchor=center, minimum size=8mm, fill=red!30, draw}] (LeftMatrix) {1 & 1 &|[fill=green!30]| 2 &|[fill=green!30]| 4\ 5 & 6 &|[fill=green!30]| 7 &|[fill=green!30]| 8 \ |[fill=yellow!30]|3 &|[fill=yellow!30]| 2 &|[fill=blue!30]| 1 &|[fill=blue!30]| 0 \ |[fill=yellow!30]|1 &|[fill=yellow!30]| 2 &|[fill=blue!30]| 3 &|[fill=blue!30]| 4 \}; \draw[-{LaTeX}] ([xshift=2mm]LeftMatrix.east) -- node[above, align=left, font=\sffamily\small] {max pool with 2x2 filters\ and stride 2} ++(0:4cm) coordinate(aux); \matrix[matrix of nodes, column sep=-\pgflinewidth, row sep=-\pgflinewidth, nodes={anchor=center, minimum size=8mm, fill=red!30, draw}, right=5mm of aux] (RightMatrix) {6 & |[fill=green!30]| 8\ |[fill=yellow!30]| 3 & |[fill=blue!30]| 4 \};

\end{tikzpicture} \end{document}

enter image description here

Ignasi
  • 136,588