Matploltib颤动图:参数顺序

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

给出纵横比值的2D数组(例如,由高程模型的纵横比表示的方位角),我正在创建quiver plot using matplotlib。将其放置在长宽比值的彩色矩阵上,以进行检查。

该代码的工作方式是创建我想要的内容,但仅在参数与我期望的内容相反的地方创建。我犯了一个简单的错误,但找不到它。


问题:尽管matplotlib.pyplot.quiver()期望quiver([X, Y], U, V, [C], **kw),但是为什么我的代码仅给出使用quiver([X, Y], V, U)的期望答案(即U和V相反)?


[顺便说一下,在绘制时,我已经将plt.imshow的原点移到了lower(如here所述)。我认为问题出在与我的索引编制等有关的地方。

下面的代码(使用python 3.5和matplotlib v3.x):

import numpy as np
import matplotlib.pyplot as plt


def compassBearing_to_standardPosition__degrees_counterClockwise(bearing_deg):
    """Vector magnitude and direction calculations assume angle is relative to the x axis 
          i.e. 0 degrees north is at 3 o'clock
    Adjust compass bearings to be relative to standard position
    """
    std_pos=(450 - bearing_deg) % 360
    return(std_pos)

def calculate_U_and_V__vector_magnitude_and_direction(angle_degrees, magnitude=1):
    """Calculates the components of a vector given in magnitude (U) and direction (V) form
    angle: Expected that angles are in standard position 
            i.e. relative to the x axis or where 3 o'clock is zero and not the compass bearing 
            where 12 o'clock is 0
    magnitude: defaults to 1
    """
    angle_rad=np.deg2rad(angle_degrees)
    x = magnitude * np.cos(angle_rad) # change in x == U
    y = magnitude * np.sin(angle_rad) # change in y == V
    return(x,y)

def array_indices(arr, indexing='xy'):
    """Calculates index positions of each cell in array
    These can be used to map to e.g. when creating a quiver plot

    indexing: Giving the string 'ij' returns a meshgrid with
        matrix indexing, while 'xy' returns a meshgrid with Cartesian indexing.
        In the 2-D case with inputs of length M and N, the outputs are of shape
        (N, M) for 'xy' indexing and (M, N) for 'ij' indexing.
    """
    nrows, ncols = arr.shape
    nx = 1
    ny = 1
    x = np.linspace(0, ncols-1, ncols)
    y = np.linspace(0, nrows-1, nrows)
    #y = np.linspace(nrows-1, 0, nrows) # note that the largest vlue is first
    xi, yi = np.meshgrid(x, y, indexing=indexing)
    return(xi, yi)

#Create a toy aspect grid (degrees North)
aspect_grid=np.array([[ 216,  226,  151],
       [  74,  323,  268],
       [ 177,  204,   84]])

#Get the array indices
x,y=array_indices(aspect_grid, indexing='xy')

#Get U and V 
x_change,y_change=calculate_U_and_V__vector_magnitude_and_direction(aspect_grid.flatten())

#Plot quiver over imshow
cmap = 'twilight_shifted' # this will expect matplotlib v3.x
plt.imshow(np.floor(aspect_grid), cmap=cmap, origin='lower')
plt.colorbar(label="Aspect (degrees N)")
plt.quiver(x, y, y_change, x_change, pivot='middle') # <<< why not x,y,x_change,y_change?
plt.title("Surface aspect values")
plt.show()
python matplotlib calculus
1个回答
0
投票

当将aspect_grid数组传递到calculate_U_and_V__vector_magnitude_and_direction时,您没有将它们从绝对方位转换为逆时针方向,因为compassBearing_to_standardPosition__degrees_counterClockwise中未调用calculate_U_and_V__vector_magnitude_and_direction。由于两个约定的90度未对准,这导致cos(angle)对应于y分量,导致sin(angle)对应于x分量(由于属性cos(x - pi/2) == sin(x))。为了解决这个问题,您只需执行以下操作即可使用已设置的转换(它可以将轴承正确转换为标准位置):

#...
angle_degrees = compassBearing_to_standardPosition__degrees_counterClockwise(angle_degrees)
angle_rad=np.deg2rad(angle_degrees)
#...

calculate_U_and_V__vector_magnitude_and_direction中。然后,您可以使用

plt.quiver(x, y, x_change, y_change, pivot='middle')

并获得正确的结果:

enter image description here

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