【问题标题】:Plotting points as 3D spheres using Plotly使用 Plotly 将点绘制为 3D 球体
【发布时间】:2022-06-18 01:46:55
【问题描述】:

我有表格形式的数据,我希望将其绘制为 3D 球体 - 其中点代表半径为 r = 点值的球体。
例如:point=4.7 --> 半径为 4.7 的球体

例子:

表 1 将产生 15 个球体,例如高度 z=1

1 2 3 4 5
4.5 4.9 4.9 4.9 4.7
4.5 4.8 4.8 4.8 4.8
4.3 4.7 4.7 4.9 4.9

表 2 将导致另外 15 个球体“向上”移动,例如高度 z=2

1 2 3 4 5
4.3 4.7 4.7 4.9 4.7
4.4 4.8 4.8 4.8 4.7
4.5 4.9 4.9 4.9 4.8

【问题讨论】:

  • 欢迎来到 Stack Overflow,这个问题缺少一些细节 - 请包含一些代码,这些代码是您的问题的 minimum reproducible example
  • 您好,感谢您的编辑。我真的没有一个“可重现”的例子,因为我首先试图找出产生情节本身的方法。

标签: python plotly-python


【解决方案1】:

我最终能够弄清楚这一点,关键思想是使用marker 参数,而该参数又具有size 属性(参考:plotly documentation

示例代码:

使用 Pandas to_numpy() 方法将数据帧转换为 numpy 数组列表,然后使用 NumPy flatten() 方法展平每个数组。
例如:arrList.append(df.iloc[0:5,:].to_numpy().flatten())
注意:我的数据还包含“-”形式的空格。

arrList =
[array([2.5, 2.7, 3.9, 3.8, 3.9, 2.6, 2.5, 2.5, 3.9, 3.7, 2.4, 2.6, 2.4, 4,
        3.9, 2.5, 2.3, 2.3, 3.9, 3.7, 3.8, 3.9, 3.6, 3.7, 3.8, 3.7, 3.7,
        3.7, 3.8, 3.9], dtype=object),
 array([3.8, 3.9, 2.7, 3, 2.6, 3.9, 3.3, 2.9, 2.7, 3.8, 4, 3.6, 3.9, 3.8,
        3.9, 3.7, 3.8, 4, 3.9, 3.6, 3.8, 3.9, '-', '-', '-', 3.9, 3.9, '-',
        '-', '-'], dtype=object)]

由于size 属性以像素为单位分配大小,我已经定义了一种方法来将像素“增加”x 倍(在我的情况下为 3 倍),用于我的数据中的浮点和/或整数值

def sizeMask(s):
    sphereSize = lambda s: s if isinstance(s, float) else s if isinstance(s, int) else 0.0
    vec_sphereSize = np.vectorize(sphereSize)
    return vec_sphereSize(s)
sizeList = [sizeMask(arr)*3.0 for arr in arrList]
sizeList =
[array([ 7.5,  8.1, 11.7, 11.4, 11.7,  7.8,  7.5,  7.5, 11.7, 11.1,  7.2,
         7.8,  7.2, 12. , 11.7,  7.5,  6.9,  6.9, 11.7, 11.1, 11.4, 11.7,
        10.8, 11.1, 11.4, 11.1, 11.1, 11.1, 11.4, 11.7]),
 array([11.4, 11.7,  8.1,  9. ,  7.8, 11.7,  9.9,  8.7,  8.1, 11.4, 12. ,
        10.8, 11.7, 11.4, 11.7, 11.1, 11.4, 12. , 11.7, 10.8, 11.4, 11.7,
         0. ,  0. ,  0. , 11.7, 11.7,  0. ,  0. ,  0. ])]

最后生成的情节如下:

x = np.asarray([[i]*5 for i in range(1,7)]).flatten()
y = np.asarray([np.arange(1,6)]*6).flatten()

data=[]
for i,arr in enumerate(arrList):
    data.append(go.Scatter3d(x=x, y=y,
                             z=np.asarray([i+1]*30),
                             mode='markers',
                             marker=dict(size=sizeList[i], showscale=False)))
    
fig = go.Figure(data=data)
fig.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2014-09-23
    相关资源
    最近更新 更多