Search code examples
pythonplotlyscatter3d

How update 2nd plots axis plotly Scatter3D subplots


Good Day to you,

I'm trying to plot the Imaginary and Real part of a complex series over time with plotlies' Scatter3d. X shall be the Real part, Y the imaginary and z Time.

BUT I have two series and I would like to display the two resulting Scatterplots side by side for comparison. For that I am using the makesubplot function.

Now I would like to label each of the axis with their according name (see above). But I can only update the first subplot, not the second.

Here is my source code, that only updates the first subplot in row 1 col 1:

d_E1 = go.Scatter3d(
    x=df['Real_E1'],
    y=df['Im_E1'],
    z=df['Time'],
    )
d_E2 = go.Scatter3d(
    x=df['Real_E2'],
    y=df['Im_E2'],
    z=df['Time'],
    )

fig                 = make_subplots(   
                                    rows    = 1,  
                                    cols    = 2,
                                   specs    = [[{"type": "scene"}, {"type": "scene"}]],                                          
                                    )

fig.update_layout(scene = dict(
                  xaxis = dict( title='X AXIS TITLE'),
                  yaxis = dict( title='y AXIS TITLE'),
                  zaxis = dict( title='Z AXIS TITLE')
                ))
 
fig.add_trace(d_E1, row=1, col=1)
fig.add_trace(d_E2, row=1, col=2)
plotly.offline.plot(fig, filename='3d.html')

the result of the code from above

I have tried changing the fig.update_layout as follows, which of course didn't work:

1 This updates go.Scatter plots. I can run the code, but it changes nothing.

fig.update_xaxes(title_text="X AXIS TITLE", row=1, col=1)
fig.update_xaxes(title_text="X AXIS TITLE", row=1, col=2)

2 On Stackoverflow I found this:

fig['layout']['xaxis']['title']='Label x-axis 1'
fig['layout']['xaxis2']['title']='Label x-axis 2'
fig['layout']['yaxis']['title']='Label y-axis 1'
fig['layout']['yaxis2']['title']='Label y-axis 2'

The error is, that xaxis2 is that xaxis2 is not defined in this context!? the errormessage

3 On the same Website is a solution with a for loop, with this I can run the code, but it changes nothing... Plotly: How to add axis layouts into a subplot?

4 Then I tried this, where my compiler says he doesnt know xaxis2 :

fig.update_layout(scene = dict(
xaxis = dict( title='X AXIS TITLE'),
xaxis2 = dict( title='X AXIS TITLE'),
yaxis = dict( title='y AXIS TITLE'),
zaxis = dict( title='Z AXIS TITLE')))

I'm using plotly version 4.14.3


Solution

  • I just found it:

    https://github.com/plotly/plotly.py/issues/2483

    fig.update_scenes(
                          xaxis = dict( title_text='x-title'),
                          yaxis = dict( title_text='y-title'),
                          zaxis = dict( title_text='z-title'),
                    )