3

I want to distribute some random points on a surface. My working example looks like this:

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

But this isn't what I'm looking for because the points are not distributed randomly. Also it would be nice to have the points round and a lot smaller.

1

I think, this is, what you are searching for, right?

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

image showing the result of above code

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.