Skip to main content
 首页 » 编程设计

python之绘制两个不同大小的网格。较小的网格以较大的中心为中心

2024年11月01日8cyq1162

我正在尝试得到这样的东西(图像中充满了红色方 block (我只画了几个)): .扩展我想要的内容:我希望红色方 block 在黄色方 block 中居中,如图所示(但所有黄色方 block 中都有红色方 block )。

那里发生的事情是较大的窗口(黄色网格)彼此重叠了一半的大小,在这种情况下较小的窗口,大窗口的一半大小,(红色方 block )以中心为中心的大 window 。我能得到的最远的是使用这个 Multiple grids on matplotlib我基本上是在使用他们的代码,但为了让事情绝对清楚,我包括了代码:

编辑:感谢罗格斯大学,我得到了我想要的。这是一个稍微编辑和缩短的版本。这段代码给出了我想要的四个黄色网格交叉点的第一个中心。

import matplotlib.pyplot as plt 
from matplotlib.pyplot import subplot 
from scipy.misc import imread 
import numpy as np 
import matplotlib.cm as cmps 
import matplotlib.collections as collections 
 
 
i = 1 
initial_frame = 1 
 
 
ax = subplot(111) 
 
 
bg = imread("./png/frame_" + str("%05d" % (i + initial_frame) ) + ".png").astype(np.float64) 
 
# define the normal (yellow) grid 
ytcks = np.arange(16,bg.shape[0],32) 
xtcks = np.arange(16,bg.shape[1],32) 
 
# plot the sample data 
ax.imshow(bg, cmap=plt.cm.Greys_r, interpolation='none') 
 
ax.set_xticks(xtcks) 
ax.set_xticks(xtcks+16, minor=True) 
 
ax.set_yticks(ytcks) 
ax.set_yticks(ytcks+16, minor=True) 
 
ax.xaxis.grid(True,'minor', linestyle='--', lw=1., color='y') 
ax.yaxis.grid(True,'minor', linestyle='--', lw=1., color='y') 
 
ax.xaxis.grid(True,'major', linestyle='--', lw=0.5, color='g') 
ax.yaxis.grid(True,'major', linestyle='--', lw=0.5, color='g') 
 
plt.show() 

请您参考如下方法:

根据我的其他答案中的示例数据 z:

# define the normal (yellow) grid 
tcks = np.arange(0,90,10) 
 
fig, ax = plt.subplots(figsize=(8,8)) 
 
# plot the sample data 
ax.imshow(z, cmap=plt.cm.Greys_r, interpolation='none', vmin=0.4, vmax=1.5, extent=[0,z.shape[0],0,z.shape[1]]) 
 
ax.set_xticks(tcks) 
ax.set_xticks(tcks+5, minor=True) 
 
ax.set_yticks(tcks) 
ax.set_yticks(tcks+5, minor=True) 
 
ax.xaxis.grid(True,'minor', linestyle='--', lw=1., color='y') 
ax.yaxis.grid(True,'minor', linestyle='--', lw=1., color='y') 
 
ax.xaxis.grid(True,'major', linestyle='-', lw=1., color='r') 
ax.yaxis.grid(True,'major', linestyle='-', lw=1., color='r') 
 
ax.set_xlim(0,80) 
ax.set_ylim(0,80) 

我希望这比用多边形绘图快得多。