ap21
ap21

Reputation: 2614

Create a wrapper function for Plotly plotting functions

I am working on Jupyter notebook, and I am trying to create a wrapper function for the regular Plotly Scatter3d() function, with my own layout settings, so that I can call this directly every time I need to plot something, and save screen space.

BUT, this is not working. Nothing is getting displayed on the screen. Does anyone know why?

My code:

def BSplot3dPlotly(xyz):
xyz = np.reshape(xyz, (int(xyz.size/3), 3))

trace1 = go.Scatter3d(
    x=xyz[:,0],
    y=xyz[:,1],
    z=xyz[:,2],
    mode = 'markers', # lines+markers',
    #marker=Marker(color=Y, colorscale='Portland')
    marker=dict(
        size=12,
        line=dict(
            color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)

data = go.Data([trace1]) #[trace1]
layout = go.Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename=name)

Here the imput xyzis just a list containing x,y,z coordinates for some points.

Upvotes: 0

Views: 1422

Answers (2)

Maximilian Peters
Maximilian Peters

Reputation: 31649

  • You are defining a function BSplot3dPlotly but it doesn't return anything which might be the reason why you don't see anything.
  • Having line in the marker dict does not do anything. You would need to set mode to markers+lines to get both markers and lines and then use a separate line dict.

enter image description here

import numpy as np
import plotly.graph_objs as go
import plotly.plotly as py
import plotly.offline as offline

def scatter3d_wrapper(xyz):

    trace = go.Scatter3d(
        x=xyz[:,0],
        y=xyz[:,1],
        z=xyz[:,2],
        mode = 'markers+lines',
        marker=dict(
            color='rgb(255,0,0)',
            size=12
        ),
        line=dict(
            color='rgb(0, 0, 255)',
            width=10
        )
    )
    return trace

xyz = np.random.random((20, 3))

trace1 = scatter3d_wrapper(xyz)

data = go.Data([trace1])

fig = go.Figure(data=data)
offline.plot(fig, filename='wrapper.html')

Upvotes: 1

flyingmeatball
flyingmeatball

Reputation: 7997

For matplotlib, you have to run the following before you can see charts:

%matplotlib inline

Try that.

Upvotes: 0

Related Questions