Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a 3d surface graph in plotly

Tags:

python

plotly

I have the following data:

enter image description here

I'm trying to plot x=long, y=short, z=balance

I've done it like this:

fig.add(go.Scatter3d(x=results['long'], y=results['short'], z=results['balance']))

and I get something like that:

enter image description here

what I am really looking for is a surface, like this example:

enter image description here

so, following the example's code, I did:

fig.add_surface(x=results['long'], y=results['short'], z=results['balance'], row=2, col=1)

but:

enter image description here

not only it doesn't display any data, it seems to totally ignore the subplot layout and draws over everything.

here is the complete code:

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

fig.add_trace(go.Scatter(x=series1['timestamp'], y=series1['data'], line_color='red', name='series 1'), row=1, col=1)
fig.add_trace(go.Scatter(x=series2['timestamp'], y=series2['data'], line_color='blue', name='series 2'), row=1, col=1)
fig.update_yaxes(title_text="USD", row=1, col=1)

fig.add_surface(x=results['long'], y=results['short'], z=results['balance'], row=2, col=1)
fig.show()

so, my questions are:

  • how can I create a smooth surface from the data I have?
  • what did I miss regarding the layout?
like image 573
Thomas Avatar asked Nov 16 '22 04:11

Thomas


1 Answers

Two answers:

  1. surface traces accept a 2-d matrix for z
  2. 3-d traces are plotted in a layout.scene object which is distinct from a 2-d cartesian subplot
like image 78
nicolaskruchten Avatar answered Dec 06 '22 21:12

nicolaskruchten