从2D直方图中以填充方式填充3D直方图 [英] filled 3D histogram from 2D histogram with plotly

查看:167
本文介绍了从2D直方图中以填充方式填充3D直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了一个图形,并在此处发布了确切的代码.这几乎是我想要的,但我希望填充2D直方图切片,而不仅仅是一条线.因此,我添加了 this 示例.我得到的图像如下:

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:

我看到代码可以尝试"做我想做的事情,但是做不到.我尝试了其他选项,例如go.Surfacego.Isosurface,但是没有用.

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.

我的代码:

# 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()

推荐答案

因此,

So, here, @empet gave me the solution.

代码:

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)

结果:

这篇关于从2D直方图中以填充方式填充3D直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