LoginSignup
1
0

More than 1 year has passed since last update.

matplotlibで3次元点群を色付きで表示

Posted at

前提

点群:
代表点4つを基にして同じ分散を持つ点群120点(4*30点)を生成する
表示:
カラーマップ(teb10)を基にして点ごとに色をつけて表示

コード

sample.py
import matplotlib.pyplot as plt
import numpy as np
#点群の生成

# 生成するグループの数
k = 4
# グループ毎の分散
sigma = [[20, 0, 0], [0, 20, 0], [0, 0, 20]]

# 座標を格納する空の配列を作成
x = np.empty((0,3))

# 配列の中に座標を格納
for ell in range(k):
    # グループ毎の代表点
    pc = np.random.uniform(low=-20, high=20, size=(3,)) 
   
    #代表点を基に分散sigmaの点を30点作成
    xs = np.random.multivariate_normal(pc,sigma,30)
    
    #xに3次元の座標を持った点群を格納
    x = np.concatenate([x,xs])
# 3次元座標を分割
x_ = x[...,0]
y_ = x[...,1]
z_ = x[...,2]

# plt.scatterで3次元にplot
# colormapを取ってくる
cmap = plt.get_cmap("tab10")

# 配列にcolorコードを格納
colors = np.empty((120,4)
for i in range(120):
    colors[i] = cmap(i % 10)

fig = plt.figure()
# projectionを3dと指定する
ax = fig.add_subplot(projection='3d')
ax.scatter(x_, y_, z_, color=colors)
plt.show()

結果

点ごとに色付きで表示できていることが確認できた
スクリーンショット 2023-01-10 21.18.08.png

注意

ax = fig.add_subplot(projection='3d')で3dを明示的に設定することで3次元の表示ができる

詳細

コードの説明はおいおい追加する予定

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0