我想用 tikz 和 pgfplots 制作一个蒙特卡罗图,类似于这:
我已经尝试根据这个帖子进行调整:
我尝试将四分之一圆定义为函数 (y=sqrt(1-x^2)。看起来有点丑。
因此我尝试了 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}
我只能做到这些了。如何才能在其他区域得到红点?我只想要一个简单的情节,不要动画。
答案1
您还可以生成点并使用 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}
干杯!
答案2
您可以在范围的帮助下使用两种不同的剪辑:
\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}