我正在使用Matplotlib的转换方法旋转一个n x n矩阵(n = 20,尽管它可能会改变),向右旋转30度。
错误出现的原因是旋转是从顶部而不是底部执行的。我已经尝试通过np.flip()
或ax.imshow(origin='lower')
来反转索引,但这也会反转三角形,所以我需要发现如何设置变换原点。
毫无疑问,这就是我想要得到的结果:
请注意,构成对角线矩阵的小正方形将变为三角形。可以做到这一点吗?也许通过返回半个像素的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的三角形类,因为三元图是通过插值操作表示的,而我想表示原始矩阵值。
我会非常感激有人帮忙。非常感谢您的预先帮助。
angle
和xtrans 参数
,angle = np.arctan(np.pi/3)
,但它不起作用。 - Miguel Gonzalez