Python 箭袋角度问题 : What is the difference between angles ="uv" and angles ="xy"

标签 python matplotlib plot

我有一个想要正确绘制的矢量场。当我按原样使用 quiver 时,它似乎输出了正确的绘图:

enter image description here

但是,如果我遵循准则来获得颤动比例并显示向量的正确角度(如此处给出的 Understanding Matplotlib's quiver plotting ),那么我不会得到相同的图:

enter image description here

有人知道为什么吗?

这是代码:

import numpy as np
import matplotlib.pyplot as plt

xx = np.array([[0. , 0.2, 0.4, 0.6, 0.8, 1. ],
       [0. , 0.2, 0.4, 0.6, 0.8, 1. ],
       [0. , 0.2, 0.4, 0.6, 0.8, 1. ],
       [0. , 0.2, 0.4, 0.6, 0.8, 1. ],
       [0. , 0.2, 0.4, 0.6, 0.8, 1. ],
       [0. , 0.2, 0.4, 0.6, 0.8, 1. ]])    

yy = np.array([[-200. , -200. , -200. , -200. , -200. , -200. ],
   [-144.6, -144.6, -144.6, -144.6, -144.6, -144.6],
   [ -89.2,  -89.2,  -89.2,  -89.2,  -89.2,  -89.2],
   [ -33.8,  -33.8,  -33.8,  -33.8,  -33.8,  -33.8],
   [  21.6,   21.6,   21.6,   21.6,   21.6,   21.6],
   [  77. ,   77. ,   77. ,   77. ,   77. ,   77. ]])


dx = np.array([[ 6.18291908e-09,  3.70076924e-06,  3.42538903e-06,
            -4.38426006e-07, -2.55227640e-06,  6.18291908e-09],
           [ 2.17018678e-06,  2.51500956e-04,  1.73477082e-04,
            -1.23855846e-04, -2.29881121e-04,  2.17018678e-06],
           [ 2.44470726e-05,  3.79715107e-03,  2.49497711e-03,
            -2.05506466e-03, -3.60106480e-03,  2.44470726e-05],
           [ 2.87699956e-04,  3.56327893e-02,  2.23694354e-02,
            -2.17573718e-02, -3.50990203e-02,  2.87699956e-04],
           [ 2.13917741e-03,  1.07630739e-01,  1.09098114e-01,
            -1.73049517e-01, -1.15168685e-01,  2.13917741e-03],
           [ 3.13827774e-04, -3.87236380e-02, -4.33945841e-02,
             7.12401001e-02,  3.91662321e-02,  3.13827774e-04]])

dy = np.array([[0.24105279, 0.24104844, 0.24104251, 0.24104319, 0.24104954,
        0.24105279],
       [0.2409212 , 0.24103392, 0.24123878, 0.24125286, 0.24105669,
        0.2409212 ],
       [0.23870441, 0.24127457, 0.24563927, 0.24579267, 0.24151955,
        0.23870441],
       [0.21110992, 0.24513396, 0.30153966, 0.30228133, 0.24619877,
        0.21110992],
       [0.07941149, 0.14922776, 0.70945221, 0.67353021, 0.12818932,
        0.07941149],
       [0.07717276, 0.04937775, 0.18100361, 0.16734886, 0.03621946,
        0.07717276]])

# PLOT 1 (Correct one)

plt.figure(1, dpi = 300)
plt.quiver(xx, yy, dx, dy)
plt.title("Plot 1")
plt.show()

# PLOT 2 (Something is wrong her)
# Inspiration from: https://stackoverflow.com/questions/36534381/understanding-matplotlibs-quiver-plotting

plt.figure(2, dpi = 300)
plt.quiver(xx, yy, dx, dy, angles="xy", scale_units='xy', scale=1)
plt.title("Plot 2")
plt.show()

编辑: 请看一下 dx、dy 数据:dx 数据小于 dy 数据,因此,向量应始终指向上方,而不是视线方向。

最佳答案

根据docs我想说,差异可以解释如下:

如果您使用angles = 'uv',箭头纵横比将设置为默认设置,而当您使用angles = 'xy'时,您实际上打印从 (x,y) 指向 (x+u,y+v) 的向量。换句话说,图 2 中的向量缩放得非常糟糕,但该函数执行了它应该执行的操作。

uv 模式下,箭头轴独立于 x,y 轴 ( explained here )。箭头纵横比(即箭头宽度与其高度的比率)设置为 1。

在你的问题中,你说你想正确绘制向量场,因此我认为你想使用xy-模式。我尝试了一些设置,也许您对此感到满意。

plt.quiver(xx, yy, dx, dy, angles="xy",scale = 8,width = 0.004)

Using angles=xy

绘制矢量场的另一个选项是使用streamplot,它可能会产生满足您需求的结果。

关于Python 箭袋角度问题 : What is the difference between angles ="uv" and angles ="xy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69146016/

相关文章:

python - 使用 matplotlib 创建条形图,其中 x 轴为日期,宽度为时间增量

python - 如何使用点绘制 Pandas 数据框的两列

r - 如何有效地将多个 rgl 图连接成一个图?

python - 将 pandas 数据帧作为 xlsx 文件写入 azure blob 存储,而不创建本地文件

python - 如何在 matplotlib 中的极轴上使用对数刻度

python - 从嵌套字典创建 dfs 并计算字符串以创建带有子图的图

python - 将错误栏添加到 Pandas 图中的单个列

python - 关于 matplotlib 的 .show()

python - 你能在 python 中轻松创建一个类似列表的对象,它使用类似描述符的东西来描述它的项目吗?

python - 为什么 Python 的 'all' 函数这么慢?