【问题标题】:Rotate a matrix with Matplotlib使用 Matplotlib 旋转矩阵
【发布时间】:2020-10-29 13:01:21
【问题描述】:

我正在使用 Matplotlib 的 transformation 方法旋转一个 n x n 矩阵(n = 20,尽管它可能会改变)向右 30 度

错误出现是因为旋转是从顶部而不是从底部进行的。 我尝试通过np.flip()ax.imshow(origin = 'lower') 反转索引,但它也反转了三角形,所以我需要了解如何设置变换原点

Defintley,这是我想要得到的

请注意,符合对角矩阵的小正方形将变成三角形。这可以做到吗?也许通过返回半个像素的 imshow 方法? 其余像素将保持不变(变形的小方块)。

这是生成矩阵的代码(起点):

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

matrix = np.random.rand(20,20)

# Generate a boolean matrix (same shape than 'matrix') and select lower triangle values:

condition = np.tril(np.ones((matrix.shape))).astype(np.bool)
triangle = np.where(condition, matrix, np.nan)

fig, ax = plt.subplots(figsize = (8,8))

ax.imshow(triangle, cmap = 'Spectral')

这是试图旋转的代码

im = ax.imshow(matrix, cmap = 'Spectral')
im.set_transform(mtransforms.Affine2D().skew(30, 0) + ax.transData)
ax.plot(transform = trans_data)

我没有使用 Matplotlib 的 Triangle 类,因为三元图是通过插值操作表示的,我想表示原始矩阵值。

我真的很感激有人的帮助。非常感谢您。

【问题讨论】:

    标签: python matplotlib matrix imshow affinetransform


    【解决方案1】:

    您可以将其与 x 方向的平移链接起来,而不是更改倾斜变换的原点,以实现您正在寻找的变换。

    请注意,skew 变换采用弧度表示的角度(您使用的是度数)。如果您想以度为单位工作,则有一个等效的skew_deg 转换,但在这里我只以弧度工作。

    还请注意,我认为您想要一个底和高都等于 20(或您选择 N 的任何值)的等腰三角形,您想要的角度不是 30 度,而是实际上是 arctan(1/2) (=26.56 度)。

    x 方向需要平移的量是xtrans = N * np.tan(angle)

    您可以在 matplotlib 中轻松链接转换。这里我们可以先倾斜,再平移:

    mtransforms.Affine2D().skew(-angle, 0).translate(xtrans, 0)
    

    请注意,此脚本适用于任何 N 值。

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.transforms as mtransforms
    
    N = 20
    matrix = np.random.rand(N, N)
    
    # Generate a boolean matrix (same shape than 'matrix') and select lower triangle values:
    
    condition = np.tril(np.ones((matrix.shape))).astype(np.bool)
    triangle = np.where(condition, matrix, np.nan)
    
    fig, ax = plt.subplots(figsize = (8,8))
    
    im = ax.imshow(triangle, cmap = 'Spectral')
    
    angle = np.arctan(1/2)
    xtrans = N * np.tan(angle)
    im.set_transform(mtransforms.Affine2D().skew(-angle, 0).translate(xtrans, 0) + ax.transData)
    
    ax.set_xlim(-0.5, N + 0.5)
    plt.show()
    

    对于 N = 20

    对于 N = 30

    【讨论】:

    • 你好@tmdavison。非常感谢您的所有游览时间。我最终不太明白你在做什么。对不起,我解释错了。我想将矩阵旋转成一个等边三角形,而不是等腰。我正在尝试更改anglextrans parameters angle = np.arctan(np.pi/3),但它不起作用。
    【解决方案2】:

    我终于得到了一个等边三角形缩放y轴。这里我显示代码。

    因此,它允许将矩阵转换为等边三角形,这回答了我之前的问题:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.transforms as mtransforms
    import matplotlib
    
    bins = 50
    Z = np.random.rand(bins, bins)
    
    # Generate a boolean matrix (same shape than 'matrix') and select lower triangle values:
    condition = np.tril(np.ones((Z.shape))).astype(np.bool)
    Z = np.where(condition, Z, np.nan)
    
    fig, ax = plt.subplots(figsize = (8,8))
    im = ax.imshow(Z, cmap = 'Spectral')
    
    # Required angles (in Rad)
    alpha = np.arctan(1/2)        # 26 deg angle, in radians.
    beta = np.arctan(np.pi/6)     # 30 deg angle, in radians.
    
    # Coefficients:
    xtrans = np.sin(beta) * bins
    scale_y = np.cos(beta)     
    
    # Transformation:
    im.set_transform(mtransforms.Affine2D().skew      (-alpha, 0)
                                           .scale     (1,scale_y)
                                           .translate (xtrans, 0) 
                                            + ax.transData)
    
    ax.set_ylim(bins,-5)
    ax.set_xlim(-5,bins)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 2014-11-30
      相关资源
      最近更新 更多