First check which Plotly version is installed on your machine:
import plotly
plotly.__version__
'1.0.19'
If not version 1.0.19 or up, please upgrade using pip:
$ pip install plotly --upgrade
or
$ sudo pip install plotly --upgrade
Now, import the plotly
and tools
module and sign in using your credentials file:
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
py.sign_in(my_creds['username'], my_creds['api_key']) # (New syntax!) Plotly sign in
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.
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.)
A = 10 # choose a maximum amplitude
L = 4 # choose length of square domain
# Get coordinate arrays
z = fxy(A,L)
# Print shape of z
z.shape
(40, 40)
z.min(), z.max()
(2.0069089092572866e-32, 10.0)
Then, build a trace dictionary containing the surface plot type:
my_surface = dict(z=z, # z coords, a 2D array
type='surface', # N.B. 'surface' plot type
)
Make a correponding figure dictionary:
my_fig = dict(data=[my_surface]) # N.B. value link to 'data' must be a list
my_fig # print figure dictionary below
{'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,
py.plot(my_fig, validate=False, filename='test-3d-fxy')
u'https://plot.ly/~etpinard/265'
Where the above is the unique URL corresponding to our Plotly plot.
Or, inside an IPython notebook, use:
py.iplot(my_fig, validate=False, filename='test-3d-fxy')
About Plotly
Big thanks to
# CSS styling within IPython notebook
from IPython.core.display import HTML
def css_styling():
styles = open("../../python-user-guide/custom.css", "r").read()
return HTML(styles)
css_styling()