Search code examples
pythongraphchartsplotly

Converting old plotly code to newer version.I don't know/understand how to do it


import plotly.plotly as py
from plotly.graph_objs import*


trace1=Scatter3d(x=Xe,y=Ye,z=Ze,mode='lines',line=Line(color='rgb(125,125,125)', width=1),hoverinfo='none')

trace2=Scatter3d(x=Xn,
               y=Yn,
               z=Zn,
               mode='markers',
               name='actors',
               marker=Marker(symbol='dot',
                             color=eigen,
                             size=6,colorbar=ColorBar(
                title='Colorbar'
            ),
                             colorscale='Viridis',
                             line=Line(color='rgb(158,18,130)', width=0.5)
                             ),
               text=labels,
               hoverinfo='text'
               )

axis=dict(showbackground=False,
          showline=False,
          zeroline=False,
          showgrid=False,
          showticklabels=False,
          title=''
          )

layout = Layout(
         title="3D Visualization of the Facebook nodes",
         width=1000,
         height=1000,
         showlegend=False,
         scene=Scene(
         xaxis=XAxis(axis),
         yaxis=YAxis(axis),
         zaxis=ZAxis(axis),
        ),
     margin=Margin(
        t=100
    ),
    hovermode='closest',
    annotations=Annotations([
           Annotation(
           showarrow=False,
#             text="Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1] miserables.json</a>",
            xref='paper',
            yref='paper',
            x=0,
            y=0.1,
            xanchor='left',
            yanchor='bottom',
            font=Font(
            size=14
            )
            )
        ]),    )

data=Data([trace1, trace2])
fig=Figure(data=data, layout=layout)

py.iplot(fig)

> Blockquote

I have this piece of a old plotly code for which I get a error saying that plotly has been migrated to chart studios, I just started a networks analysis project and for the visualization of the said/given data I already have it was done in an older version of plotly.Please help I saw plotly documentation and I don't understand the workings of it.


Solution

  • Two core recommendations

    1. use dictionaries instead of objects (i.e. no need for Margin, Data, etc)
    2. use standard import import plotly.graph_objects as go improves clarity of scope of an object

    I defined variables upfront that your code uses. dot is no longer a valid marker symbol replaced with square

    import plotly.graph_objects as go
    
    # define some input data...
    t = np.linspace(0, 10, 50)
    Xe, Ye, Ze = np.cos(t), np.sin(t), t
    Xn = Xe * 2
    Yn = Ye * 2
    Zn = Ze * 2
    eigen = t
    labels = ["a"]
    
    trace1 = go.Scatter3d(
        x=Xe,
        y=Ye,
        z=Ze,
        mode="lines",
        line=dict(color="rgb(125,125,125)", width=1),
        hoverinfo="none",
    )
    
    trace2 = go.Scatter3d(
        x=Xn,
        y=Yn,
        z=Zn,
        mode="markers",
        name="actors",
        marker=dict(
            symbol="square",
            color=eigen,
            size=6,
            colorbar=dict(title="Colorbar"),
            colorscale="Viridis",
            line=dict(color="rgb(158,18,130)", width=0.5),
        ),
        text=labels,
        hoverinfo="text",
    )
    
    axis = dict(
        showbackground=False,
        showline=False,
        zeroline=False,
        showgrid=False,
        showticklabels=False,
        title="",
    )
    
    layout = go.Layout(
        title="3D Visualization of the Facebook nodes",
        width=1000,
        height=1000,
        showlegend=False,
        scene=dict(
            xaxis=axis,
            yaxis=axis,
            zaxis=axis,
        ),
        margin={"t": 100},  # go.Margin(t=100),
        hovermode="closest",
        annotations=[
            dict(
                showarrow=False,
                text=
                "Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1] miserables.json</a>",
                xref="paper",
                yref="paper",
                x=0,
                y=0.1,
                xanchor="left",
                yanchor="bottom",
                font=dict(size=14),
            )
        ],
    )
    
    fig = go.Figure(data=[trace1, trace2], layout=layout)
    fig