Guest User

Untitled

a guest
Nov 14th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. from scipy.interpolate import griddata
  5.  
  6. # x, y, z をそれぞれ1次元配列やリストに格納してしまった!
  7. x = []
  8. y = []
  9. z = []
  10. for i in range(-10, 10):
  11. for j in range(-10, 10):
  12. x.append(j)
  13. y.append(i)
  14. z.append(i ** 2 + j ** 2)
  15.  
  16. # そのままプロットしようとしてエラー!
  17. # plt.contourf(x, y, z) # --> TypeError: Input z must be a 2D array.
  18.  
  19. # そういうときは、scipy.interpolate.griddata() でサンプリングしなおしましょう。
  20. x_new, y_new = np.meshgrid(np.unique(x), np.unique(y))
  21. z_new = griddata((x, y), z, (x_new, y_new))
  22.  
  23. # これでちゃんと描画できます
  24. ax = Axes3D(plt.figure())
  25. ax.plot_surface(x_new, y_new, z_new)
  26. plt.show()
Add Comment
Please, Sign In to add comment