Show Sidebar Hide Sidebar

# Array

A NumPy array is like a Python list and is handled very similarly.

#### New to Plotly?¶

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


#### Create an Array¶

Very similar to the Python list object, a numpy array is an array for which data can be appended, removed, and can be reshaped. The data can be read according to other programming languages (eg. C, Fortran) and instantiated as all-zeros or as an empty array.

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

x = np.array([1, 2, 3])
y = np.array([4, 7, 2])

trace = go.Scatter(x=x, y=y)
py.iplot([trace], filename='numpy-array-ex1')

Out[2]:

#### Create an N-D Array¶

np.ndarray creates an array of a given shape and fills the array with garbage values.

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

nd_array = np.ndarray(shape=(2,3), dtype=float)
nd_array[0] = x
nd_array[1] = y

trace = go.Scatter(x=nd_array[0], y=nd_array[1])
py.iplot([trace], filename='numpy-ndarray-ex2')

Out[3]:
In [4]:
help(np.array)

Help on built-in function array in module numpy.core.multiarray:

array(...)
array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)

Create an array.

Parameters
----------
object : array_like
An array, any object exposing the array interface, an
object whose __array__ method returns an array, or any
(nested) sequence.
dtype : data-type, optional
The desired data-type for the array.  If not given, then
the type will be determined as the minimum type required
to hold the objects in the sequence.  This argument can only
be used to 'upcast' the array.  For downcasting, use the
.astype(t) method.
copy : bool, optional
If true (default), then the object is copied.  Otherwise, a copy
will only be made if __array__ returns a copy, if obj is a
nested sequence, or if a copy is needed to satisfy any of the other
requirements (dtype, order, etc.).
order : {'C', 'F', 'A'}, optional
Specify the order of the array.  If order is 'C', then the array
will be in C-contiguous order (last-index varies the fastest).
If order is 'F', then the returned array will be in
Fortran-contiguous order (first-index varies the fastest).
If order is 'A' (default), then the returned array may be
in any order (either C-, Fortran-contiguous, or even discontiguous),
unless a copy is required, in which case it will be C-contiguous.
subok : bool, optional
If True, then sub-classes will be passed-through, otherwise
the returned array will be forced to be a base-class array (default).
ndmin : int, optional
Specifies the minimum number of dimensions that the resulting
array should have.  Ones will be pre-pended to the shape as
needed to meet this requirement.

Returns
-------
out : ndarray
An array object satisfying the specified requirements.

--------
empty, empty_like, zeros, zeros_like, ones, ones_like, fill

Examples
--------
>>> np.array([1, 2, 3])
array([1, 2, 3])

Upcasting:

>>> np.array([1, 2, 3.0])
array([ 1.,  2.,  3.])

More than one dimension:

>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
[3, 4]])

Minimum dimensions 2:

>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

Type provided:

>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j,  2.+0.j,  3.+0.j])

Data-type consisting of more than one element:

>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])

Creating an array from sub-classes:

>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
[3, 4]])

>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
[3, 4]])


Still need help?