# 3D plots¶

## with Plotly¶

First check which Plotly version is installed on your machine:

In [1]:
import plotly

plotly.__version__

Out[1]:
'1.0.17'

$pip install plotly --upgrade  or $ sudo pip install plotly --upgrade

Now, import the plotly and tools module and sign in using your credentials file:

In [2]:
import plotly.plotly as py  # (New syntax!) tools to communicate with Plotly's server
import plotly.tools as tls  # (NEW!) useful Python/Plotly tools

my_creds = tls.get_credentials_file()                  # read credentials


Graph objects for 3d plots have yet to be inserted in the latest version of the Plotly package; hence, we will use standard Python dictionaries and list to create our figure in this notebook.

The following example is just snippet of things to come.

### 1. A 3D Gaussian surface¶

Let's start by making a simple 3D Gaussian surface (the below function was inspired by this stackoverflow answer, thanks).

In [3]:
import numpy as np

def makeGaussian(A, L, fwhm = 3, center=None):
""" Make a square gaussian kernel.

A is the amplitude of the curve
L is the length of a side of the square
fwhm is full-width-half-maximum, which
can be thought of as an effective radius.
"""

x = np.arange(0, L, 0.1, float)
y = x[:,np.newaxis]

if center is None:
x0 = y0 = L // 2
else:
x0 = center[0]
y0 = center[1]

return (x, y, A*np.exp(-4*np.log(2) * ((x-x0)**2 + (y-y0)**2) / fwhm**2))

In [4]:
A = 10  # choose a maximum amplitude
L = 8   # choose length of square domain

# Get coordinate arrays
x,y,z = makeGaussian(A,L)

In [5]:
# Print shape of x,y and z
x.shape, y.shape, z.shape

Out[5]:
((80,), (80, 1), (80, 80))

Then, build a trace dictionary containing the surface plot type:

In [6]:
my_surface = dict(x=x,  # x coords (optional)
y=y,  # y coords (optional)
z=z,  # z coords, a 2D array
type='surface', # N.B. 'surface' plot type
)


Make a correponding figure dictionary:

In [7]:
my_fig = dict(data=[my_surface])  # N.B. value link to 'data' must be a list

my_fig  # print figure dictionary below

Out[7]:
{'data': [{'type': 'surface',
'x': array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,
2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,
3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,
4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,  5.2,  5.3,  5.4,
5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,  6.3,  6.4,  6.5,
6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,  7.4,  7.5,  7.6,
7.7,  7.8,  7.9]),
'y': array([[ 0. ],
[ 0.1],
[ 0.2],
[ 0.3],
[ 0.4],
[ 0.5],
[ 0.6],
[ 0.7],
[ 0.8],
[ 0.9],
[ 1. ],
[ 1.1],
[ 1.2],
[ 1.3],
[ 1.4],
[ 1.5],
[ 1.6],
[ 1.7],
[ 1.8],
[ 1.9],
[ 2. ],
[ 2.1],
[ 2.2],
[ 2.3],
[ 2.4],
[ 2.5],
[ 2.6],
[ 2.7],
[ 2.8],
[ 2.9],
[ 3. ],
[ 3.1],
[ 3.2],
[ 3.3],
[ 3.4],
[ 3.5],
[ 3.6],
[ 3.7],
[ 3.8],
[ 3.9],
[ 4. ],
[ 4.1],
[ 4.2],
[ 4.3],
[ 4.4],
[ 4.5],
[ 4.6],
[ 4.7],
[ 4.8],
[ 4.9],
[ 5. ],
[ 5.1],
[ 5.2],
[ 5.3],
[ 5.4],
[ 5.5],
[ 5.6],
[ 5.7],
[ 5.8],
[ 5.9],
[ 6. ],
[ 6.1],
[ 6.2],
[ 6.3],
[ 6.4],
[ 6.5],
[ 6.6],
[ 6.7],
[ 6.8],
[ 6.9],
[ 7. ],
[ 7.1],
[ 7.2],
[ 7.3],
[ 7.4],
[ 7.5],
[ 7.6],
[ 7.7],
[ 7.8],
[ 7.9]]),
'z': array([[ 0.00052322,  0.00066739,  0.00084606, ...,  0.00106596,
0.00084606,  0.00066739],
[ 0.00066739,  0.00085128,  0.00107918, ...,  0.00135968,
0.00107918,  0.00085128],
[ 0.00084606,  0.00107918,  0.00136808, ...,  0.00172368,
0.00136808,  0.00107918],
...,
[ 0.00106596,  0.00135968,  0.00172368, ...,  0.0021717 ,
0.00172368,  0.00135968],
[ 0.00084606,  0.00107918,  0.00136808, ...,  0.00172368,
0.00136808,  0.00107918],
[ 0.00066739,  0.00085128,  0.00107918, ...,  0.00135968,
0.00107918,  0.00085128]])}]}

We are now ready to send the figure dictionary (or figure object) to Plotly. As graph objects for 3D plots are missing for the current release of the Plotly package, we must turn off the automatic key-value validation by adding the validate=False keyword argument in the py.plot() call. Otherwise, we would get an error and no plot.

So,

In [8]:
py.plot(my_fig, validate=False, filename='test-3d')

Out[8]:
u'https://plot.ly/~etpinard/234'

Where the above is the unique URL corresponding to our Plotly plot.

Or, inside an IPython notebook, use:

In [9]:
py.iplot(my_fig, validate=False, filename='test-3d')


And there you go. An interactive 3D plot from Plotly.

#### Notebook styling ideas

Big thanks to

In [10]:
# CSS styling within IPython notebook
from IPython.core.display import HTML
def css_styling():