Search code examples
pythonpython-3.x3dplotlyplotly-python

Plotly: How to put two 3D graphs on the same plot with plotly.graph_objects?


In below code, I draw 2 3D graphs with plotly.graph_objects. I'm unable to put them together.

import plotly.graph_objects as go
import numpy as np

pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
x, y, z = pts.T

### First graph
fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z,
                   alphahull=5,
                   opacity=0.4,
                   color='cyan')])
fig.show()
#######

x = [0, 1, 0]
y = [0, 2, 3]
tvects = [x,y]
orig = [0,0,0]
df=[]
coords = [[orig, np.sum([orig, v],axis=0)] for v in tvects]

for i,c in enumerate(coords):
    X1, Y1, Z1 = zip(c[0])
    X2, Y2, Z2 = zip(c[1])
    vector = go.Scatter3d(x = [X1[0],X2[0]],
                          y = [Y1[0],Y2[0]],
                          z = [Z1[0],Z2[0]],
                          marker = dict(size = [0,5],
                                        color = ['blue'],
                                        line=dict(width=5,
                                                  color='DarkSlateGrey')),
                          name = 'Vector'+str(i+1))
    data.append(vector)

### Second graph
fig = go.Figure(data=data)
fig.show()
########

Could you please elaborate on how to do so, and how to have the arrow with head rather than a line in the second graph?


Solution

  • If you'd like to build on your first fig definition, just include the following after your first call to go.Figure()

    data = fig._data
    

    data is now a list which will have new elements appended to it in the rest of your already exising code under:

    for i,c in enumerate(coords):
        X1, Y1, Z1 = zip(c[0])
        X2, Y2, Z2 = zip(c[1])
        vector = go.Scatter3d(<see details in snippet below>)
        data.append(vector)
    

    Result:

    enter image description here

    Regarding the arrows, the only options dierectly available to you through go.Scatter3D are:

    ['circle', 'circle-open', 'square', 'square-open','diamond', 'diamond-open', 'cross', 'x']
    

    I hope one of those options will suit your needs. You can specify which one in:

    marker = dict(size = [15,15],
                color = ['blue'],
                symbol = 'diamond',
                line=dict(width=500,
                          #color='red'
                         )),
    

    Complete code:

    import plotly.graph_objects as go
    import numpy as np
    
    pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
    x, y, z = pts.T
    
    ### First graph
    fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z,
                       alphahull=5,
                       opacity=0.4,
                       color='cyan')])
    #fig.show()
    #######
    
    data = fig._data
    
    x = [0, 1, 0]
    y = [0, 2, 3]
    tvects = [x,y]
    orig = [0,0,0]
    df=[]
    coords = [[orig, np.sum([orig, v],axis=0)] for v in tvects]
    
    # ['circle', 'circle-open', 'square', 'square-open','diamond', 'diamond-open', 'cross', 'x']
    
    for i,c in enumerate(coords):
        X1, Y1, Z1 = zip(c[0])
        X2, Y2, Z2 = zip(c[1])
        vector = go.Scatter3d(x = [X1[0],X2[0]],
                              y = [Y1[0],Y2[0]],
                              z = [Z1[0],Z2[0]],
                              marker = dict(size = [15,15],
                                            color = ['blue'],
                                            symbol = 'diamond',
                                            line=dict(width=500,
                                                      #color='red'
                                                     )),
                              name = 'Vector'+str(i+1))
        data.append(vector)
    
    ### Second graph
    fig = go.Figure(data=data)
    fig.show()
    ########