简体   繁体   English

Plot 3D 分散 plot 来自 3D 阵列

[英]Plot 3D scatter plot from 3D array

I am currently trying to plot a 3D scatter plot by using a 3D array.我目前正在尝试 plot 一个 3D 分散 plot 使用 Z76AA96369ABBBA52E621BFA8ZDA3 阵列。 What I found online about plotting 3D scatter plot looks something like ax.scatter3D(x, y, z) where x , y , z are all 1D array.我在网上找到的关于绘制 3D 散点图 plot 看起来像ax.scatter3D(x, y, z)其中x , y , z都是一维数组。 However, in my case, I am generating an array of shapes (3, 3, 3) by using numpy's histogramdd .但是,就我而言,我使用 numpy 的histogramdd生成了一个形状数组(3, 3, 3)

In [61]: h, edges = histogramdd(array([[1,2,4],[4,2,8],[3,2,1],[2,1,2],[2,1,3],[2,1,1],[2,1,4]]),bins=3)
In [64]: h
Out[64]:
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  1.,  0.]],

       [[ 3.,  1.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 1.,  0.,  1.]]])

My question is, how can I unpack this (3, 3, 3) into 1-dimensional arrays that correspond to the axes so that I can plot 3d scatter plot? My question is, how can I unpack this (3, 3, 3) into 1-dimensional arrays that correspond to the axes so that I can plot 3d scatter plot?

I would say you need 4 dimensions to plot the histogram you have created.我会说你需要 4 个维度来 plot 你创建的直方图。 One idea could be using a 3D scatter plot changing the size of the marker to encode the information about the number of data points contained in each of the 3D bins.一个想法可能是使用 3D 散布 plot 更改标记的大小以编码有关每个 3D 箱中包含的数据点数量的信息。

Here's what I would do.这就是我要做的。

  1. generate the 3D histogram生成 3D 直方图
import numpy as np

a = np.array([[1,2,4],[4,2,8],[3,2,1],[2,1,2],[2,1,3],[2,1,1],[2,1,4]])
h, edges = np.histogramdd(a, bins=3)
  1. Create the 3D coordinates of your bins position创建您的 bin position 的 3D 坐标
ex, ey, ez = edges
x, y, z = np.meshgrid(np.linspace(ex[0], ex[-1], 3),
                      np.linspace(ey[0], ey[-1], 3),
                      np.linspace(ez[0], ez[-1], 3))
  1. Use a 3D scatter to plot your bin and change the size of the markers to encode the number of points contained in each plotted bin:使用 3D 分散到 plot 您的 bin 并更改标记的大小以编码每个绘制的 bin 中包含的点数:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(x.flatten(), y.flatten(), z.flatten(), s=h.flatten()*50)

Here's the plot result:这是 plot 结果:

3D 散点图

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

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