Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- from scipy.interpolate import griddata
- # x, y, z をそれぞれ1次元配列やリストに格納してしまった!
- x = []
- y = []
- z = []
- for i in range(-10, 10):
- for j in range(-10, 10):
- x.append(j)
- y.append(i)
- z.append(i ** 2 + j ** 2)
- # そのままプロットしようとしてエラー!
- # plt.contourf(x, y, z) # --> TypeError: Input z must be a 2D array.
- # そういうときは、scipy.interpolate.griddata() でサンプリングしなおしましょう。
- x_new, y_new = np.meshgrid(np.unique(x), np.unique(y))
- z_new = griddata((x, y), z, (x_new, y_new))
- # これでちゃんと描画できます
- ax = Axes3D(plt.figure())
- ax.plot_surface(x_new, y_new, z_new)
- plt.show()
Add Comment
Please, Sign In to add comment