3

I am trying to generate a tikz figure similar to enter image description here

So far I made the following latex code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
  pics/BitArray/.style n args={2}{
  code={\begin{scope}[#2]
  \fill
    \foreach \row [count=\y] in {#1} {
      \foreach \cell [count=\x] in \row {
        \ifnum\cell=1 %
          (\x-1, -\y+1) rectangle ++(1, -1)
        \fi
        \pgfextra{%
          \global\let\maxx\x
          \global\let\maxy\y
        }%
      }
    }
  ;
  \draw[thin] (0, 0) grid[step=1] (\maxx, -\maxy);
  \end{scope}
  }
 }
}


\begin{document}
\[
\begin{tikzpicture}[scale=5]
[baseline=(N.base)]
\begin{scope}[local bounding box=BA1,scale=0.4]
 \foreach \y in {0.2,0.4,...,1.8} {
      \foreach \x in {0.2,0.4,...,1.2} {
          \pgfmathparse{0.9*rnd+0.01}
          \definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
          \node[fill=MyColor,inner sep=0.4cm,outer sep=0pt,anchor=center] at (\x,\y) {}; 
      }
  }
  \draw[step=.2,help lines] (0,0) grid (1.4,2); 

\end{scope}  
\node[above=1pt of BA1] {$D$};
\node[left=1pt of BA1.north west] {$X$};
\node[left=1pt of BA1.west] (N) {$N$};
\end{tikzpicture}
~\sim~
\begin{tikzpicture}[baseline=(N2.base)]
\begin{scope}[local bounding box=BA2,scale=0.5]
 \pic[right=3cm of BA1.north east] {BitArray={%
    {1,1,1,1},
    {1,1,1,0},
    {1,0,1,1},
    {1,0,0,1},
    {1,1,0,0},
    {0,1,0,1},
    {0,0,1,0},
    {0,1,0,0},
    {0,0,0,0},
    {0,0,0,0}%
  }{fill=black,scale=0.5}};
\end{scope}  
\node[left=1pt of BA2.north west] {$\mathbf{Z}$};
\node[above=1pt of BA2] {$K$};
\node[left=1pt of BA2.west] (N2) {$N$};
\end{tikzpicture}
~\dots~*
\begin{tikzpicture}[scale=5][baseline=(dots.base)]
\begin{scope}[local bounding box=BA3,scale=0.4]
 \foreach \y in {0.2,0.4,...,0.6} {
      \foreach \x in {0.2,0.4,...,1.2} {
          \pgfmathparse{0.9*rnd+0.01}
          \definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
          \node[fill=MyColor,inner sep=0.4cm,outer sep=0pt,anchor=center] at (\x,\y) {}; 
      }
  }
  \draw[step=.2,help lines] (0,0) grid (1.4,0.8); 

\end{scope} 
\node[below=1pt of BA3.south] (dots) {$\vdots$};
\end{tikzpicture}
\]
\end{document}

Any suggestion how to complete my figure?

Second question: How can I make for instance a gamma distribution just using tikz? Is it possible?

1
  • 1
    As for your second question: yes, it is possible, see here. (Generally it is better two ask two separate question. There is no charge.)
    – user121799
    Commented Dec 4, 2018 at 1:31

1 Answer 1

4

Here is a proposal. Your baselines were correct except you put them into separate [...] brackets. Other than that you may want to load the decorations.pathreplacing library for the braces and so on. (I had to move some nodes to the left since they'd interfere with the braces otherwise.)

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathreplacing}

