pgfplots:在表面上分布随机点

pgfplots:在表面上分布随机点

我想在表面上分布一些随机点。我的工作示例如下:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
view={10}{25},
ticks = none,
grid=major,
xlabel=$x$,
ylabel=$y$,
zlabel={$z$},
]

\addplot3[
surf,
opacity=0.8,
samples=50, samples y=30,
domain=-1:1,y domain=-1:1,
z buffer=sort,
]
{x^2+x};

\addplot3+[mesh,scatter,samples=10,domain=-1:1] {x^2+x};

\end{axis}
\end{tikzpicture}
\end{document}

但这不是我想要的,因为点不是随机分布的。另外,如果点是圆形的并且小得多就更好了。

答案1

我想,这就是你所寻找的,对吗?

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    % load the ColorBrewer library to use another colormap
    \usetikzlibrary{
        pgfplots.colorbrewer,
    }
    \pgfplotsset{
        compat=1.3,
        % declare some constants for the min and max values of the x and y axis
        % to be later used in the "random" columns and the `domain's for the
        % functions
        /pgf/declare function={
            xmin=-1;
            xmax=1;
            ymin=-1;
            ymax=1;
        },
    }
\begin{document}
        % because tikz/pgf(plots) can only produce pseudo-random numbers,
        % change the following number to produce other ones
        \pgfmathsetseed{7}
    % create a table that will later be used to plot the "random" points
    \pgfplotstableset{
        % create the random numbers for the x and y values
        % here they are declared generally, so it will also work when you will
        % change the limits of the x and y values
        create on use/randomx/.style={
            create col/expr={(xmax-xmin)*rnd + xmin}
        },
        create on use/randomy/.style={
            create col/expr={(ymax-ymin)*rnd + ymin}
        },
        % and here we calculate the corresponding `z' values
        % (adapt the expression, when the formula is changing)
        create on use/z/.style={
            create col/expr={(\thisrow{randomx})^2 + \thisrow{randomx}}
        },
    }
    % initialise a table with a certain number of rows and "fill it" with
    % the former created columns
    \pgfplotstablenew[
        columns={randomx,randomy,z},
    ]{100}\loadedtable       % <-- How many points to you want to plot?
% uncomment the following line to typeset the former table
%    \pgfplotstabletypeset{\loadedtable}
    \begin{tikzpicture}
        \begin{axis}[
            view={-20}{25},
%            view={0}{90},   % <-- to view from top how the random points are distributed
            ticks=none,
            grid=major,
            xlabel=$x$,
            ylabel=$y$,
            zlabel={$z$},
            % load another colormap
            colormap/YlGnBu,
        ]

            \addplot3[
                surf,
                opacity=0.8,
                samples=50,
                samples y=30,
                domain=xmin:xmax,
                y domain=ymin:ymax,
                z buffer=sort,
            ] expression {x^2+x};

            % plot the table with the "random" points
            \addplot3+ [
                only marks,
                mark=*,
                % change the mark size to a value that fits your needs
                mark size=1pt,
                % use `scatter' to plot the points in the same color
                % as the corresponding `surf' point
                scatter,
            ] table [
                x=randomx,
                y=randomy,
                z=z,
            ] {\loadedtable};
        \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容