【问题标题】:How to get arrow directions (i.e. U and V) in a matplotlib.pyplot.quiver plot?如何在 matplotlib.pyplot.quiver 图中获取箭头方向(即 U 和 V)?
【发布时间】:2020-08-19 17:25:26
【问题描述】:

我想知道在绘制matplotlib.pyplot.quiver 之后,是否有任何方法可以使用Axes 对象来获取箭头方向(即UV)。例如:

fig, ax = plt.subplots()
plt.quiver(X=..., Y=..., U=..., V=...)
ax.collections[0].something_that_returns_UV?

我知道ax.collections[0].get_offsets() 会返回XY 值,但我对UV 值感兴趣。

用例:自动测试绘图 (example)。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    ax.quiverkey 创建的集合是matplotlib.quiver.Quiver 类型。可以直接获取XYUVax.collections[0].X等。

    import matplotlib.pyplot as plt
    import numpy as np
    
    X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
    U = np.cos(X)
    V = np.sin(Y)
    fig1, ax = plt.subplots()
    Q = ax.quiver(X, Y, U, V)
    
    print('X:', ax.collections[0].X)
    print('Y:', ax.collections[0].Y)
    print('U:', ax.collections[0].U)
    print('V:', ax.collections[0].V)
    

    【讨论】:

    • 啊,我真是太傻了,我搜索了很多都没有找到!我认为像get_offsets 这样的函数应该可以处理这个问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 2021-12-22
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多