Pythonで3次元プロットをする前に座標回転を追加してみた。
前のコードに追加を行った。
回転行列をかけるということで座標変換を行う。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
def orthogonal_proj(zfront, zback):
width = zfront - zback;
a = 1 / width
b = - a * (zfront + zback) / 2
return np.array([[1,0,0,0],
[0,1,0,0],
[0,0,a,b],
[0,0,0,zback]])
proj3d.persp_transformation = orthogonal_proj
def rotX(X):
theta = X / 180.0 * np.pi;
Rx = np.array([[1,0,0,0],
[0,np.cos(theta),np.sin(theta),0],
[0,-np.sin(theta),np.cos(theta),0],
[0,0,0,1]]);
return Rx;
def rotY(Y):
theta = Y / 180.0 * np.pi;
Ry = np.array([[np.cos(theta),0,-np.sin(theta),0],
[0,1,0,0],
[np.sin(theta),0,np.cos(theta),0],
[0,0,0,1]]);
return Ry;
def rotZ(Z):
theta = Z / 180.0 * np.pi;
Rz = np.array([[np.cos(theta),np.sin(theta),0,0],
[-np.sin(theta),np.cos(theta),0,0],
[0,0,1,0],
[0,0,0,1]]);
return Rz;
def trans(x):
Tr = np.array([[1,0,0,x[0]],
[0,1,0,x[1]],
[0,0,1,x[2]],
[0,0,0,1]]);
return Tr;
def main():
r = 50.0;
theta = np.arange(0.0, 360.0, 30.0);
x = np.array([]);
y = np.array([]);
z = np.array([]);
for i in range(10):
x_ = r * np.cos(theta / 180.0 * np.pi);
y_ = r * np.sin(theta / 180.0 * np.pi);
z_ = 10.0 * i * np.ones(x_.shape[0]);
x = np.append(x, x_);
y = np.append(y, y_);
z = np.append(z, z_);
X = np.array([x, y, z, np.ones(x.shape[0])]);
X = np.dot(rotX(45.0), X);
X = np.dot(rotY(45.0), X);
X = np.dot(rotZ(10.0), X);
X = np.dot(trans([10, 10, -10]), X);
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter3D(X[0], X[1], X[2]);
ax.set_aspect('equal')
plt.grid()
plt.savefig('plot201803.svg');
plt.show();
if __name__ == '__main__':
main()