矩形内的随机点

矩形内的随机点

我制作了一个图表,如下图所示:

Plotted graphs

对于我想要生成的每个矩形n其中的随机点,可能吗?

我的代码:

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

答案1

编辑:我意识到我原来的答案(如下)不适用于较新版本的 pgfplots(因为与您用于矩形的明确坐标之间存在一些不匹配xmaxymax我修复了这个问题,并且将矩形分成更小的部分。点的数量由控制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

原始答案(对于旧版本的 pgfplots,从您的代码生成屏幕截图):我不太确定您说的是哪个矩形。这个答案将它们视为较大的矩形,即从 到 的矩形x=0x=12但我很乐意为您划分它们。我还取了n=100,它由 控制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

相关内容