Sterling Butters Sterling Butters - 13 days ago 7
Python Question

3D Surface Streaming in Plotly

I am trying to generate a 3D surface using Plotly's streaming API and I receive no errors in the actual Python code however I get the "Oops! An error occured while loading this plot's data" on Plotly. Here is my code:

import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go
from random import uniform
import pandas as pd
import time

tls.set_credentials_file(username='sterlingbutters', api_key='2dc5zzdbva')

stream_id = tls.get_credentials_file()['stream_ids']
token = stream_id[-1]

stream_id = dict(token=token)

z = []

surface = go.Surface(z=z, stream=stream_id)
data = [surface]
layout = go.Layout(
title='Test',
autosize=False,
width=500,
height=500,
margin=dict(
l=65,
r=50,
b=65,
t=90
)
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.iplot(fig, filename='elevations-3d-surface', auto_open=True)

s = py.Stream(stream_id=token)
s.open()

matrices = []
for p in range(5):
matrix = []
for x in range(25):
row = []
for y in range(25):
row.append(uniform(25, 100))
matrix.append(row)
test = pd.DataFrame(matrix)
print(test)
matrices.append(matrix)
print(pd.DataFrame(matrices[1]))

i = 0
while True:
step = 3
z = matrices[i]
s.write(go.Surface(z=z))
time.sleep(step)
i += 1
if i == len(matrices):
i = 0
print(i)
# print(pd.DataFrame(z))
s.close()

Answer

Got it work, here is my final code:

import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go
import scipy.ndimage as ndimage
from random import uniform
import pandas as pd
import numpy as np
import time

tls.set_credentials_file(username='', api_key='')

stream_id = tls.get_credentials_file()['stream_ids']
token = stream_id[-1]

stream_id = dict(token=token)

z_init = np.zeros(100).reshape((10, 10))
z = z_init

surface = go.Surface(z=z, stream=stream_id)
data = [surface]
layout = go.Layout(
     title='Test',
     autosize=False,
     width=500,
     height=500,
     margin=dict(
         l=65,
         r=50,
         b=65,
         t=90
     )
 )
fig = go.Figure(data=data, layout=layout)
plot_url = py.iplot(fig, filename='', auto_open=True)

s = py.Stream(stream_id=token)
s.open()

arr_width = 25
arr_length = 25

matrices = []
for p in range(100):
    matrix = []
    for x in range(arr_width):
        row = []
        for y in range(arr_length):
            row.append(uniform(-1, 1))
        matrix.append(row)
    matrices.append(matrix)

##################################################################

# given 2 arrays arr1, arr2, number of steps between arrays, and order of interpolation
# numpoints = 10
# order = 2
# arr1 = matrices[1]
# arr2 = matrices[2]


def interp(arr1, arr2, numpoints, order):

    # rejoin arr1, arr2 into a single array of shape (2, 10, 10)
    arr = np.r_['0, 3', arr1, arr2]

    # define the grid coordinates where you want to interpolate
    X, Y = np.meshgrid(np.arange(arr_width), np.arange(arr_length))

    k = 0
    interp_arr_vec = []
    while k <= 1:
        coordinates = np.ones((arr_width, arr_length))*k, X, Y
        # given arr interpolate at coordinates
        interp_arr = ndimage.map_coordinates(arr, coordinates, order=order).T
        interp_arr_vec.append(interp_arr)

        step = 1 / numpoints
        k += step

    return interp_arr_vec

##################################################################

sleep_time = .1

i = 0
while True:

    between_test = interp(matrices[i], matrices[i+1], 200, 3)

    r = 0
    for r in range(len(between_test)):
        s.write(go.Surface(z=between_test[r]))
        time.sleep(sleep_time)

    i += 1
    print('i = ', i)
    time.sleep(3)

    if i == len(matrices):
        i = 0

s.close()