
在下面的 MWE 中,有沒有辦法讓標記*
位於圓圈內(即\x^2 + \y^2 <= 1
)而不是x
?
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[anchor=origin, axis equal image, xmin=-1,xmax=1, xlabel={$x$},
ylabel={$y$}, ymin=-1,ymax=1, ]
\draw[thick] (-1,-1) -- (1,-1) -- (1,1) -- (-1,1) -- (-1,-1);
\draw[thick] (0,0) circle (1);
\addplot [only marks, mark=x, samples=500]
({rand}, {rand} );
\end{axis}
\end{tikzpicture}%
\end{document}
答案1
完成修訂:我對點元非常簡單。只需要定義兩個類別並適當設定點元,請參閱程式碼。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[anchor=origin, axis equal image, xmin=-1,xmax=1, xlabel={$x$},
ylabel={$y$}, ymin=-1,ymax=1, ]
\draw[thick] (-1,-1) -- (1,-1) -- (1,1) -- (-1,1) -- (-1,-1);
\draw[thick] (0,0) circle (1);
\clip (-1,-1) rectangle (1,1); % to prevent the marks from overshooting
\addplot[scatter,clip=true, clip marker paths=true,
only marks, mark=*, samples=500,
scatter/classes={0={mark=*,blue},
1={mark=x,red}},point meta=int(sqrt(x^2+y^2)),
]({rand},{rand});
\end{axis}
\end{tikzpicture}%
\end{document}
以下只是更複雜的工件。
我會製作兩個圖並剪輯它們。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[anchor=origin, axis equal image, xmin=-1,xmax=1, xlabel={$x$},
ylabel={$y$}, ymin=-1,ymax=1, ]
\draw[thick] (-1,-1) -- (1,-1) -- (1,1) -- (-1,1) -- (-1,-1);
\draw[thick] (0,0) circle (1);
\begin{scope}
\clip (0,0) circle (1) (-1,-1) rectangle (1,1);
\addplot+[clip=true, clip marker paths=true,only marks, mark=*, samples=500]({rand},{rand});
\end{scope}
\clip (0,0) circle (1);
\addplot+[clip=true, clip marker paths=true,only marks, mark=x, samples=500]
({rand},{rand});
\end{axis}
\end{tikzpicture}%
\end{document}
更新:或者您使用散佈圖的功能。我只是關閉圓圈內/外的標記。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\edef\Star{*}
\edef\X{x}
\begin{tikzpicture}
\tikzset{scatter/@pre marker code/.append style={/tikz/mark
size=\perpointmarksize}}
\begin{axis}
[anchor=origin, axis equal image, xmin=-1,xmax=1, xlabel={$x$},
ylabel={$y$}, ymin=-1,ymax=1, ]
\draw[thick] (-1,-1) -- (1,-1) -- (1,1) -- (-1,1) -- (-1,-1);
\draw[thick] (0,0) circle (1);
\addplot[scatter,
scatter/use mapped color={
draw=black,
fill=blue}, only marks, mark=*, samples=500,
visualization depends on={int(sqrt(x^2+y^2)) \as \rad},
scatter/@pre marker code/.append style=
{/tikz/mark size=2*\rad}]({rand},{rand});
\addplot[scatter,
scatter/use mapped color={
draw=red,
fill=red}, only marks, mark=x, samples=500,
visualization depends on={int(2-sqrt(x^2+y^2)) \as \rad},
scatter/@pre marker code/.append style=
{/tikz/mark size=2*\rad}]({rand},{rand});
\end{axis}
\end{tikzpicture}%
\end{document}
這樣符號就不會被切斷。您仍然可以根據邊界框進行剪輯,例如使用第一個範例。
答案2
在這裡,我提出一個解決方案,首先在表中建立隨機數據,然後用於繪製它們。將創建的點分開裡面和外部然後可以透過過濾輕鬆完成圓圈。
主要區別為土撥鼠的回答是我的解決方案實際上只列印指定數量的點,而在土撥鼠中,答案是創建指定數量的點的兩倍,然後「過濾」圓內和圓外。但由於使用了隨機數據,因此結果圖中不太可能準確顯示指定點的數量。所以問題是,如果你需要確切地是否達到指定的點數...
詳細內容請看程式碼中的註解。
% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{
% use this `compat' level or higher so circles use axis coordinates
% as "units" when a radius is given without a unit
compat=1.11,
}
% create a table containing the random data
\pgfplotstablenew[
% create a column for the x data
create on use/x/.style={
create col/expr={rand}
},
% create a column for the y data
create on use/y/.style={
create col/expr={rand}
},
% create a column for the vector length
create on use/veclen/.style={
create col/expr={sqrt( (\thisrow{x})^2 + (\thisrow{y})^2 )}
},
% "load" the columns which shall be used
columns={
x,
y,
veclen%
},
% replace the "500" to the number of sample points you want to have
]{500}{\datatable}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal image,
xmin=-1,
xmax=1,
ymin=-1,
ymax=1,
xlabel={$x$},
ylabel={$y$},
% moved common options here
only marks,
]
% state the radius of the circle here
\pgfmathsetmacro{\CircleRadius}{1}
\draw [thick] (0,0) circle (\CircleRadius);
% add values inside the circle
\addplot table [
x=x,
y expr={
ifthenelse(
\thisrow{veclen} <= \CircleRadius,
\thisrow{y},
NaN
)
}
] {\datatable};
% add values outside the circle
\addplot table [
x=x,
y expr={
ifthenelse(
\thisrow{veclen} > \CircleRadius,
\thisrow{y},
NaN
)
}
] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}