如何在matplotlib中绘制表面上的箭头?

2024-04-28 10:52:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

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

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


def fun(x, y):
    return x ** 2 - y ** 2


if __name__ == '__main__':
    fig = plt.figure(dpi=160)
    ax = fig.add_subplot(111, projection='3d')
    x = y = np.arange(-3.0, 3.0, 0.05)
    X, Y = np.meshgrid(x, y)
    zs = np.array(fun(np.ravel(X), np.ravel(Y)))
    Z = zs.reshape(X.shape)

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

    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

    ax.set_xlim3d(-3.5, 3.5)
    ax.set_ylim3d(-3.5, 3.5)
    ax.set_zlim3d(-8.5, 8.5)

    plt.show()

如何在曲面上绘制这些箭头?我使用的是matplotlib3.1.1,这是本次提问时的最新版本。你知道吗


Tags: importasnpfig绘制plt箭头ax