nano_exit

基礎的なことこそ、簡単な例が必要だと思うのです。

pythonで三次元プロットする時の自分用メモ

三次元プロット用のx軸、y軸の配列。

import numpy as np

N = 100
x_min, x_max = -1, 1
y_min, y_max = -1, 1

x1 = np.linspace( x_min, x_max, N )
y1 = np.linspace( y_min, y_max, N )
X, Y = np.meshgrid( x1, y1 )

三次元プロット用のz軸データ。

def arbitrary_function( x, y ):
    return x - y

Z = np.zeros( ( N, N ) )
for ix, x_ in enumerate( x1 ):
    for iy, y_ in enumerate( y1 ):
        Z[ iy ][ ix ] = arbitrary_function( x_, y_ )  # Caution: not Z[ ix ][ iy ]

カラーマップ付きの等高プロット

from matplotlib import pyplot as plt

fig = plt.figure( figsize = ( 5, 4 ) )
ax = fig.add_subplot( 1, 1, 1 )
v_min, v_max = -2, 2
c = ax.pcolor( X, Y, Z, cmap = 'viridis', vmin = v_min, vmax = v_max )  # or "pcolormesh"
# level_list = [ -2, -1, 0, 1, 2 ]
#c = ax.contour( X, Y, Z, cmap = 'viridis', levels = level_list ) 
#c = ax.contourf( X, Y, Z, cmap = 'viridis', levels = level_list ) 
fig.colorbar( c )
plt.show()

三次元散布図。

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure( )
ax = fig.add_subplot( 1, 1, 1, projection='3d' )
maker_size = 20  # default
ax.scatter( X, Y, Z, s = maker_size )
plt.show()