Python Plotly中的三维曲面流

Python Plotly中的三维曲面流,python,plot,3d,plotly,surface,Python,Plot,3d,Plotly,Surface,我正在尝试使用Plotly的流式API生成3D曲面,实际Python代码中没有收到任何错误,但是我在Plotly上得到“Oops!加载此绘图数据时出错”。这是我的密码: 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_

我正在尝试使用Plotly的流式API生成3D曲面,实际Python代码中没有收到任何错误,但是我在Plotly上得到“Oops!加载此绘图数据时出错”。这是我的密码:

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='', api_key='')

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()

成功了,这是我的最终代码:

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()
导入plotly.plotly作为py
将plotly.tools作为tls导入
导入plotly.graph_objs作为go
将scipy.ndimage导入为ndimage
从随机导入制服
作为pd进口熊猫
将numpy作为np导入
导入时间
tls.set_凭证_文件(用户名=“”,api_密钥=“”)
stream\u id=tls.get\u凭证\u文件()['stream\u id']
令牌=流\u id[-1]
流\u id=dict(令牌=token)
z_init=np.零(100).重塑((10,10))
z=z_init
surface=go.surface(z=z,stream=stream_id)
数据=[表面]
布局=开始。布局(
title='Test',
autosize=False,
宽度=500,
高度=500,
页边(
l=65,
r=50,
b=65,
t=90
)
)
图=开始图(数据=数据,布局=布局)
plot\u url=py.iplot(图,文件名=“”,自动打开=真)
s=py.Stream(Stream\u id=token)
s、 开()
arr_宽度=25
arr_长度=25
矩阵=[]
对于范围(100)内的p:
矩阵=[]
对于范围内的x(阵列宽度):
行=[]
对于范围内的y(arr_长度):
行追加(统一(-1,1))
矩阵追加(行)
矩阵。追加(矩阵)
##################################################################
#给定2个数组arr1、arr2、数组之间的步数和插值顺序
#numpoints=10
#订单=2
#arr1=矩阵[1]
#arr2=矩阵[2]
def interp(arr1、arr2、NUMPOINT、订单):
#将arr1、arr2重新连接到单个形状数组(2、10、10)
arr=np.r_389;['0,3',arr1,arr2]
#定义要插值的栅格坐标
十、 Y=np.meshgrid(np.arange(arr_宽度),np.arange(arr_长度))
k=0
interp_arr_vec=[]

虽然这也可以在javascript中实现,但我无法想象对Python的支持会先于对JS的支持。事实上,我相信Python API依赖于JS。如果你问如何在JS中实现这一点,那么你可能会分享一些代码我有一个蓝牙串行通信设备,它通过蓝牙以低于300毫秒的速度发送z轴数据流,如何在基于javasciprt web view的android应用程序中使用plotly的surface plot功能通过javascript界面将数据流到javascript,并刷新图形数据而不丢失其相机位置我也不正确理解其流api。。。为什么我们需要使用api密钥通过蓝牙进行数据流传输,或者该流api不用于蓝牙目的?流传输本身与通过协议传输数据无关。这仅仅意味着数据是根据时间更新的。