5

I would like to produce a Monte-Carlo-Plot with tikz and pgfplots, similar to this:

enter image description here

I've already tried to adapt from this thread:

Filling specified area by random dots in TikZ

I've tried defining the quarter-circle as a function (y=sqrt(1-x^2). Looks kind of ugly.

So I've tried the method submitted by Claudio Fiandrino.

\begin{tikzpicture}

\draw [-latex, thick] (0, 0) -- (0, 6) node  [above] {\Large{$y$}};
\draw [-latex, thick] (0, 0) -- (6, 0) node  [right] {\Large{$x$}};
\draw [thick] (0, 5) -- (5, 5);
\draw [thick] (5, 0) -- (5, 5);

\draw [thick] (5,0 ) arc (0:90:5);

\clip (5, 0) arc (0:90:5)--++(5,0) arc(160:320:0); 
\drawdiagram[
 num points=600,
 use points equal width,
 point width=0.5pt,
 point fill color=blue,
 random point diagram] at (0, 0){};
 \end{tikzpicture}

This is as far as I got. How do I get red dot in the other area? I just want a plain plot, no animation.

1

2 Answers 2

5

You can also generate points and evaluate them with pgfmathparse.

\documentclass{article}
\usepackage[natural, table, fixpdftex, hyperref]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,matrix,calc}

%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2pt}%
%%%>

\def\x{0}
\def\y{0}
\def\k{0}
\def\radius{5}

\begin{document}
\begin{tikzpicture}
    \draw[fill=gray, opacity=0.1] (\radius,0) arc(0:90:\radius) -- (0,0) -- cycle;
    \draw[gray, opacity=0.25] (0,0) rectangle (\radius,\radius);
    \draw[->] (0,0) -- (1.1*\radius,0);
    \draw[->] (0,0) -- (0,1.1*\radius);
    \foreach \i in {1,2,...,1000}{%
        \typeout{Point \i}%
        \pgfmathsetmacro\x{\radius*rnd}%
        \typeout{X \x}%
        \pgfmathsetmacro\y{\radius*rnd}%
        \typeout{Y \y}%
        \pgfmathsetmacro\k{(pow(\x,2)+pow(\y,2)) <pow(\radius,2)}%
        \typeout{im Kreis?: \k}%
        \pgfmathparse{ifthenelse(\k==1,"red","blue")}%
        \fill[\pgfmathresult] (\x,\y)circle(0.5pt);%
    }
\end{tikzpicture}
\end{document}

Result

cheers!

1
2

You can use two different clipping with the help of scopes:

\begin{tikzpicture}

\draw [-latex, thick] (0, 0) -- (0, 6) node  [above] {\Large{$y$}};
\draw [-latex, thick] (0, 0) -- (6, 0) node  [right] {\Large{$x$}};
\draw [thick] (0, 5) -- (5, 5);
\draw [thick] (5, 0) -- (5, 5);

\draw [thick] (5,0 ) arc (0:90:5);

\begin{scope}
    \clip (5, 0) arc (0:90:5)--++(5,0) arc(160:320:0); 
    \node[
         num points=600,
         use points equal width,
         point width=0.5pt,
         point fill color=blue,
         random point diagram] at (0, 0){};
\end{scope}
\begin{scope}
    \clip (5, 0) arc (0:90:5)  -- (0,0) -- cycle; 
    \node[
         num points=600,
         use points equal width,
         point width=0.5pt,
         point fill color=red,
         random point diagram] at (0, 0){};
\end{scope}
\end{tikzpicture}   
1
  • Please complete the snippet to a full document example. May 12, 2015 at 14:39

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.