# Adding a shape to a 3D plot

**URL:** <https://community.plotly.com/t/adding-a-shape-to-a-3d-plot/1441>\
**Category:** Plotly Fundamentals\
**Created:** [July 1, 2016, 3:30pm UTC](https://community.plotly.com/t/adding-a-shape-to-a-3d-plot/1441 "2016-07-01T15:30:42Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)\
**Post date:** [February 28, 2020, 9:53pm UTC](https://community.plotly.com/t/adding-a-shape-to-a-3d-plot/1441/8 "2020-02-28T21:53:06Z")

</div>

@NEGU93

Update: To avoid the drawback of the Delaunay triangulation, discussed above, I defined a function that triangulates each histogram bar, and now the histogram is an instance of `go.Mesh3d`:

```auto
import numpy as np
import pandas as pd
import plotly.graph_objects as go

def triangulate_histogtam(x, y, z):
   
    if len(x) != len(y) != len(z) :
        raise ValueError("The lists x, y, z, must have the same length")
    n = len(x)
    if n % 2 :
        raise ValueError("The length of lists x, y, z must be an even number") 
    pts3d = np.vstack((x, y, z)).T
    pts3dp = np.array([[x[2*k+1], y[2*k+1], 0] for k in range(1, n//2-1)])
    pts3d = np.vstack((pts3d, pts3dp))
    #triangulate the histogram bars:
    tri = [[0,1,2], [0,2,n]]
    for k, i in zip(list(range(n, n-3+n//2)), list(range(3, n-4, 2))):
        tri.extend([[k, i, i+1], [k, i+1, k+1]])
    tri.extend([[n-3+n//2, n-3, n-2], [n-3+n//2, n-2, n-1]])      
    return pts3d, np.array(tri)

# 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
bins = 10

bar_color = ['#e763fa', '#ab63fa', '#636efa', '#00cc96']
for m, col in enumerate(df.columns):
    a0=np.histogram(df[col], bins=bins, density=False)[0].tolist()
    a0=np.repeat(a0,2).tolist()
    a0.insert(0,0)
    a0.pop()
    a0[-1]=0
    a1=np.histogram(df[col], bins=bins-1, density=False)[1].tolist()
    a1=np.repeat(a1,2)
   
    verts, tri = triangulate_histogtam([m]*len(a0), a1, a0)
    x, y, z = verts.T
    I, J, K = tri.T
    fig.add_traces(go.Mesh3d(x=x, y=y, z=z, i=I, j=J, k=K, color=bar_color[m], opacity=0.7))
                   
fig.update_layout(width=700, height=700, scene_camera_eye_z=0.8)                                       

```

 ![histograms-as-mesh3d](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/5/58b80afd36098f627904e2f4f43de1c745a7b0e6.png)

---

_[View the full topic](https://community.plotly.com/t/adding-a-shape-to-a-3d-plot/1441)._
