LoginSignup
2
2

More than 5 years have passed since last update.

3次元の散布図を補間

Posted at

2次元の散布図を補間する例はたくさんあるけれど、3次元にはあまりないので。

3つの軸のどれかをX軸に選ぶのがおそらく一番簡単。

import numpy as np
import scipy.interpolate as itpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def show_points(points):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    for i in points:
        x, y, z = i
        ax.scatter(x, y, z, marker='o')

    ax.set_xlabel('X')
    ax.set_xlim(0, 1)
    ax.set_ylabel('Y')
    ax.set_ylim(0, 1)
    ax.set_zlabel('Z')
    ax.set_zlim(0, 1)

    plt.show()


def gen_curve(n_point):
    x = np.linspace(0, 1, n_point)
    points = np.array([x, np.sin(x * np.pi / 2), np.cos(x * np.pi / 2)]).transpose((1, 0))
    return points

def main():
    points = gen_curve(10)
    show_points(points)
    f = itpl.interp1d(points[:,0], points[:,1:3], axis=0, kind='cubic')
    x = np.linspace(0, 1, 20)
    y = f(x)
    points2 = np.hstack((x.reshape(-1, 1), y))
    show_points(points2)

if __name__=='__main__':
    main()
2
2
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
2
2