簡體   English   中英

使用 Matplotlib 旋轉矩陣

[英]Rotate a matrix with Matplotlib

30度向右使用Matplotlib的轉化方法旋轉anxn矩陣(N = 20,雖然它可以變化)。

出現錯誤是因為旋轉是從頂部而不是從底部執行的。 我試圖通過np.flip()ax.imshow(origin = 'lower')反轉索引,但它也反轉三角形,所以我需要發現如何設置轉換原點

Defintley,這就是我想獲得的

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 的 Triangle 類,因為三元圖是通過插值操作表示的,我想表示原始矩陣值。

我真的很感激有人的幫助。 非常感謝您提前。

您可以將其與 x 方向的平移鏈接起來,而不是更改傾斜變換的原點,以實現您正在尋找的變換。

請注意, skew變換采用以弧度表示的角度(您使用的是度數)。 如果你想以度為單位工作,有一個等效的skew_deg變換,但在這里我只以弧度工作。

還要注意,我認為你想要一個等腰三角形,底和高都等於 20(或者你選擇 N 的任何值),你想要的角度不是 30 度,而是實際上 arctan(1/2) (=26.56度)。

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

您可以在 matplotlib 中輕松鏈接變換。 這里我們可以先傾斜,再翻譯:

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 在此處輸入圖片說明

對於 N = 30 在此處輸入圖片說明

我終於得到了一個等邊三角形縮放 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM