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

```txt:動作環境
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 の表示](http://qiita.com/7of9/items/ec9e4bd04a2449e54099)


[Jupyter | Matplotlib > 凹凸の激しい形状の可視化 > ADDA:IntField-Yファイルからの読込み v0.2 > 上半球と下半球の表示 > Spherical particles | Chebyshev particles | Prise](http://qiita.com/7of9/items/54290368046f0be0dec4)
の続き

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

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

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/e6e3ccd6-a114-7100-fdcd-8bf0b6fee2eb.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
の回答において球を色づけて描画する例がある。

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

### 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](https://qiita-image-store.s3.amazonaws.com/0/32870/708577d9-ee2e-ac12-71c6-3337609a5316.png)

### code v0.2 > 8 spheres

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

```py: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](https://qiita-image-store.s3.amazonaws.com/0/32870/f2f0b543-0053-1a1e-d1c1-21ef123e8054.png)

### code v0.3 > 3000 spheres with colors

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

```py: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](https://qiita-image-store.s3.amazonaws.com/0/32870/4ce1ce38-da4e-222a-7bb9-7ce5122602af.png)

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

描画処理時間は31.367秒。