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'

If not version 1.0.17 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:

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

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 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
z = makeGaussian(A,L)
In [5]:
# Print shape of z
z.shape 
Out[5]:
(80, 80)

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

In [6]:
my_surface = dict(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',
   '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.

2. 3D random scatter

Next, let's make a 3D scatter plot of random numbers

In [10]:
my_scatter3d = dict(x=np.random.rand(50),  # x coords 
                    y=np.random.rand(50),  # y coords 
                    z=np.random.rand(50),  # z coords 
                    type='scatter3d',      # N.B. 'scatter3d' plot type
                    )
In [11]:
my_fig = dict(data=[my_scatter3d])  # N.B. value link to 'data' must be a list

my_fig
Out[11]:
{'data': [{'type': 'scatter3d',
   'x': array([ 0.90891063,  0.66495489,  0.42536839,  0.73446836,  0.92028244,
           0.15901418,  0.95075856,  0.08652361,  0.60609082,  0.62593319,
           0.67771766,  0.72482683,  0.7431216 ,  0.06357979,  0.56048281,
           0.23627947,  0.66447963,  0.51306179,  0.70917052,  0.77437643,
           0.5170396 ,  0.62786638,  0.43246615,  0.80702958,  0.74298939,
           0.94450533,  0.73586829,  0.17058953,  0.30408492,  0.36281235,
           0.26037782,  0.53388127,  0.401091  ,  0.54285035,  0.12482553,
           0.76104236,  0.67894581,  0.80438958,  0.4528201 ,  0.02626623,
           0.55937506,  0.25304773,  0.57409613,  0.28472371,  0.22327665,
           0.74942565,  0.45285706,  0.14486434,  0.42879803,  0.21545253]),
   'y': array([ 0.16491877,  0.42499136,  0.40704121,  0.91350253,  0.59336859,
           0.01719267,  0.16700364,  0.64304314,  0.55497295,  0.28612511,
           0.78403632,  0.72630749,  0.26845661,  0.27790411,  0.32381544,
           0.44838693,  0.68614402,  0.13128439,  0.84270861,  0.8992895 ,
           0.65559962,  0.36223201,  0.54981769,  0.7489496 ,  0.56760058,
           0.25887289,  0.03351865,  0.64961158,  0.97513919,  0.79645385,
           0.02564121,  0.99391304,  0.52128273,  0.3237757 ,  0.220231  ,
           0.49120419,  0.10130706,  0.62622458,  0.25782746,  0.68324434,
           0.90646088,  0.58050968,  0.19334138,  0.93992663,  0.77481758,
           0.9418977 ,  0.72775942,  0.2439782 ,  0.07899829,  0.13749408]),
   'z': array([ 0.64323269,  0.80533082,  0.07076433,  0.16011959,  0.87599579,
           0.26219927,  0.12650942,  0.64844385,  0.4876765 ,  0.07399142,
           0.86502568,  0.1456915 ,  0.41047598,  0.03456356,  0.91468817,
           0.80083334,  0.77151516,  0.62834246,  0.37431752,  0.94174073,
           0.53531856,  0.46071303,  0.98216146,  0.36411092,  0.89080965,
           0.91281308,  0.71035728,  0.22081817,  0.54085976,  0.94140168,
           0.30783964,  0.12436135,  0.52390685,  0.96123641,  0.65408168,
           0.71531631,  0.75431791,  0.19176365,  0.66286671,  0.53331073,
           0.92744481,  0.67545827,  0.03196769,  0.89444475,  0.66895663,
           0.38314529,  0.03310145,  0.11261851,  0.02207369,  0.50573115])}]}
In [12]:
py.plot(my_fig, validate=False, filename='test-3d-scatter')
Out[12]:
u'https://plot.ly/~etpinard/263'
In [13]:
py.iplot(my_fig, validate=False, filename='test-3d-scatter')

Got Questions or Feedback?

About Plotly

  • email: [email protected]
  • tweet: @plotlygraphs

Notebook styling ideas

Big thanks to


In [14]:
# 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()
Out[14]:
In [14]: