Show Sidebar Hide Sidebar

Randn

How to sample numbers from the normal distribution.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Imports

This tutorial imports Plotly and Numpy.

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

Randn

np.random.randn() allows you to sample from the normal distribution. It can take an integer or a shape for its input.

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

num_of_points = 200
normal_pts_x = np.random.randn(num_of_points)
normal_pts_y = np.random.randn(num_of_points)

trace1 = go.Scatter(
    x=normal_pts_x,
    y=normal_pts_y,
    mode='markers',
    marker = dict(
        size=11,
        color=[normal_pts_x[i] * normal_pts_y[i] for i in range(len(normal_pts_x))],
        colorscale='Portland'
    ),
    name='Normally Sampled Points'
)

py.iplot([trace1], filename='numpy-randn')
Out[2]:
In [2]:
help(np.random.randn)
Help on built-in function randn:

randn(...)
    randn(d0, d1, ..., dn)
    
    Return a sample (or samples) from the "standard normal" distribution.
    
    If positive, int_like or int-convertible arguments are provided,
    `randn` generates an array of shape ``(d0, d1, ..., dn)``, filled
    with random floats sampled from a univariate "normal" (Gaussian)
    distribution of mean 0 and variance 1 (if any of the :math:`d_i` are
    floats, they are first converted to integers by truncation). A single
    float randomly sampled from the distribution is returned if no
    argument is provided.
    
    This is a convenience function.  If you want an interface that takes a
    tuple as the first argument, use `numpy.random.standard_normal` instead.
    
    Parameters
    ----------
    d0, d1, ..., dn : int, optional
        The dimensions of the returned array, should be all positive.
        If no argument is given a single Python float is returned.
    
    Returns
    -------
    Z : ndarray or float
        A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from
        the standard normal distribution, or a single such float if
        no parameters were supplied.
    
    See Also
    --------
    random.standard_normal : Similar, but takes a tuple as its argument.
    
    Notes
    -----
    For random samples from :math:`N(\mu, \sigma^2)`, use:
    
    ``sigma * np.random.randn(...) + mu``
    
    Examples
    --------
    >>> np.random.randn()
    2.1923875335537315 #random
    
    Two-by-four array of samples from N(3, 6.25):
    
    >>> 2.5 * np.random.randn(2, 4) + 3
    array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],  #random
           [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]]) #random

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.