【发布时间】: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