10

Enter image description here

I am trying to code the above picture, especially the dots inside the rectangle.

This is my current code, but it does not look like the intended figure. How can I change my code to obtain the desired output?

\documentclass[11pt,a5paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[thick,->] (0,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);
\foreach \x in {0,1,2,3,4}
    \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4}
    \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
\draw [ultra thick,blue] (2,0) rectangle (3,2);
 \node at (2.5,1){patch};

\end{tikzpicture}
\end{document}
  • 12
    Junior percusse detected... – kiss my armpit Sep 16 '14 at 7:50
7

A bit kludgy but if randomness is required than a path picture is one way to go. Note that for low sparseness the compilation time increases dramatically. One solution to this would be to create a separate largish standalone document and then use \includegraphics with trimming.

\documentclass[tikz,border=5]{standalone}
\tikzset{
  random dots/.style={
    path picture={
      \pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
        {\pgfpointanchor{path picture bounding box}{north east}}%
      \pgfgetlastxy\pathwidth\pathheight
      \tikzset{shift=(path picture bounding box.center)}
      \def\sparseness{#1}
      \pgfmathparse{int(\pathwidth/\sparseness+2)}\let\m=\pgfmathresult
      \pgfmathparse{int(\pathheight/\sparseness+2)}\let\n=\pgfmathresult
      \foreach \i in {-\m,...,\m}
        \foreach \j in {-\n,...,\n}  
          \fill [shift={(\i*\sparseness,\j*\sparseness)}, shift={(rand*\sparseness/2,rand*\sparseness/2)}, every random dot/.try]
           (0,0) circle [radius=1pt];
    }
  },
  random dots/.default=5pt,
  every random dot/.style={fill=red}
}
\begin{document}
\begin{tikzpicture}

\draw [ultra thick,blue, random dots]      (0,0) rectangle ++(1,2);
\draw [ultra thick,blue, random dots=10pt] (2,0) rectangle ++(1,2);
\draw [ultra thick,blue, random dots=2pt]  (4,0) rectangle ++(1,2);

\end{tikzpicture}
\end{document}

enter image description here

6

An alternative approach for randomness using waves decoration (those "things" in the original picture look like small waves to me) and loops:

\documentclass[11pt,a5paper]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,fit}
\begin{document}
\begin{tikzpicture}[decoration={waves,radius=.8mm, segment length=2mm, angle=5mm}]
\draw[thick,->] (0,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);
\foreach \x in {0,1,2,3,4}
    \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4}
    \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

\foreach \i in {290,110,80,40}
\foreach \l in {250,90,330}
\foreach \m in {1,7}{
\node(a)[blue,fit={(2,0) (3,2)},inner sep=-\m pt]{};
\draw [thick,decorate,red,line cap=round](a.\l) to (a.\i);
}

\draw [ultra thick,blue] (2,0) rectangle (3,2) node (b)[inner xsep=0pt,inner ysep=2pt,midway,fill=white] {patch};
\draw [blue,<->,thick](b.south west)--(b.south east);
\end{tikzpicture}
\end{document}

enter image description here

5

Here a quick attempt using the patterns-library of tikz. There are much more possibilities to draw this image. I just used a white background for the patch-label, but there might be much more sophisticated solutions. For example one could choose inverted clipping to make the pattern around the patch-label fading to white/opaque.

Furthermore i threw one of your loops away, so the axes-ticks are drawn within one loop.

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

\usetikzlibrary{patterns}

\begin{document}
 \begin{tikzpicture}
  % axes and x-/y-ticks
  \draw [>=latex, <->, thick] (0,5) -- (0,0) -- (5,0);
  \foreach \x in {1,...,4} {
   \draw (\x,-2pt) -- ++(0,4pt) node [below=.1cm] {\scriptsize $\x$};
   \draw (-2pt,\x) -- ++(4pt,0) node [left=.1cm] {\scriptsize $\x$};
  }
  % rectangle with pattern and patch
  \draw [ultra thick, blue, fill, pattern=crosshatch dots, pattern color=red] (2,0) rectangle (3,2);
  \draw [thick, blue, >=latex, <->] (2,1) -- (3,1) node [midway, above=.1cm, fill=white, inner sep=0pt] {\scriptsize patch}; 
  % x-labels
  \draw [thick, dashed] (2.5,0) -- ++(0,-.5) node [below] {$X_1$};
  \draw [thick, dashed] (0,0) -- ++(0,-.5) node [below] {$X_0=0$};
 \end{tikzpicture}
\end{document}

Rendered image:

rendered image

EDIT Change colors

As the initial question was how to use special colour here some additional information about colors.

If you use dvipsnames as option in your \documentclass or do an \usepackage[usenames,dvipsnames]{color} you can use the dvips-colors. For an overview visit this link.

If you need more control about your colors you can play around with them by using e.g.

red
red!75
red!75!blue

to set the saturation of a color or mix colors up.

5

Just for fun with PSTricks.

\documentclass[pstricks,border=25pt,12pt]{standalone}
\usepackage{pstricks-add}
\begin{document}
\psLoop{25}{
\begin{pspicture}[dimen=monkey](8,4)
    \psRandom[dotsize=2pt,randomPoints=500,color](6,0)(8,4){\psframe(6,0)(8,4)}
    \psline(0,4)(0,0)(8,0)
    \psxTick(0){X_0=0}\psxTick(7){X_1}
    \pcline[nodesep=2pt]{<->}(6,2)(8,2)\naput{patch}
\end{pspicture}}
\end{document}

enter image description here

  • Thank you so much. I really appreciate your help guys. Thanks a lot. – percusse Sep 16 '14 at 8:09
  • I can not run the code, I have an error (! Undefined control sequence.<recently read> \c@lor@to@ps \end{pspicture}})). How can I define this one. – percusse Sep 16 '14 at 8:21
  • @pecusse try compiling with xelatex. – Mark Wibrow Sep 16 '14 at 12:03
4

Looks like another use case for my poisson disc sampling library:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{poisson}
\usetikzlibrary{calc}

\begin{document}
\tikzset{ % Define the shape of each little mark
    pattern/.pic = {
        \draw[red!50!black] (-0.1,0) to[out=360*rnd,in=360*rnd] (0.1*rnd,0);
    }
}

\begin{tikzpicture}
    \draw[thick,->] (0,0) -- (4.5,0);
    \draw[thick,->] (0,0) -- (0,4.5);
    \foreach \x in {0,1,2,3,4}
        \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
    \foreach \y in {0,1,2,3,4}
        \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
    % Generate the random coordinates for each little mark, using
    % poisson disc sampling (over a rectangle of 1x2, minimum distance
    % between marks of 0.1)
    \edef\mylist{\poissonpointslist{1}{2}{0.1}{25}}
    \foreach \x/\y in \mylist {
        \pgfmathsetmacro{\angle}{60*rnd}
        \path ($(\x,\y)+ (2,0)$) pic[scale=0.4,rotate=\angle] {pattern};
    }
    \draw [ultra thick,blue] (2,0) rectangle +(1,2);
    \node[fill=white, inner sep=1pt] at (2.5,1){patch};
\end{tikzpicture}

\end{document}

Compiling with lualatex and the latest pgf libraries, you'll get:

Result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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