可视化和旋转矩阵 [英] Visualising and rotating a matrix
问题描述
我正在尝试可视化一个大约为500x500的上三角矩阵.除此之外,我还尝试旋转该图像,以使其看起来像三角形朝上:
I am trying to visualise an upper triangular matrix that is approximately 500x500. Alongside this I am also trying to rotate that image so that it looks like the triangle is pointing upward:
(这是通过拍摄图形设备的快照然后旋转该图像来实现的).
(This was achieved by taking a snapshot of a graphics device and then rotating that image).
在该图像中,每一列和每一行都需要指定宽度.
As in that image each column and row needs to have it's width specified.
我尝试将image()
函数与grid
包一起使用(使用旋转了45度的观察面板),但是不起作用.有谁知道更好的解决方案?
I have tried using the image()
function with the grid
package (using a viewpanel that is rotated by 45 degrees) however that does not work. Does anybody know a better solution?
推荐答案
以下是使用基础图形的rasterImage
的简单愚蠢方法:
Here a simple and stupid approach using base graphics' rasterImage
:
plotTriMatrix <- function(x) {
## clear lower triangle
x[lower.tri(x)] <- NA
## calculate diag
nr <- nrow(x)
nc <- ncol(x)
d <- sqrt(nr^2 + nc^2)
d2 <- 0.5 * d
## empty plot area
plot(NA, type="n", xlim=c(0, d), ylim=c(0, d), xlab="", ylab="", asp=1)
## plot matrix and rotate 45
rasterImage(as.raster(x),
xleft=d2, xright=d2+nc, ybottom=-d2, ytop=-d2+nr,
interpolate=FALSE, angle=45)
}
示例:
set.seed(123)
m <- matrix(runif(100), 10, 10)
plotTriMatrix(m)
这篇关于可视化和旋转矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!