2017-12-19 4 views
1

Ich versuche, ein Oberflächendiagramm unter Verwendung von 3 numpy Arrays zu erzeugen:Matplotlib 3D-Oberflächendiagramm Eingang Arrays

  • x_deflections [Form: (10,)]
  • y_alphas [Form: (12,)]
  • z_height_at_target [Form: (120)]

x_deflections und y_alphas gegeben und z_height_at_target berechnet, um die 2 von ihnen, wie dies mit:

i = 0 
for x in x_deflections: 
    for y in y_alphas: 
     exit_v = spring_calc.do_calc(k, x) 
     # Only care about x_dist and h here 
     vX, vY, x_dist, h = traj_calc.do_calc(exit_v, y, stop_at=target_dist) 
     try: 
      if max(x_dist) < target_dist: 
       raise StopIteration 
      else: 
       target_dist_index = find_nearest(x_dist, target_dist, 0.04) 
     except StopIteration: 
      print('Target Distance not achieved') 
      continue 
     z_height_at_target[i] = h[target_dist_index] 
     i += 1 

Dies funktioniert wie ich erwarten würde und gibt vernünftige Werte in z_height_at_target, aber ich kann nicht zu arbeiten, scheint wie eine richtige Oberflächenplot von diesem zu erstellen. Meine aktuelle Methode gibt mir Graphen voller Spitzen Kauderwelsch:

fig = pl.figure() 
ax = fig.add_subplot(111, projection='3d') 
X, Y = np.meshgrid(x_deflections, np.rad2deg(y_alphas), indexing='xy') 
Z = z_height_at_target.reshape((len(x_deflections), len(y_alphas))) 
ax.plot_surface(X, Y, Z, color='b') 
ax.set_xlabel('Spring Deflection [m]') 
ax.set_ylabel('Launch Angle [deg]') 
ax.set_zlabel('Height at Target Distance [m]') 
pl.show() 

Ich bin mir bewusst, das Problem in einer der folgenden Zeilen liegt, jedoch scheine ich kann meinen Kopf nicht um es zu bekommen:

X, Y = np.meshgrid(x_deflections, np.rad2deg(y_alphas), indexing='xy') 
Z = z_height_at_target.reshape((len(x_deflections), len(y_alphas))) 

Dies wirft derzeit einen Fehler, falsche Form zu sagen, die korrekt ist, aber die Umsetzung Z gibt Kauderwelsch.

Jede Hilfe wäre willkommen. Vielen Dank!

+2

Wie du getan hat Versuchen Sie, Z zu transponieren, mit 'Z = ZT'? Das sollte Ihnen Ihr Oberflächenplot wie vorgesehen geben. – Uvar

+2

Oh, und etwas, das ich gerade gedacht habe ... bitte stellen Sie sicher, dass Ihre x und y sortiert sind, sonst könnten Sie seltsame Ergebnisse bekommen. – Uvar

Antwort

0

dies mit @Uvar ‚s Vorschlag Gelöst z_height_at_target zur Umsetzung und durch die Werte in in 2 Dimensionen setzen, anstatt danach die Umformung (Diese effektiv macht das gleiche wie der vorherigen Code):

i = 0 
x_count = 0 
miss_count = 0 
for x in x_deflections: 
    y_count = 0 
    for y in y_alphas: 
     exit_v = spring_calc.do_calc(k, x, y) 
     # only need h here but the rest of can come along for the ride as well 
     vX, vY, x_dist, h = traj_calc.do_calc(exit_v, y, stop_at=target_dist) 
     try: 
      if max(x_dist) < target_dist: 
       raise StopIteration 
      else: 
       target_dist_index = find_nearest(x_dist, target_dist, 0.04) 
     except StopIteration: 
      print('Target Distance not achieved') 
      miss_count += 1 
      continue 
     z_height_at_target[x_count, y_count] = h[target_dist_index] 
     print('Completed iter', i+1) 
     # print('{}, {}, {}'.format(exit_v, h[target_dist_index], np.rad2deg(y))) 
     i += 1 
     y_count += 1 
    x_count += 1 

fig = pl.figure() 
ax = fig.add_subplot(121, projection='3d') 
# ax = fig.gca(projection='3d') 
X, Y = np.meshgrid(x_deflections, np.rad2deg(y_alphas), indexing='xy') 
Z = z_height_at_target.T