LoginSignup
3
4

More than 5 years have passed since last update.

Jupyter | Matplotlib > 凹凸の激しい形状の可視化 > v0.1-0.3: 2つの球を並べる | 8つの球 | 3000球 + 色

Last updated at Posted at 2017-09-10
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

関連: Jupyter | Matplotlib > 凹凸の激しい形状の可視化 > ADDA:IntField-Yファイルからの読込み v0.1 > A sphere | Chebyshev particle の表示

Jupyter | Matplotlib > 凹凸の激しい形状の可視化 > ADDA:IntField-Yファイルからの読込み v0.2 > 上半球と下半球の表示 > Spherical particles | Chebyshev particles | Prise
の続き

TODO
Cartesian coordinateをPolar coordinateに変換 > link
Polar coordinateにて外側の位置座標をMeshgridに拾う
plot_surfaceで表示

上記を試したが、Prismがおかしな形状に描画された。

qiita.png

Polar coordinate上においてMeshgrid状に座標点を拾うと、球形のものしか扱えないような気がしてきた。
この方法はここで中止。

球の描画

https://stackoverflow.com/questions/31768031/plotting-points-on-the-surface-of-a-sphere-in-pythons-matplotlib

answered Aug 2 '15 at 19:49
Amy Teegarden
の回答において球を色づけて描画する例がある。

それを利用して、2つの球を描画してみる。

code v0.1 > 2 spheres

showChebyshev_170910.py
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
from pylab import rcParams
import numpy as np

rcParams['figure.figsize'] = 15, 10


# reference
# https://stackoverflow.com/questions/31768031/plotting-points-on-the-surface-of-a-sphere-in-pythons-matplotlib

# Create a sphere
r = 1
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0.0:pi:100j, 0.0:2.0*pi:100j]
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)

#Set colours and render
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(
    x, y, z,  rstride=1, cstride=1, color='c', alpha=1.0, linewidth=0)

ax.plot_surface(
    x + 2, y, z,  rstride=1, cstride=1, color='c', alpha=1.0, linewidth=0)

ax.set_xlim([-2,2])
ax.set_ylim([-2,2])
ax.set_zlim([-2,2])
ax.set_aspect("equal")
plt.tight_layout()
plt.show()

qiita.png

code v0.2 > 8 spheres

球の分解能を減らして、球の数を増やしてみた。

showChebyshev_170910.ipynb
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
from pylab import rcParams
import numpy as np


"""
v0.2 Sep. 10, 2017
  - show 8 spheres
  - lower the resolution of the sphere (from 100j to 6j)
v0.1 Sep. 10, 2017
  - show 2 spheres
"""

rcParams['figure.figsize'] = 15, 10


# reference
# https://stackoverflow.com/questions/31768031/plotting-points-on-the-surface-of-a-sphere-in-pythons-matplotlib

# Create a sphere
r = 1
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0.0:pi:6j, 0.0:2.0*pi:6j]
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)

#Set colours and render
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

xp = [0, 2, 0, 2, 0, 2, 0, 2]
yp = [0, 0, 2, 2, 0, 0, 2, 2]
zp = [0, 0, 0, 0, 2, 2, 2, 2]

def plot_spheres(xps, yps, zps):
    for elem in zip(xps, yps, zps):
        axp, ayp, azp = elem
        print(elem)
        ax.plot_surface(
            x + axp, y + ayp, z + azp,  rstride=1, cstride=1, color='c', alpha=1.0, linewidth=0)

plot_spheres(xp, yp, zp)

ax.set_xlim([-4,4])
ax.set_ylim([-4,4])
ax.set_zlim([-4,4])
ax.set_aspect("equal")
plt.tight_layout()
plt.show()

qiita.png

code v0.3 > 3000 spheres with colors

3000個の球にして、X方向の色付けをしてみた。

showChebyshev_170910.ipynb
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
from pylab import rcParams
import numpy as np

"""
v0.3 Sep. 10, 2017
  - set colors in X direction
  - increase number of spheres to 3000
v0.2 Sep. 10, 2017
  - show 8 spheres
  - lower the resolution of the sphere (from 100j to 6j)
v0.1 Sep. 10, 2017
  - show 2 spheres
"""

# coding rule: PEP8

rcParams['figure.figsize'] = 15, 10


# reference
# https://stackoverflow.com/questions/31768031/plotting-points-on-the-surface-of-a-sphere-in-pythons-matplotlib


def plot_spheres(xps, yps, zps):
    for elem in zip(xps, yps, zps):
        axp, ayp, azp = elem
        # print(elem)
        dx = x + axp
        dy = y + ayp
        dz = z + azp
        ax.plot_surface(
            dx, dy, dz,  rstride=1, cstride=1, color='c', alpha=1.0, linewidth=0,
            facecolors=plt.cm.Set2((dx - 0) / (50 - 0)))

# Create a sphere
r = 1
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0.0:pi:6j, 0.0:2.0*pi:6j]
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)

# Set colours and render
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

NUM_SPHERES = 3000
xp = np.random.rand(NUM_SPHERES)*30
yp = np.random.rand(NUM_SPHERES)*30
zp = np.random.rand(NUM_SPHERES)*30

plot_spheres(xp, yp, zp)

ax.set_xlim([0, 50])
ax.set_ylim([0, 50])
ax.set_zlim([0, 50])
ax.set_aspect("equal")
plt.tight_layout()
plt.show()

qiita.png

単色の場合よりは、座標関係が見えるようにはなった?

描画処理時間は31.367秒。

3
4
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
3
4