'filled 3D histogram from 2D histogram with plotly

I have done a graph with the exact code posted here. It is almost exactly what I want but I would like the 2D histogram slices to be filled and not just a line. Therefore, I add the parameter surfaceaxis=0 as I found in this example. The image I get is the following:

enter image description here

I see that the code kind of "tries" to do what I want but not quite. I tried other options like go.Surface or go.Isosurface but didn't work.

My code:

# imports
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
from pdb import set_trace

pio.renderers.default = 'browser'

# data
np.random.seed(123)
df = pd.DataFrame(np.random.normal(50, 5, size=(300, 4)), columns=list('ABCD'))

# plotly setup
fig = go.Figure()

# data binning and traces
for i, col in enumerate(df.columns):
    a0 = np.histogram(df[col], bins=10, density=False)[0].tolist()
    a0 = np.repeat(a0, 2).tolist()
    a0.insert(0, 0)
    a0.append(0)
    a1 = np.histogram(df[col], bins=10, density=False)[1].tolist()
    a1 = np.repeat(a1, 2)
    # set_trace()

    fig.add_traces(go.Scatter3d(x=[i] * len(a0), y=a1, z=a0,
                                mode='lines',
                                name=col,
                                surfaceaxis=0
                                )
                   )

    # fig.add_traces(go.Surface(x=[i] * len(a0), y=a1, z=a0))
    # fig.add_traces(go.Isosurface(x=[i] * len(a0), y=a1, z=a0))
fig.show()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source