多次使用 Jupyter Notebook / plt.quiver 时如何阻止箭头堆叠在一起?

问题描述 投票:0回答:1

我在使用 matplotlib.pyplot.quiver() 命令时遇到问题,希望您知道该怎么办。我正在尝试编写一个代码,该代码将 2D 数组作为输入,并为数组中的每个角度输出颜色和箭头。

这是我用于颜色的函数:

def plot_map(arr_2D):
    Farben = colors.Normalize(vmin=-np.pi, vmax=np.pi)
    plt.imshow(arr_2D, cmap='hsv', norm=Farben)
    return

这是箭头:

def plot_arrows(arr_2D):
    X = np.arange(2)
    Y = np.arange(2)
    U = np.cos(arr_2D[X][Y])
    V = np.sin(arr_2D[X][Y])
    plt.quiver(X,Y,U,V, units = 'x', headaxislength = 2, headlength = 1, pivot = 'mid', scale = 1.1)
    return

我想要的结果看起来像这样:

示例数组 1: [[-np.pi -np.pi/2] [-np.pi 0]]

图1:It's a 2x2 grid, with each box being a certain color and having an arrow in it that points in the right direction. -180° (the first angle) is dark red, -90° is green.

到目前为止,代码工作正常,当我为不同的数组再次调用相同的函数时,就会出现问题。

示例数组 2: [[-np.pi/2 -np.pi/2] [-np.pi 0]]

图2:I changed the first angle here. The color of the first angle did change and is now green, as it should be. A new arrow with the new angle has been added in the box. The problem is that the first arrow is still there. This means that there are now two arrows in the box, one of which is wrong.

我试图找到一个命令,可以在生成第二张图片之前删除第一张图片。我无法找到这样的命令,而且我不知道还能尝试什么,因为我实际上不知道是什么导致了问题。

对于我将要编写的最终程序,所有箭头都在不断改变方向。对于这个问题,它只添加新箭头但不删除旧箭头,最后基本上只有黑色圆圈。如何让它在添加新箭头之前删除旧箭头?它对颜色也没有这样做,颜色效果很好,所以我很困惑。

非常感谢您的帮助!

python matplotlib jupyter-notebook anaconda
1个回答
0
投票

您使用

figure
获得,哦,好吧,一个新图形,这样您稍后绘制的内容就不会叠加在您之前绘制的内容上。

© www.soinside.com 2019 - 2024. All rights reserved.