3d surface plot in Plotly

In [36]:
import plotly.plotly as py
import numpy as np
from plotly.graph_objs import *

numpy review: Create a 5x1 array "y1t"

In [58]:
x1 = y1 = np.arange(-1,1,0.4)
yt1 = x1[:,np.newaxis]
print np.shape(yt1)
print '***'
print yt1
(5, 1)
***
[[-1. ]
 [-0.6]
 [-0.2]
 [ 0.2]
 [ 0.6]]

1x5 array * 1x5 array = 1x5 array

In [64]:
print x1, '-->', np.shape(x1)
print y1, '-->', np.shape(y1)
print x1*y1, '-->', np.shape(x1*y1)
[-1.  -0.6 -0.2  0.2  0.6] --> (5,)
[-1.  -0.6 -0.2  0.2  0.6] --> (5,)
[ 1.    0.36  0.04  0.04  0.36] --> (5,)

1x5 array * 5x1 array = 5x5 matrix

In [49]:
print x1*yt1
print '***'
print 
[[ 1.    0.6   0.2  -0.2  -0.6 ]
 [ 0.6   0.36  0.12 -0.12 -0.36]
 [ 0.2   0.12  0.04 -0.04 -0.12]
 [-0.2  -0.12 -0.04  0.04  0.12]
 [-0.6  -0.36 -0.12  0.12  0.36]]

Make the surface plot

In [38]:
x = y = np.arange(-5,5,0.1)
yt = x[:,np.newaxis]
z = np.cos(x*yt)+np.sin(x*yt)
In [10]:
np.size(z) # this is how many points we're plotting
Out[10]:
10000
In [34]:
%%capture
# capture to suppress help() output
# remeber you can always call help on Plotly objects
help(Surface)
In [39]:
# aquatic colorscale
cls=[[0, 'rgb(0,0,131)'], [0.125, 'rgb(0,60,170)'], [0.375, 'rgb(5,255,255)'], [0.625, 'rgb(255,255,0)'], [0.875, 'rgb(250,0,0)'], [1, 'rgb(128,0,0)']]

data = Data([ Surface( x=x, y=y, z=z, colorscale=cls ) ])

layout = Layout(
    title='waaaaves',
    autosize=True,
    scene=Scene( 
        zaxis=ZAxis(range=[-5, 5],domain=[-5, 5],autorange=False,nticks=10,gridcolor='rgb(255, 255, 255)',gridwidth=2,zerolinecolor='rgb(255, 255, 255)',zerolinewidth=2),
        xaxis=XAxis(nticks=10,gridcolor='rgb(255, 255, 255)',gridwidth=2,zerolinecolor='rgb(255, 255, 255)',zerolinewidth=2),   
        yaxis=YAxis(nticks=10,gridcolor='rgb(255, 255, 255)',gridwidth=2,zerolinecolor='rgb(255, 255, 255)',zerolinewidth=2),        
        bgcolor="rgb(244, 244, 248)"
    )
)

fig = Figure(data=data, layout=layout)

# As of Plotly's python package plotly-1.5.0
# 2d contours in 3d plots aren't supported, 
# but you can hack them in like this.
# Stayed tuned for updates.
contours = { 'z':{ 'show':True } }
fig['data'][0]['contours'] = contours

# validate = False prevents syntax checking that would complain about the 2d contour hack (see above)
py.iplot(fig, filename='waaaaves', validate=False)
Out[39]:

Add custom css - feel free to copy

In [46]:
from IPython.core.display import HTML
import urllib2
HTML( urllib2.urlopen('http://bit.ly/1Bf5Hft').read() )
Out[46]:
In [ ]: