Python 使用Matplotlib旋转矩阵

Python 使用Matplotlib旋转矩阵,python,matplotlib,matrix,imshow,affinetransform,Python,Matplotlib,Matrix,Imshow,Affinetransform,我使用Matplotlib的变换方法将一个nxn矩阵(n=20,尽管它可能会改变)向右旋转30度 出现错误,因为旋转是从顶部执行的,而不是从底部执行的。 我尝试通过np.flip()或ax.imshow(原点='lower')反转索引,但它也反转三角形,因此我需要了解如何设置变换原点 确实,这是我想要得到的: import numpy as np import matplotlib.pyplot as plt import matplotlib.transforms as mtransforms

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

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

确实,这是我想要得到的:

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')

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

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

下面是试图旋转它的代码:

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

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


我真的很感谢别人的帮助。非常感谢您的光临。

不必更改倾斜变换的原点,您可以将其与x方向的平移链接起来,以实现所需的变换

请注意,
skew
变换采用弧度表示的角度(您将其与度一起使用)。如果你想以度为单位,有一个等价的
skew_deg
变换,但这里我只以弧度为单位

还要注意的是,我认为你想要一个等腰三角形,底面和高度都等于20(或者你选择的任意N),你想要的角度不是30度,而是弧角(1/2)(=26.56度)

需要在x方向上平移的量是
xtrans=N*np.tan(角度)

可以在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

我最终得到了一个等边三角形,缩放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()

你好@tmdavison。非常感谢您一直来参观。我最终不太明白你在做什么。对不起,我解释错了。我想把矩阵旋转成等边三角形,而不是等腰三角形。我试图更改
角度
xtrans参数
角度=np.arctan(np.pi/3)
,但它不起作用。