何の目標もないけど、暇つぶしにやるPython。 まずは3次元表示してみたい。 以前C#では結構苦労したことがある。 Pythonではmatplotlibというモジュールがあって、簡単に実現できる。
とりあえず枠だけ。
Python 3.12.2, numpy 2.2.3, matplotlib 3.10.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import numpy as np import matplotlib.pyplot as plt # 3D座標設定 def coordinate_3d(axes, range_x, range_y, range_z): axes.set_xlabel("x", fontsize=16) axes.set_ylabel("y", fontsize=16) axes.set_zlabel("z", fontsize=16) axes.set_xlim(range_x[0], range_x[1]) axes.set_ylim(range_y[0], range_y[1]) axes.set_zlim(range_z[0], range_z[1]) #3D グリッドの描画 def grid_3d(axes, range_x, range_y, range_z): x, y, z = np.mgrid[range_x[0]: range_x[1]: 11j, range_y[0]: range_y[1]: 11j, range_z[0]: range_z[1]: 11j] axes.scatter(x, y, z, s=0.2, color='red') fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection='3d') coordinate_3d(ax, [-5, 5], [-5, 5], [-5, 5]) grid_3d(ax, [-5, 5], [-5, 5], [-5, 5]) plt.show() |
ちょっと解り難いけど、空中にも赤い点を表示した。 デフォルトでマウスでグリグリできるので便利。