0

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.

1
  • please show your import, and how is py defined. Are you using offline plot? Commented Feb 7, 2017 at 22:36

2 Answers 2

1
  • 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')
Sign up to request clarification or add additional context in comments.

1 Comment

So, I need to return the trace (and layout) from the function, instead of trying to plot from inside. Got it! Thanks.
0

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

%matplotlib inline

Try that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.