9

I have made a graph as shown in the following figure:

Plotted graphs

For each rectangle I'd like to generate n random points in it, is it possible?

My code:

\begin{tikzpicture}
\begin{axis}[
    grid=major,
    width=\textwidth,
    height=\axisdefaultheight,
    xlabel = Time $(s)$,
    ylabel = Frequency,
    xmin = 0, xmax = 13,
    ymin = 0, ymax = 4000,
]

    \draw[pattern=north west lines, pattern color=blue, line width=1pt] (0,0) rectangle (40,100);
    \draw[pattern=north east lines, pattern color=blue, line width=1pt] (40,0) rectangle (80,100);
    \draw[pattern=north west lines, pattern color=blue, line width=1pt] (80,0) rectangle (120,100);

    \draw[pattern=north east lines, pattern color=green, line width=1pt] (0,100) rectangle (40,200);
    \draw[pattern=north west lines, pattern color=green, line width=1pt] (40,100) rectangle (80,200);
    \draw[pattern=north east lines, pattern color=green, line width=1pt] (80,100) rectangle (120,200);

    \draw[pattern=north west lines, pattern color=red, line width=1pt] (0,200) rectangle (40,300);
    \draw[pattern=north east lines, pattern color=red, line width=1pt] (40,200) rectangle (80,300);
    \draw[pattern=north west lines, pattern color=red, line width=1pt] (80,200) rectangle (120,300);

    \draw[pattern=north east lines, pattern color=orange, line width=1pt] (0,300) rectangle (40,400);
    \draw[pattern=north west lines, pattern color=orange, line width=1pt] (40,300) rectangle (80,400);
    \draw[pattern=north east lines, pattern color=orange, line width=1pt] (80,300) rectangle (120,400);


\end{axis}
\draw[decoration={brace,mirror,raise=15pt},decorate]   (0,0) -- node[below=16pt] {\textbf{C}} (3.2,0);
\draw[decoration={brace,mirror},decorate]   (10.8,0) -- node[xshift=10pt] {\rotatebox{-90}{\textbf{band}}} (10.8,1.4);
\end{tikzpicture}
egreg
  • 1,121,712

1 Answers1

12

EDIT: I realized that my original answer (below) does not work for newer versions of pgfplots (since there is some mismatch between xmax, ymax and the explicit coordinates you are using for the rectangles. I fixed that, and also split the rectangles in smaller pieces. The number of points is controlled by samples=100.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    grid=major,
    width=\textwidth,
    height=\axisdefaultheight,
    xlabel = Time $(s)$,
    ylabel = Frequency,
    xmin = 0, xmax = 130,
    ymin = 0, ymax = 4000,
]
%
    \draw[pattern=north west lines, pattern color=blue, line width=1pt] (0,00) rectangle (40,1000);
    \draw[pattern=north east lines, pattern color=blue, line width=1pt] (40,00) rectangle (80,1000);
    \draw[pattern=north west lines, pattern color=blue, line width=1pt] (80,00) rectangle (120,1000);
%
    \draw[pattern=north east lines, pattern color=green, line width=1pt] (0,1000) rectangle (40,2000);
    \draw[pattern=north west lines, pattern color=green, line width=1pt] (40,1000) rectangle (80,2000);
    \draw[pattern=north east lines, pattern color=green, line width=1pt] (80,1000) rectangle (120,2000);
%
    \draw[pattern=north west lines, pattern color=red, line width=1pt] (0,2000) rectangle (40,3000);
    \draw[pattern=north east lines, pattern color=red, line width=1pt] (40,2000) rectangle (80,3000);
    \draw[pattern=north west lines, pattern color=red, line width=1pt] (80,2000) rectangle (120,3000);
