【问题标题】:Plotting two different sized grids. Smaller grid centred on the centre of the bigger绘制两个不同大小的网格。较小的网格以较大的中心为中心
【发布时间】:2013-08-06 13:22:03
【问题描述】:

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

发生的情况是较大的窗口(黄色网格)彼此重叠一半大小,在这种情况下,较小的窗口(大窗口大小的一半)(红色正方形)以中心为中心的大窗户。我能得到的最远的是使用这个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()

【问题讨论】:

  • 你用什么代码来制作图像?如果您不想使用标准网格选项,则可能多边形集合会起作用。它的工作量更大,但非常灵活。
  • 对我来说,不清楚你想要实现什么。你应该写得更清楚,这样更容易理解你的意思。此外,您应该包含到目前为止的代码,即使它没有为您提供所需的输出。现在,您要求其他人为您完成所有工作(因为您没有包含任何代码) - 如果您表明您已经付出了一些努力来尝试自己解决问题,那么您更有可能得到好的答案。
  • @nordev。抱歉,我一直认为它已经足够清楚了,并且由于我链接了我正在使用的内容,我不想用代码向问题发送垃圾邮件。我试图尽可能明确地表达我想要的东西(这真的很简单,只需将较小的正方形放在大正方形的中间即可)。现在这是一个更清楚的问题吗:)?不太确定如何让它变得更好......
  • @Rutger Kassies。你的意思是stackoverflow.com/questions/15968762/… 吗?

标签: python matplotlib


【解决方案1】:

鉴于我的其他答案中的示例数据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)

我希望这比使用多边形绘制要快得多。

【讨论】:

  • 是的,这可以解决问题(即使我不需要图像的第一个(在您的图像中)10 px 中的红线(但我不是在问这个,因为您帮助了已经很多了!)。谢谢罗格!
  • 很高兴它对您有用。在设置次要刻度时,可以通过切片 tcks 变量来移除边缘:'ax.set_yticks(tcks[1:-1]+5, minor=True)'
【解决方案2】:

提到更大和更小的网格有点令人困惑,因为在我看来它们的大小相同,但我假设您的意思是“主要”和“次要”网格。

好吧,用我的想法来模仿你的照片,看看这是否有意义:

import matplotlib.collections as collections
import numpy as np
import matplotlib.pyplot as plt

# generate some fake data, after:
# http://matplotlib.org/examples/images_contours_and_fields/pcolormesh_levels.html
dx, dy = 0.05, 0.05
y, x = np.mgrid[slice(1, 5, dy), slice(1, 5, dx)]
z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

# define the normal (yellow) grid
tcks = np.arange(0,90,10)

fig, ax = plt.subplots(figsize=(8,8), subplot_kw={'xticks': tcks, 'yticks': tcks})

# 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]])

# plot the yellow grid
ax.grid(True, linestyle='--', color='y', lw=1.5, alpha=1.0)

# define some random 'red' grid cells
custom_grid = []

for i in range(10):   
    x = np.random.randint(0,7) * 10 + 5
    y = np.random.randint(0,7) * 10 + 5

    polygon = plt.Rectangle((x, y), 10, 10)
    custom_grid.append(polygon)

p = collections.PatchCollection(custom_grid, facecolor='none', edgecolor='r', lw=1.5)
ax.add_collection(p)

它仍然有点不清楚,例如当你想显示“红色”网格单元时,什么时候不显示。

【讨论】:

  • 是的,对不起,我没有它的词汇,在我正在做的事情中,网格代表了一个感兴趣的窗口。大窗口的大小,在这种情况下是64x64,它们在水平和垂直方向上重叠一半(即4个黄色小方块组成一个大窗口(我混淆地称为“网格”,谢谢指点那出来)。我想要什么:在大窗口的中心用红色方块填充整个图像(4个黄色网格相交,就像你在一些例子中所做的那样)。我很确定你所做的会奏效给我,但让我检查一下。谢谢!
  • 好的,所以我已经仔细阅读了您的代码,是的,我看不出它有任何不工作的原因。我只需要找到一种方法来替换 for 循环以提高速度(我正在绘制数千张图像)。再次感谢!
  • 让混乱继续下去 :)。我想你想要一些'红色'网格单元格,因为你提到'但我得到了所有不必要的网格'。我想我现在明白你的意思了,Ticklocators 不允许偏移,所以你应该简单地手动设置刻度,一次用于主要刻度,一次用于次要刻度。如果您确实想要绘制“所有”网格单元格,那就是要走的路。我会在新的回复中回答以保持分开。
  • 再次澄清这一点。简单的网格会在中心之外有线条(嗯,这很令人困惑:))。我的意思是我不需要网格出现在图像所有侧面的第一个(在这种情况下)16 px 中。这有意义吗?
猜你喜欢
  • 1970-01-01
  • 2021-05-04
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多