雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

【matplotlib】とりあえず3dデータをプロットするコード

出力したい画像

ソースコード

>>> import numpy as plt
>>> import matplotlib.pyplot as plt

>>> x1 = np.arange(-5,5,0.2)
>>> x1
array([-5.0000000e+00, -4.8000000e+00, -4.6000000e+00, -4.4000000e+00,
       -4.2000000e+00, -4.0000000e+00, -3.8000000e+00, -3.6000000e+00,
       -3.4000000e+00, -3.2000000e+00, -3.0000000e+00, -2.8000000e+00,
       -2.6000000e+00, -2.4000000e+00, -2.2000000e+00, -2.0000000e+00,
       -1.8000000e+00, -1.6000000e+00, -1.4000000e+00, -1.2000000e+00,
       -1.0000000e+00, -8.0000000e-01, -6.0000000e-01, -4.0000000e-01,
       -2.0000000e-01,  4.4408921e-15,  2.0000000e-01,  4.0000000e-01,
        6.0000000e-01,  8.0000000e-01,  1.0000000e+00,  1.2000000e+00,
        1.4000000e+00,  1.6000000e+00,  1.8000000e+00,  2.0000000e+00,
        2.2000000e+00,  2.4000000e+00,  2.6000000e+00,  2.8000000e+00,
        3.0000000e+00,  3.2000000e+00,  3.4000000e+00,  3.6000000e+00,
        3.8000000e+00,  4.0000000e+00,  4.2000000e+00,  4.4000000e+00,
        4.6000000e+00,  4.8000000e+00])

>>> x2 = np.arange(-5,5,0.2)
>>> y = x1*0.2 + x2*0.5
>>> y
array([-3.50000000e+00, -3.36000000e+00, -3.22000000e+00, -3.08000000e+00,
       -2.94000000e+00, -2.80000000e+00, -2.66000000e+00, -2.52000000e+00,
       -2.38000000e+00, -2.24000000e+00, -2.10000000e+00, -1.96000000e+00,
       -1.82000000e+00, -1.68000000e+00, -1.54000000e+00, -1.40000000e+00,
       -1.26000000e+00, -1.12000000e+00, -9.80000000e-01, -8.40000000e-01,
       -7.00000000e-01, -5.60000000e-01, -4.20000000e-01, -2.80000000e-01,
       -1.40000000e-01,  3.10862447e-15,  1.40000000e-01,  2.80000000e-01,
        4.20000000e-01,  5.60000000e-01,  7.00000000e-01,  8.40000000e-01,
        9.80000000e-01,  1.12000000e+00,  1.26000000e+00,  1.40000000e+00,
        1.54000000e+00,  1.68000000e+00,  1.82000000e+00,  1.96000000e+00,
        2.10000000e+00,  2.24000000e+00,  2.38000000e+00,  2.52000000e+00,
        2.66000000e+00,  2.80000000e+00,  2.94000000e+00,  3.08000000e+00,
        3.22000000e+00,  3.36000000e+00])

>>> plt.figure(figsize=(6,5))
>>> from mpl_toolkits.mplot3d import axes3d, Axes3D
>>> ax = plt.subplot(1,1,1,projection="3d")
>>> ax.scatter(x1, x2, y)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7fd8dba73b38>