matplotlib: グラフ描画 https://matplotlib.org/stable/index.html
mplot3d: 3d描画 https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
seaborn: matplotlibをきれいに見せるラッパー https://seaborn.pydata.org/
matplotlib tutorial: https://matplotlib.org/stable/tutorials/index.html
import numpy as np
import matplotlib.pyplot as plt
def get_2d(x0_reso=21, x1_reso=11):
x_range = (0, 10)
x0s = np.linspace(x_range[0], x_range[1], x0_reso)
x1s = np.linspace(x_range[0], x_range[1], x1_reso)
xx0s, xx1s = np.meshgrid(x0s, x1s)
xs = np.c_[xx0s.reshape(-1), xx1s.reshape(-1)]
ys = 1*np.sin(xs[:, 0]) + 1*np.cos(xs[:, 1])
yys = ys.reshape(x1_reso, x0_reso)
x_n = (x0_reso, x1_reso)
return xx0s, xx1s, yys, x_range, x0s, x1s
xx0s, xx1s, yys, x_range, x0s, x1s = get_2d()
print('xx0s', xx0s.shape)
print('xx1s', xx1s.shape)
print('yys', yys.shape)
xx0s (11, 21) xx1s (11, 21) yys (11, 21)
xx0s, xx1s, yys, x_range, x0_tick, x1_tick = get_2d(x0_reso=21, x1_reso=11)
fig = plt.figure()
# colorbar を出すときには ax方式ではなくplt方式が楽
plt.pcolor(yys, cmap='Blues')
# Blues, jet, hsv, bone, ocean, hsv, terrain, etc.
# cmap一覧: https://beiznotes.org/matplot-cmap-list/
plt.colorbar()
plt.show()
xx0s, xx1s, yys, x_range, x0_tick, x1_tick = get_2d(x0_reso=41, x1_reso=41)
fig = plt.figure()
ax = fig.add_subplot(111)
cont = ax.contour(xx0s, xx1s, yys, cmap='jet')
cont.clabel(fmt='%1.1f', fontsize=12)
plt.show()
xx0s, xx1s, yys, x_range, x0_tick, x1_tick = get_2d(x0_reso=41, x1_reso=41)
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(xx0s, xx1s, yys, cmap='ocean')
plt.show()
## 表示パラメータを調整する
xx0s, xx1s, yys, x_range, x0_tick, x1_tick = get_2d(x0_reso=41, x1_reso=41)
plt.rcParams['font.size'] = 16
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(xx0s, xx1s, yys, cmap='ocean')
ax.set_xlabel('\nx label', linespacing=1.0) # labelの軸からの距離調整
ax.set_ylabel('\ny label', linespacing=1.0)
ax.set_zlabel('\nz label', linespacing=1.0)
ax.set_title('$sin(x_0) + sin(x_1)$', y=1.15)
ax.view_init(elev=60, azim=-30)
plt.tight_layout()
plt.show()
plt.rcParams['font.size'] = 12
xx0s, xx1s, yys, x_range, x0_tick, x1_tick = get_2d(x0_reso=4, x1_reso=4)
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
x0s = xx0s.reshape(-1)
x1s = xx1s.reshape(-1)
ys = yys.reshape(-1)
ys += 2
for x0, x1, yy in zip(x0s, x1s, ys):
ax.plot(
[x0, x0], [x1, x1], [0, yy],
color='#555',
linewidth=1,
alpha=0.5,
)
ax.plot(
x0, x1, yy, 'o',
markerfacecolor='#0a0',
markeredgecolor='#fff',
markersize=10,
alpha=0.9,
)
ax.text(x0, x1, yy+0.2, '{:.1f}'.format(yy),
color='#555',
fontsize=14)
plt.show()