%
    \draw[pattern=north east lines, pattern color=orange, line width=1pt] (0,3000) rectangle (40,4000);
    \draw[pattern=north west lines, pattern color=orange, line width=1pt] (40,3000) rectangle (80,4000);
    \draw[pattern=north east lines, pattern color=orange, line width=1pt] (80,3000) rectangle (120,4000);
    \pgfplotsinvokeforeach{1,...,12}
    {\pgfmathtruncatemacro{\X}{mod((#1-1),3)}
    \pgfmathtruncatemacro{\Y}{int((#1-1)/3+1)}
    \addplot[scatter,clip=true, clip marker paths=true,
        only marks, mark=*, samples=100,point meta=int(#1)             
    ]({20+40*\X+20*rand},{-500+1000*\Y+500*rand});}
\end{axis}
\draw[decoration={brace,mirror,raise=15pt},decorate]   (0,0) -- node[below=16pt] {\textbf{C}} (3.2,0);
\draw[decoration={brace,mirror},decorate]   (10.8,0) -- node[xshift=10pt] {\rotatebox{-90}{\textbf{band}}} (10.8,1.4);
\end{tikzpicture}
\end{document}

enter image description here

ORIGINAL ANSWER(for older versions of pgfplots, produces your screen shot from your code): I am not quite sure which rectangles you are talking about. This answer takes them to be the large ones, i.e. those which run from x=0 to x=12, but I will be happy to divide them for you. I also took n=100, which is controlled by samples=100.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    grid=major,
    width=\textwidth,
    height=\axisdefaultheight,
    xlabel = Time $(s)$,
    ylabel = Frequency,
    xmin = 0, xmax = 13,
    ymin = 0, ymax = 4000,
]

    \draw[pattern=north west lines, pattern color=blue, line width=1pt] (0,0) rectangle (40,100);
    \draw[pattern=north east lines, pattern color=blue, line width=1pt] (40,0) rectangle (80,100);
    \draw[pattern=north west lines, pattern color=blue, line width=1pt] (80,0) rectangle (120,100);

    \draw[pattern=north east lines, pattern color=green, line width=1pt] (0,100) rectangle (40,200);
    \draw[pattern=north west lines, pattern color=green, line width=1pt] (40,100) rectangle (80,200);
    \draw[pattern=north east lines, pattern color=green, line width=1pt] (80,100) rectangle (120,200);

    \draw[pattern=north west lines, pattern color=red, line width=1pt] (0,200) rectangle (40,300);
    \draw[pattern=north east lines, pattern color=red, line width=1pt] (40,200) rectangle (80,300);
    \draw[pattern=north west lines, pattern color=red, line width=1pt] (80,200) rectangle (120,300);

    \draw[pattern=north east lines, pattern color=orange, line width=1pt] (0,300) rectangle (40,400);
    \draw[pattern=north west lines, pattern color=orange, line width=1pt] (40,300) rectangle (80,400);
    \draw[pattern=north east lines, pattern color=orange, line width=1pt] (80,300) rectangle (120,400);
    \pgfplotsinvokeforeach{1,2,3,4}
    {\addplot[scatter,clip=true, clip marker paths=true,
        only marks, mark=*, samples=100,point meta=int(y/1000)             
    ]({6+6*rand},{-500+1000*#1+500*rand});}
\end{axis}
\draw[decoration={brace,mirror,raise=15pt},decorate]   (0,0) -- node[below=16pt] {\textbf{C}} (3.2,0);
\draw[decoration={brace,mirror},decorate]   (10.8,0) -- node[xshift=10pt] {\rotatebox{-90}{\textbf{band}}} (10.8,1.4);
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you so much, but whan trying to compile the following errors appear: Missing character: There is no . in font nullfont! Missing character: There is no 0 in font nullfont!

    What can I do?

    – Davide Pisanò Jun 13 '18 at 09:23
  • @DavidePisanò That's odd. Both codes run without any errors on my machine. But I have looked up the message here and made a small change in my first code. Does it run now on your machine? –  Jun 13 '18 at 12:29
  • No dice, the errors are still there, but the pdf is generated correctly. I'm using LyX 2.2.2 on debian 9.4 – Davide Pisanò Jun 13 '18 at 15:45
  • @DavidePisanò I guess the relevant information is which TeX version ou are using? TeXLive or MikTeX? Did you update them recently? (It is very hard to debug an error that I do not get.) –  Jun 13 '18 at 16:15
  • This is the output from LyX register: This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex 2018.5.28) 13 JUN 2018 18:40 entering extended mode restricted \write18 enabled. %&-line parsing enabled. ... LaTeX2e <2017/01/01> patch level 3

    They should be up to date, I reinstalled them 2 weeks ago

    – Davide Pisanò Jun 13 '18 at 16:42
  • 1
    @DavidePisanò Well, I guess you'd greatly benefit from updating your TeXLive distribution. You have a 2016 version but now is 2018. You should have a TeXLive manager somewhere and have to tell it to update. –  Jun 13 '18 at 16:46
  • Thank you fur the suggestion, now I'm struggling to update TeXLive since in Debian 9 the latest version in the official repository is the 2016 one. – Davide Pisanò Jun 13 '18 at 18:26
  • @DavidePisanò I guess you could ask a separate question on this. I myself work on MacOS and I am not at all an expert on these issues, but others are (yet do not see our conversation) and I am sure you will get a good answer. –  Jun 13 '18 at 18:28
  • Well, I'll do that for sure – Davide Pisanò Jun 13 '18 at 19:08