[英]Plot 3D scatter plot from 3D array
我目前正在嘗試 plot 一個 3D 分散 plot 使用 Z76AA96369ABBBA52E621BFA8ZDA3 陣列。 我在網上找到的關於繪制 3D 散點圖 plot 看起來像ax.scatter3D(x, y, z)
其中x
, y
, z
都是一維數組。 但是,就我而言,我使用 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?
我會說你需要 4 個維度來 plot 你創建的直方圖。 一個想法可能是使用 3D 散布 plot 更改標記的大小以編碼有關每個 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)
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))
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)
這是 plot 結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.