Search code examples
pythondataframematplotlibplotlyplotly-python

Unable to reproduce Matplotlib 3D surface with Plotly


I'm trying to recreate a matplotlib surface plot with Plotly.

The matplotlib code:

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.plot_trisurf(
data3["strike"],
dates.date2num(data3["expiration"]),
data3["GEX"],
cmap="seismic_r",)
ax.yaxis.set_major_formatter(dates.AutoDateFormatter(ax.xaxis.get_major_locator()))
ax.set_ylabel("Expiration date", fontweight="heavy")
ax.set_xlabel("Strike Price", fontweight="heavy")
ax.set_zlabel("Gamma (M$ / %)", fontweight="heavy")
plt.show()

The plot looks like this:

enter image description here

The dataframe is the following: enter image description here

Now my code with plotly looks like this:

z = data3['GEX']

fig5 = go.Figure([go.Surface(z= [z,z],
                                 x=data3['expiration'],
                                 y=data3['strike'],
                                    opacity=0.95,
                                    connectgaps=False,
                                    colorscale='reds',
                                    showscale=True,
                                    reversescale=False,
                                    )
                        ]
                )

I read that in plotly the z-axis needs to be at least a 2D array. Which i'm still unsure on how to arrange the dataframe to meet this criteria.

The plot with the code above looks like this and only shows one y-axis value (381 in this case)

enter image description here


Solution

  • Since you are plotting a triangulated surface with plot_trisurf, you should use something similar with plotly, that is create_trisurf from the plotly.figure_factory module.