\tikzset{
  pics/BitArray/.style n args={2}{
  code={\begin{scope}[#2]
  \fill
    \foreach \row [count=\y] in {#1} {
      \foreach \cell [count=\x] in \row {
        \ifnum\cell=1 %
          (\x-1, -\y+1) rectangle ++(1, -1)
        \fi
        \pgfextra{%
          \global\let\maxx\x
          \global\let\maxy\y
        }%
      }
    }
  ;
  \draw[thin] (0, 0) grid[step=1] (\maxx, -\maxy);
  \end{scope}
  }
 }
}


\begin{document}
\[
\begin{tikzpicture}[scale=5,baseline=(N.base)]
\begin{scope}[local bounding box=BA1,scale=0.4]
 \foreach \y in {0.2,0.4,...,1.8} {
      \foreach \x in {0.2,0.4,...,1.2} {
          \pgfmathparse{0.9*rnd+0.01}
          \definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
          \node[fill=MyColor,inner sep=0.4cm,outer sep=0pt,anchor=center] at (\x,\y) {}; 
      }
  }
  \draw[step=.2,help lines] (0,0) grid (1.4,2); 

\end{scope}  
\node[above=1pt of BA1] {$D$};
\node[left=10pt of BA1.north west] {$X$};
\node[left=10pt of BA1.west] (N) {$N$};
\path (BA1.north west) -- (BA1.south west) coordinate[pos=0.8] (aux);
\draw[line width=1pt,blue,fill=blue,fill opacity=0.2]
([xshift=-0.25pt,yshift=0.25pt]aux) rectangle ([xshift=0.25pt,yshift=-0.25pt]BA1.south east);
\draw[thick,decorate,decoration={brace,raise=3pt}] (BA1.south west) -- (aux)
node[midway,left=3pt]{$X_{w}$}; 
\draw[thick,decorate,decoration={brace,raise=3pt}]  (aux) -- (BA1.north west)
node[midway,left=3pt]{$X_{-w}$}; 
\end{tikzpicture}
~\sim~
\begin{tikzpicture}[baseline=(N2.base)]
\begin{scope}[local bounding box=BA2,scale=0.5]
 \pic[right=3cm of BA1.north east] {BitArray={%
    {1,1,1,1},
    {1,1,1,0},
    {1,0,1,1},
    {1,0,0,1},
    {1,1,0,0},
    {0,1,0,1},
    {0,0,1,0},
    {0,1,0,0},
    {0,0,0,0},
    {0,0,0,0}%
  }{fill=black,scale=0.5}};
\end{scope}  
\node[left=10pt of BA2.north west] {$\mathbf{Z}$};
\node[above=1pt of BA2] {$K$};
\node[left=10pt of BA2.west] (N2) {$N$};
\path (BA2.north west) -- (BA2.south west) coordinate[pos=0.8] (aux);
\draw[line width=1pt,blue,fill=blue,fill opacity=0.2]
([xshift=-0.25pt,yshift=0.25pt]aux) rectangle ([xshift=0.25pt,yshift=-0.25pt]BA2.south east);
\draw[thick,decorate,decoration={brace,raise=3pt}] (BA2.south west) -- (aux)
node[midway,left=3pt]{$Z_{w}$}; 
\draw[thick,decorate,decoration={brace,raise=3pt}]  (aux) -- (BA2.north west)
node[midway,left=3pt]{$Z_{-w}$}; 
\end{tikzpicture}
~\dots~*
\begin{tikzpicture}[scale=5,baseline=(dots.base)]
\begin{scope}[local bounding box=BA3,scale=0.4]
 \foreach \y in {0.2,0.4,...,0.6} {
      \foreach \x in {0.2,0.4,...,1.2} {
          \pgfmathparse{0.9*rnd+0.01}
          \definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
          \node[fill=MyColor,inner sep=0.4cm,outer sep=0pt,anchor=center] at (\x,\y) {}; 
      }
  }
  \draw[step=.2,help lines] (0,0) grid (1.4,0.8); 

\end{scope} 
\node[below=1pt of BA3.south] (dots) {$\vdots$};
\end{tikzpicture}
\]
\end{document}

enter image description here

1
  • Wonderful! I like this image.
    – Sebastiano
    Commented Dec 4, 2018 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .