使用Matplotlib旋转矩阵

4

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

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

毫无疑问,这就是我想要得到的结果

2

请注意,构成对角线矩阵的小正方形将变为三角形。可以做到这一点吗?也许通过返回半个像素的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')

1

这里是尝试旋转该图片的代码:

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

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

我会非常感激有人帮忙。非常感谢您的预先帮助。

2个回答

3

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

请注意,skew 变换需要一个弧度角(您使用的是角度)。如果您想按度数工作,则有等效的 skew_deg 变换,但此处我仅使用弧度。

还要注意,我认为您希望拥有底边和高度均为 20(或您选择的 N)的等腰三角形,您想要的角度不是 30 度,而实际上是 arctan(1/2)(=26.56deg)。

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

您可以在 matplotlib 中轻松地链接多个变换。这里我们首先进行 skew 变换,然后进行平移:

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,结果如下图所示:enter image description here 对于 N = 30,结果如下图所示:enter image description here

你好 @tmdavison。非常感谢你的所有时间。我最终并不是很理解你在做什么。抱歉,我表达得不太清楚。我想将矩阵旋转成一个等边三角形,而不是等腰三角形。我正在尝试更改 anglextrans 参数angle = np.arctan(np.pi/3),但它不起作用。 - Miguel Gonzalez

0

我终于得到了一个沿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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接