[英]Matplotlib 3d surface plot input arrays
我正在尝试使用3个numpy数组创建表面图:
x_deflections
[shape:(10,)] y_alphas
[shape:(12,)] z_height_at_target
[形状:(120,)] x_deflections
和y_alphas
,并使用其中的2个来计算z_height_at_target
,如下所示:
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
这可以按我期望的方式工作,并在z_height_at_target
提供了合理的值,但是我似乎无法弄清楚如何从中创建合适的表面图。 我目前的方法给我的图表充满了尖峰:
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()
我知道问题出在以下几行之一,但是我似乎无法解决:
X, Y = np.meshgrid(x_deflections, np.rad2deg(y_alphas), indexing='xy')
Z = z_height_at_target.reshape((len(x_deflections), len(y_alphas)))
当前,这会引发一个错误,说明形状不正确,这是正确的,但转置Z
会产生乱码。
任何帮助,将不胜感激。 谢谢!
使用@Uvar的建议来解决此问题,即建议转置z_height_at_target
并将值放在2维中,而不是事后重塑(这实际上与前面的代码相同):
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.