简体   繁体   English

Plotly:如何制作 3D 堆叠直方图?

[英]Plotly: How to make a 3D stacked histogram?

I have several histograms that I succeded to plot using plotly like this:我有几个直方图,我成功地使用这样的plotly绘制:

fig.add_trace(go.Histogram(x=np.array(data[key]), name=self.labels[i]))

I would like to create something like this 3D stacked histogram but with the difference that each 2D histogram inside is a true histogram and not just a hardcoded line (my data is of the form [0.5 0.4 0.5 0.7 0.4] so using Histogram directly is very convenient)我想创建类似这个 3D 堆叠直方图的东西,但不同之处在于里面的每个 2D 直方图都是一个真正的直方图,而不仅仅是一条硬编码的线(我的数据的形式为[0.5 0.4 0.5 0.7 0.4]所以直接使用直方图非常方便的)

Note that what I am asking is not similar to this and therefore also not the same as this .请注意,我要问的与此不同,因此也与此不同 In the matplotlib example, the data is presented directly in a 2D array so the histogram is the 3rd dimension.在 matplotlib 示例中,数据直接呈现在 2D 数组中,因此直方图是第 3 维。 In my case, I wanted to feed a function with many already computed histograms.就我而言,我想为一个函数提供许多已经计算出的直方图。

The snippet below takes care of both binning and formatting of the figure so that it appears as a stacked 3D chart using multiple traces of go.Scatter3D and np.Histogram .下面的代码段负责图形的分箱和格式设置,使其显示为使用go.Scatter3Dnp.Histogram多个跟踪的堆叠 3D 图表。 The input is a dataframe with random numbers using np.random.normal(50, 5, size=(300, 4)) We can talk more about the other details if this is something you can use:输入是一个带有随机数的数据np.random.normal(50, 5, size=(300, 4))使用np.random.normal(50, 5, size=(300, 4))如果这是你可以使用的东西,我们可以更多地讨论其他细节:

Plot 1: Angle 1图 1:角度 1

在此处输入图片说明

Plot 2: Angle 2图 2:角度 2

在此处输入图片说明

Complete 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

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.pop()
    a1=np.histogram(df[col], bins=10, density=False)[1].tolist()
    a1=np.repeat(a1,2)
    fig.add_traces(go.Scatter3d(x=[i]*len(a0), y=a1, z=a0,
                                mode='lines',
                                name=col
                               )
                  )
fig.show()

Unfortunately you can't use go.Histogram in a 3D space so you should use an alternative way.不幸的是,您不能在 3D 空间中使用go.Histogram ,因此您应该使用另一种方式。 I used go.Scatter3d and I wanted to use the option to fill line doc but there is an evident bug see我使用了go.Scatter3d并且我想使用该选项来填充行文档,但有一个明显的错误,请参阅

import numpy as np
import plotly.graph_objs as go

# random mat
m = 6
n = 5
mat = np.random.uniform(size=(m,n)).round(1)

# we want to have the number repeated
mat = mat.repeat(2).reshape(m, n*2)

# and finally plot

x = np.arange(2*n)
y = np.ones(2*n)
fig = go.Figure()
for i in range(m):
    fig.add_trace(go.Scatter3d(x=x,
                               y=y*i,
                               z=mat[i,:],
                               mode="lines",
                               # surfaceaxis=1 # bug
                               )
                 )
fig.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM