データに基づく条件付きプロット

データに基づく条件付きプロット

以下の MWE で、マーカーをではなく*円の内側 (つまり) に配置する方法はありますか?\x^2 + \y^2 <= 1x

\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

完全な改訂: ポイント メタを使用すると非常に簡単になります。2 つのクラスを定義し、ポイント メタを適切に設定するだけです。コードを参照してください。

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

ここに画像の説明を入力してください

以下は、より複雑なアーティファクトです。

2 つのプロットを作成してクリップします。

\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

ここでは、最初にテーブルにランダムデータを作成し、それをプロットするソリューションを紹介します。作成されたポイントを内部そしてフィルタリングによって円を簡単に作成できます。

主な違いはマーモットの答え私のソリューションでは、指定された数のポイントのみが印刷されますが、marmots answerでは、指定された数のポイントが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}

上記コードの結果を示す画像

関連情報