[英]Rotate a matrix with Matplotlib
I am rotating anxn matrix (n = 20, although it could change) 30 degrees rightwards using Matplotlib's transformation methods.我30度向右使用Matplotlib的转化方法旋转anxn矩阵(N = 20,虽然它可以变化)。
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 .我试图通过
np.flip()
或ax.imshow(origin = 'lower')
反转索引,但它也反转三角形,所以我需要发现如何设置转换原点。
Defintley, this is what I would like to obtain : Defintley,这就是我想获得的:
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?
也许通过返回半个像素的 imshow 方法? 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')
And here is the code trying to rotate it:这是尝试旋转它的代码:
im = ax.imshow(matrix, cmap = 'Spectral')
im.set_transform(mtransforms.Affine2D().skew(30, 0) + ax.transData)
ax.plot(transform = trans_data)
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.我没有使用 Matplotlib 的 Triangle 类,因为三元图是通过插值操作表示的,我想表示原始矩阵值。
I'd really appreciate some one's help.我真的很感激有人的帮助。 Thank you very much in advance.
非常感谢您提前。
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.您可以将其与 x 方向的平移链接起来,而不是更改倾斜变换的原点,以实现您正在寻找的变换。
Note that the skew
transform takes an angle in radians (you were using it with degrees).请注意,
skew
变换采用以弧度表示的角度(您使用的是度数)。 There is an equivalent skew_deg
transform if you want to work in degrees, but here I just work in radians.如果你想以度为单位工作,有一个等效的
skew_deg
变换,但在这里我只以弧度工作。
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).还要注意,我认为你想要一个等腰三角形,底和高都等于 20(或者你选择 N 的任何值),你想要的角度不是 30 度,而是实际上 arctan(1/2) (=26.56度)。
The amount you need to translate in the x direction is xtrans = N * np.tan(angle)
.您需要在 x 方向平移的量是
xtrans = N * np.tan(angle)
。
You can chain transforms easily in matplotlib.您可以在 matplotlib 中轻松链接变换。 Here we can skew first, then translate:
这里我们可以先倾斜,再翻译:
mtransforms.Affine2D().skew(-angle, 0).translate(xtrans, 0)
Note that this script works for any value of N.请注意,此脚本适用于任何 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()
I finally obtain an equilateral triangle scaling y-axis.Here I show the code.我终于得到了一个等边三角形缩放 y 轴。这里我展示了代码。
Therefore, it allows converting a matrix into an equilateral triangle , what answer my previous question:因此,它允许将矩阵转换为等边三角形,这回答了我之前的问题:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.