【问题标题】:How to plot series of images onto a particular map如何将一系列图像绘制到特定地图上
【发布时间】:2022-01-19 01:31:01
【问题描述】:

我想以特定模式绘制图像,如下图所示

我想了解使用 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()

【问题讨论】:

  • 你的代码产生的情节是什么样子的?
  • 刚刚添加了它看起来的情节
  • 图像是否需要按照它们在示例图像中出现的顺序添加(从中心开始逆时针对角线)?
  • 是的,从中心开始的顺序相同
  • 理想情况下,它适用于 36 张图片。

标签: python matplotlib ggplot2 plot


【解决方案1】:

对于方形螺旋的每一层,都会添加两条边,除了最后一层只需要一条边。 i,j 位置每一步递增或递减 1,并且在每个拐角处方向旋转 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 和 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()

【讨论】:

  • 我认为这可能是最接近的解决方案。太感谢了。我可能需要修改位置以匹配地图上显示的图像编号。顺便说一句,可以将图像倾斜 45 度吗? @JohanC​​pan>
  • 请参阅How to rotate a simple matplotlib Axes 了解旋转子图。它不是很灵活,因此在不知道确切用法的情况下实施它没有太大意义。
  • 感谢旋转图的链接。是的,出于某种原因,2 和 3 的顺序与其他的不同。希望我可以将图像编号 4 更改为位于 0 之上。
  • 因为我必须在将这张地图中的所有 36 张图像组合后制作一张图像。所以这些图片的定位对我来说非常重要。
  • 这个解决方案就像一个魅力。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
相关资源
最近更新 更多