我想以特定模式繪制影像,如下圖所示

我想了解使用 python 繪制影像的最佳方法是什么。我使用以下方法以網格模式繪制影像。輸出看起來像這樣

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib as mpl
from matplotlib.dates import date2num
import matplotlib.dates as mdates
from mpl_toolkits.axes_grid1 import ImageGrid
img0 = f['B00'][i_ant, :, :]
img1 = f['B01'][i_ant, :, :]
img2 = f['B02'][i_ant, :, :]
img3 = f['B03'][i_ant, :, :]
img_arr = [img0,img1,img2,img3]
fig = plt.figure(figsize=(5., 5.))
grid = ImageGrid(fig, 111,
nrows_ncols=(2, 2), # creates 2x2 grid of axes
axes_pad=0, # pad between axes
)
for ax, im in zip(grid, img_arr):
ax.imshow(im)
plt.show()
uj5u.com熱心網友回復:
對于方形螺旋的每個附加級別,添加兩個邊,除了最后一個只需要一個邊。位置每一步遞增或遞減 1 ,i,j并且在每個拐角處方向旋轉 90 度。
這就是它的作業方式:
import matplotlib.pyplot as plt
import numpy as np
N = 6
fig = plt.figure(figsize=(16, 18), constrained_layout=True)
spec = fig.add_gridspec(ncols=N * 2 - 1, nrows=N * 2 - 1)
i, j = N - 2 N % 2, N - 1
dir_i, dir_j = 1, - 1
plot_num = 0
for k in range(1, N 1):
for _ in range(2): # add two strokes of k subplots (only 1 when k==N)
for _ in range(k):
ax = fig.add_subplot(spec[i, j])
ax.imshow(np.random.rand(3, 3))
ax.set_xlabel(f'{plot_num} [{k}]', fontsize=18)
plot_num = 1
i = dir_i
j = dir_j
if k == N:
break
dir_i, dir_j = -dir_j, dir_i
plt.show()

要準確復制原始編號,可以繪制一些同心正方形。對于奇數N,最后一個方格非常不規則,只有 2 條邊,最后一條線跳到另一邊,比其他方格少一個方格。
對于奇怪的 2 和 3 編號,可以引入一些重新編號:
import matplotlib.pyplot as plt
import numpy as np
N = 6
fig = plt.figure(figsize=(16, 18), constrained_layout=True)
spec = fig.add_gridspec(ncols=2 * N - 1, nrows=2 * N - 1)
plot_num = 0
for k in range(2, N 2, 2):
# i, j = N - 2 N % 2, N - 1
i, j = N - k N % 2, N - 1
dir_i, dir_j = 1, - 1
for side in range(4): # add four strokes of k subplots (only 2 when k==N)
for _ in range(k - 1):
ax = fig.add_subplot(spec[i, j])
modified_plot_num = 5 - plot_num if plot_num in (2, 3) else plot_num
ax.imshow(np.random.rand(6, 6), cmap='inferno')
ax.set_xlabel(f'{modified_plot_num} [{k}]', fontsize=18)
plot_num = 1
i = dir_i
j = dir_j
if plot_num == N * N: # for odd N, the very last cell should be skipped
break
if k == N 1:
if side == 0: # for last side of uneven square: jump to the other side
dir_i, dir_j = -dir_i, -dir_j
i, j = i - 1, 2 * N - 2
elif side == 1:
break
dir_i, dir_j = -dir_j, dir_i
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416776.html
標籤:
