使用 Matplotlib 旋转矩阵 [英] Rotate a matrix with Matplotlib

查看:104
本文介绍了使用 Matplotlib 旋转矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am rotating a n x n matrix (n = 20, although it could change) 30 degrees rightwards using Matplotlib's transformation methods.

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

The error shows up because rotation is perfomed from the top and not from the base. I have tried to inverse the index through np.flip() or ax.imshow(origin = 'lower') but it also invert the triangle, so I need to discovered how to set the transformation origin point.

Defintley,这是我想要的:

Defintley, this is what I would like to obtain:

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

Note that the little squares that conforms the diagonal matrix would be turned into triangles. Could this be done? Maybe by an imshow method that returns half a pixel? The rest of the pixeles would stay the same (deformed little squares).

以下是用于生成矩阵的代码(起点):

Here is the code for generate the matrix (starting point):

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类,因为三元图是通过插值运算来表示的,而我想表示原始矩阵值.

I am not using Triangle class of Matplotlib because the ternary diagram is represented througout an interpolation operation, and I want to represent the original matrix values.

我真的很感谢别人的帮助.预先非常感谢您.

I'd really appreciate some one's help. Thank you very much in advance.

推荐答案

您可以更改x方向的平移链接它,而不用改变偏斜变换的原点,从而实现所需的变换.

Instead of changing the origin of the skew transformation, you could chain it with a translation in the x direction to achieve the transformation you are looking for.

请注意, skew 变换以弧度表示一个角度(您将其与度一起使用).如果要以度为单位,则有一个等效的 skew_deg 变换,但是这里我只是以弧度为单位.

Note that the skew transform takes an angle in radians (you were using it with degrees). There is an equivalent skew_deg transform if you want to work in degrees, but here I just work in radians.

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

Note also that I think you want to have an isosceles triangle with base and height both equal to 20 (or whatever you choose N to be), the angle you want is not 30 degrees, but actually arctan(1/2) (=26.56deg).

您需要在x方向上平移的量为 xtrans = N * np.tan(angle).

The amount you need to translate in the x direction is xtrans = N * np.tan(angle).

您可以在 matplotlib 中轻松链接转换.在这里,我们可以先倾斜,然后翻译:

You can chain transforms easily in matplotlib. Here we can skew first, then translate:

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

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

Note that this script works for any value of 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

这篇关于使用 Matplotlib 旋转矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