NEW PROJECT Sign in Sign up API libraries Python User guide 3d plots Fork on GitHub Plotly for Python - User Guide Section 8: 3D Plots Welcome to Plotly's Python API User Guide. Links to the other sections are on the User Guide's homepage If unfamiliar with Plotly's Python API version 1.0 see section 0 The Github repository is available here Section 8 is divided as follows: 8.1 A simple surface plot 8.2 A helix curve in 3D Quickstart (a 3d scatter plot of 3 points): >>> import plotly.plotly as py >>> # auto sign-in with credentials or use py.sign_in() >>> trace1 = dict( type='scatter3d', x=[1,2,3], y=[3,4,5], z=[1,3,4] ) >>> data = [trace1] >>> py.plot(data, validate=False) The User Guide assumes version 1.0 and up of Plotly's Python API. Check which version is installed on your machine and please upgrade if needed. In [1]: # (*) Import plotly package import plotly # Check plolty version (if not latest, please upgrade) plotly.__version__ Out[1]: '1.2.7' See the User Guide's homepage for more info on installation and upgrading. In this section, we introduce Plotly's 3D plot engine. 3D plots are a new featur e to Plotly and this section (in its currently form) only scratches the surface on its possibilities. We first import a few modules and sign in to Plotly using our credential file: In [2]: # (*) To communicate with Plotly's server, sign in with credentials file import plotly.plotly as py # (*) Useful Python/Plotly tools import plotly.tools as tls # (*) Graph objects to piece together plots from plotly.graph_objs import * import numpy as np # (*) numpy for math functions and arrays If you are not familiar with credentials files, refer to the User Guide's homepa ge . Important 3D plotting is currently not fully integrated into the latest version of Plotly 's Python API. Namely, there are currently no 3D graph objects and the avaiable keys are not do cumented. Therefore, surface and 3D scatter plots must be generated using standard Python dictionaries and lists and bypassing validation within Plotly's Python API (i.e. validate=False ). That said, if you are eager to start plotting in 3D, you can, right now! The fol lowing examples demonstrate how to do so. 8.1 A simple surface plot Let's try to make a Plotly version of this plot taken from Roberto Colistete's w eb page . Big thanks! The function to be plotted is: f(x,y)=Acos(pxy)e-(x2+y2)/2 where A is some number corresponding to the amplitude of the surface. So, In [3]: from numpy import pi, cos, exp # (*) import the functions needed # Define the function to be plotted def fxy(x, y): A = 1 # choose a maximum amplitude return A*(cos(pi*x*y))**2*exp(-(x**2+y**2)/2.) In [4]: # Choose length of square domain, make row and column vectors L = 4 x = y = np.arange(-L/2., L/2., 0.1) # use a mesh spacing of 0.1 yt = y[:,np.newaxis] # (!) make column vector # Get surface coordinates! z = fxy(x, yt) Next, build a trace dictionary containing the 'surface' plot type: In [5]: trace1 = dict( type='surface', # (!) 'surface' plot type z=z, # link the fxy 2d numpy array x=x, # link 1d numpy array of x coords y=y # link 1d numpy array of y coords ) # Package the trace dictionary into a data list data = [trace1] Add a title and customize the axes: In [6]: # Dictionary of style options for all axes axis = dict( showbackground=True, # (!) show axis background backgroundcolor="rgb(204, 204, 204)", # set background color to grey gridcolor="rgb(255, 255, 255)", # set grid line color zerolinecolor="rgb(255, 255, 255)", # set zero grid line color ) # Make a layout dictionary layout = dict( title='$f(x,y) = A \cos(\pi x y) e^{-(x^2+y^2)/2}$', # set plot title scene=dict( # (!) axes are part of a 'scene' in 3d plots xaxis=axis, # set x-axis style yaxis=axis, # set y-axis style zaxis=axis # set z-axis style ) ) Package layout and data into a figure dictionary and call Plotly: In [7]: # Make a figure dictionary fig = dict(data=data, layout=layout) # (@) Send to Plotly and show in notebook py.iplot(fig, validate=False, filename='s8_surface') Out[7]: A 3D interacting surface plot Plotly 3D plots have three modes of interaction (togglable from the top right co rner of the plot's frame): Rotation (leftmost button) Zoom (center button) Pan (rightmost button) You are invited to play around with each of them, Plotly 3D plots are alive like no other. Moreover, this surface plot can be viewed in full screen at the following unique URL: In [8]: py.plot(fig, validate=False, auto_open=False, filename='s8_surface') Out[8]: u'https://plot.ly/~etpinard/464' 8.2 A helix curve in 3D Our next plot will be a 3-dimensional helix curve. A helix curve is described mathematically in cartesian coordinates by: x(t)y(t)z(t)=cos(t)=sin(t)=t where t is some conitnuous variable. So consider, In [9]: from numpy import sin # (*) import sin (cos was imported in 8.1) # Define a function generating the helix coordinates def helix(t): x = cos(t) y = sin(t) z = t return x, y, z Next, get the coordinates of the helix: In [10]: # Make a linear space from 0 to 4pi (i.e. 2 revolutions), get coords t = np.linspace(0, 4*pi, 200) # (pi was imported in 8.1) x, y, z = helix(t) Build a trace dictionary with 'type' set to 'scatter3d' : In [11]: trace1 = dict( type='scatter3d', # (!) 'scatter3d' plot type x=x, # x coords y=y, # y coords z=z, # z coords mode='lines', # (!) draw lines between coords (as in Scatter) line=dict( color='black', # black line segments width=3 # set line segment width ) ) # Package the trace dictionary into a data list data = [trace1] Add a title: In [12]: # Make a layout dictionary layout = dict( title='Fig 8.2: Helix curve' ) And finally, In [13]: # Make a figure dictionary fig = dict(data=data, layout=layout) # (@) Send to Plotly and show in notebook py.iplot(fig, validate=False, filename='s8_helix') Out[13]: The full screen verison is available at the following URL: In [14]: py.plot(fig, validate=False, auto_open=False, filename='s8_helix') Out[14]: u'https://plot.ly/~etpinard/468' Go to Section 7 --- Plotly's Streaming API Go to top of page Go to User Guide's homepage Got Questions or Feedback? About Plotly email: feedback@plot.ly tweet: @plotlygraphs Notebook styling ideas Big thanks to Cam Davidson-Pilon Lorena A. Barba Product Jobs Team Terms Privacy Feedback plotly ^Contact us!