Matplotlib로 행렬 회전
나는 회전 (이 변할 수 있지만, N = 20) anxn 행렬을 30도 우측 하기 matplotlib의 사용 변환 방법.
오류가 회전 상단에서 perfomed 아닌 기지에서하기 때문에 나타납니다. 인덱스를 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 클래스를 사용하지 않습니다.
누군가의 도움을 정말 감사하겠습니다. 미리 감사드립니다.
답변
기울이기 변환의 원점을 변경하는 대신 x 방향의 변환으로 연결하여 원하는 변환을 얻을 수 있습니다.
있습니다 skew변환 라디안 각도를 (당신도 함께 사용했다)합니다. skew_deg각도로 작업 하려면 동등한 변환이 있지만 여기서는 라디안으로 작업합니다.
또한 밑변과 높이가 모두 20 (또는 N을 선택하는 것)과 같은 이등변 삼각형을 원한다고 생각합니다. 원하는 각도는 30 도가 아니라 실제로 arctan (1/2) (= 26.56)입니다. deg).
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()