2013-08-06 70 views
2

我想要得到像這樣的東西(圖像圖像是用紅色正方形填充的(我只畫了幾個)):enter image description here。展開我想要的內容:我希望紅色方塊如圖所示以黃色方塊爲中心(但在所有黃色方塊中都有紅色方塊)。繪製兩個不同大小的網格。以較大的中心爲中心的較小的網格

發生什麼事情是有更大的窗口(黃色網格)之間相互重疊的一半大小,在這種情況下,較小的窗口,大窗口的一半大小,(紅色正方形)集中在大窗戶的中心。我可以用這個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() 
+0

你用什麼代碼製作圖像?如果您不想使用標準網格選項,也許可以使用多邊形集合。它更多的工作,但非常靈活。 –

+0

對我來說,目前還不清楚你想達到什麼。你應該寫的更清楚,所以更容易理解你的意思。此外,即使它沒有給你想要的輸出,你也應該包含目前爲止的代碼。現在你要求其他人爲你做所有的工作(因爲你沒有包含任何代碼) - 如果你表明你已經付出了一些努力去嘗試自己解決問題,那麼你很可能會得到很好的答案。 – hooy

+0

@nordev。對不起,我總是認爲這已經夠清楚了,既然我把我正在使用的東西聯繫起來了,我不想用代碼發送垃圾郵件。我試圖儘可能明確我想要的東西(這很簡單,只需將大方塊中間的小方塊居中即可)。這是一個更明確的問題:)?不知道如何讓它變得更好... –

回答

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) 

enter image description here

我希望這是遠遠快於用多邊形繪圖。

+0

是的,這可以做到這一點(即使我不需要第一張照片中的紅線(在你的圖像中)10張圖像(但我不是問你這已經幫了很多了!)。 Rutger! –

+0

Glad it works for you。在設置小勾號時,可以通過切分tcks變量來刪除邊緣:'ax.set_yticks(tcks [1:-1] +5,minor = True)' –

1

提起更大和更小的網格是有點混亂,因爲對我來說,他們似乎相同大小的,但我相信你的意思是「主要」和「次要」網格。

嗯,有點模仿您的圖片與我的初衷,看看這使得任何意義:

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) 

enter image description here

它還是老樣子有點不清楚,例如當你想顯示'紅色'網格單元格,而不是。

+0

是的,對不起,我沒有它的詞彙表和我在做的網格代表一個感興趣的窗口。在這種情況下,大窗口的大小是64x64,並且它們在水平和垂直方向上重疊一半大小(也就是說,4個小的黃色方塊組成了一個大窗口(我將其混淆地稱爲「網格」,感謝指向)我想要的是:在大窗口的中心填充整個圖像(4個黃色網格相交,就像你已經做了一些例子)。 我很確定你所做的一切會爲我工作,但讓我檢查。謝謝! –

+0

好吧,我已經仔細閱讀了您的代碼,是的,我沒有看到任何原因導致無法正常工作。我只需要找到一種方法來取代for循環的速度(我正在繪製數千張圖片)。再次感謝! –

+0

讓困惑繼續:)。我想你自從你提到'想要一些'紅色'的網格單元,但我得到了所有不必要的網格。我想我明白你的意思,'Ticklocators'不允許抵消,所以你應該簡單地設置蜱手動,一次爲主要,一次爲次要蜱。如果你想要「所有」網格單元被繪製,那麼這就是要走的路。我會在新的回覆中回答,以保持它的分離。 –