matplotlib 为什么不能在一个表面上画一条线?

dauxcl2d  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(145)

我尝试使用quiver在曲面上绘制3个箭头。箭头似乎总是在曲面后面绘制。结果如下:

这是生成这个结果的代码:

  1. import numpy as np
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import matplotlib.pyplot as plt
  4. def fun(x, y):
  5. return x ** 2 - y ** 2
  6. if __name__ == '__main__':
  7. fig = plt.figure(dpi=160)
  8. ax = fig.add_subplot(111, projection='3d')
  9. x = y = np.arange(-3.0, 3.0, 0.05)
  10. X, Y = np.meshgrid(x, y)
  11. zs = np.array(fun(np.ravel(X), np.ravel(Y)))
  12. Z = zs.reshape(X.shape)
  13. ax.plot_surface(X, Y, Z, cmap=plt.get_cmap('Blues'))
  14. ax.quiver([0], [0], [1], [0, -1, 0], [-1, 0, 0], [0, 0, 2.5], lw=4, color=['r', 'g', 'b']) # The z is 1 unit above the surface
  15. ax.set_xlim3d(-3.5, 3.5)
  16. ax.set_ylim3d(-3.5, 3.5)
  17. ax.set_zlim3d(-8.5, 8.5)
  18. plt.show()

如何在曲面上绘制这些箭头?我使用的是matplotlib 3.1.1,这是提出这个问题时的最新版本。

sgtfey8w

sgtfey8w1#

你可以使用的一个黑客解决方案,虽然不是理想的,是减少表面的alpha。

  1. ax.plot_surface(X, Y, Z, cmap=plt.get_cmap('Blues'), alpha=0.5)

相关问题