# More 3D plots¶

## with Plotly¶

First check which Plotly version is installed on your machine:

In [1]:
import plotly

plotly.__version__

Out[1]:
'1.0.19'

$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.¶

Let's try to make a Plotly version of this plot taken from Roberto Colistete's web page.
Big thanks!

The function to be plotted is:

$$f(x,y) = A \cos(\pi x y) e^{-(x^2+y^2)/2}$$

where $A$ is some number corresponding to the amplitude of the surface. So,

In [3]:
import numpy as np

def fxy(A, L):
"""
A is the amplitude of the curve
L is the length of a side of the square
"""

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

return A*(np.cos(np.pi*x*y))**2*np.exp(-(x**2+y**2)/2.)

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

# Get coordinate arrays
z = fxy(A,L)

In [5]:
# Print shape of z
z.shape

Out[5]:
(40, 40)
In [6]:
z.min(), z.max()

Out[6]:
(2.0069089092572866e-32, 10.0)

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

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


Make a correponding figure dictionary:

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

my_fig  # print figure dictionary below

Out[8]:
{'data': [{'type': 'surface',
'z': array([[ 0.18315639,  0.14568834,  0.02557518, ...,  0.03046634,
0.02557518,  0.14568834],
[ 0.14568834,  0.0310402 ,  0.02013064, ...,  0.21816952,
0.02013064,  0.0310402 ],
[ 0.02557518,  0.02013064,  0.20811508, ...,  0.45015744,
0.20811508,  0.02013064],
...,
[ 0.03046634,  0.21816952,  0.45015744, ...,  0.4919921 ,
0.45015744,  0.21816952],
[ 0.02557518,  0.02013064,  0.20811508, ...,  0.45015744,
0.20811508,  0.02013064],
[ 0.14568834,  0.0310402 ,  0.02013064, ...,  0.21816952,
0.02013064,  0.0310402 ]])}]}

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 [9]:
py.plot(my_fig, validate=False, filename='test-3d-fxy')

Out[9]:
u'https://plot.ly/~etpinard/265'

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

Or, inside an IPython notebook, use:

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

In [10]:



#### Notebook styling ideas

Big thanks to

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