2011-03-15 104 views
0

我想在matplotlib中使用numpy和meshgrid繪製一個圖。繪製3D網格圖,訪問網格圖陣列中的點?

我想在數組中心填充'ones'和其他零的區域。我嘗試設置數組,但for循環似乎沒有輸入(打印語句不打印在for循環中)。任何指針?

import numpy as np 
import pylab as py 
from scipy import * 
from numpy.fft import fft 
import mpl_toolkits.mplot3d.axes3d as p3 

def fft2dplot(
    type = 'rect', aperature = 16, method = 'transfer', 
    wavelength = 1, distance = 1000000): 
    dict = { 
     'rect' : 'Rectangle', 'circ' : 'Circle', 
     'transfer' : 'Tranfer Function', 'integral' : 'Integral'} 
    #Scale is not correct 
    scale = aperature/(distance*wavelength) #in mm 
    #Range for aperature, 
    x = y = arange(-aperature*8,aperature*8, 1) 
    X,Y = np.meshgrid(x,y) 
    print len(X) 
    X = Y = Z = X*0 

    #These following statements never enter (type == rect passes, but for loop don't) 
    if type == 'rect': 
     for u in x[-aperature/2:aperature/2]: 
       for w in y[-aperature/2:aperature/2]: 
        Z[u,w] = 1 
        print "I'm here" 

    fig = py.figure() 
    ax = p3.Axes3D(fig) 
    #ax.contour3D(X,Y,Z) 
    ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1) 
    ax.set_xlabel('X') 
    ax.set_ylabel('Y') 
    ax.set_zlabel('Z') 
    py.show() 
+0

你想用這條線完成什麼:'X = Y = Z = X * 0'? – Paul 2011-03-15 06:36:57

回答

0

這是我認爲你想要的代碼。你想避免循環,你不能以這種方式索引(見下面的評論)。

import numpy as np 
import pylab as py 
from scipy import * # it is better practice to do: from scipy import module1, module2, etc... 
from numpy.fft import fft 
import mpl_toolkits.mplot3d.axes3d as p3 

def fft2dplot(
    type = 'rect', aperature = 16, method = 'transfer', 
    wavelength = 1, distance = 1000000): 
    # here you are overwriting the built-in 'dict' suggest using a different variable name 
    dict = { 
     'rect' : 'Rectangle', 'circ' : 'Circle', 
     'transfer' : 'Tranfer Function', 'integral' : 'Integral'} 
    #Scale is not correct 
    scale = aperature/(distance*wavelength) #in mm 
    #Range for aperature, 
    x = y = arange(-aperature*8,aperature*8, 1) 
    X,Y = np.meshgrid(x,y) 
    print len(X) 
    # you were losing references to the meshgrid arrays X and Y here. 
    # I've provided an alternate method of getting an array of zeros 
    # Z = np.zeros(X.shape) works as well 
    #X = Y = Z = X*0 
    Z = X.copy() 
    Z[:,:] = 0 


    # Indexing does not work like this. Indices are unsigned integers 0 and above 
    # or a boolean array the same shape as the indexed array. 
    #These following statements never enter (type == rect passes, but for loop don't) 
## if type == 'rect': 
##  for u in x[-aperature/2:aperature/2]: 
##    for w in y[-aperature/2:aperature/2]: 
##     Z[u,w] = 1 
##     print "I'm here" 

    # what you need is to define your indices based on your conditions. 
    # Here we create a boolean array of indices that indicate where we meet your 
    # conditions. 
    # Then we use that array to assign those elements of the Z array to the value 1 
    idxs = (X>-aperature/2)&(X<aperature/2)&(Y>-aperature/2)&(Y<aperature/2) 
    Z[idxs] = 1 

    fig = py.figure() 
    ax = p3.Axes3D(fig) 
    #ax.contour3D(X,Y,Z) 
    ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1) 
    ax.set_xlabel('X') 
    ax.set_ylabel('Y') 
    ax.set_zlabel('Z') 
    py.show() 
+0

太棒了,非常感謝。這工作很好。當我試圖製作更大的圖像時,遇到了一些內存問題以及其他一些問題,所以我切換到使用PIL繪製圖像並將其轉換爲數組。我被困在別的東西上,但我會做一個新的話題。謝謝您的幫助!!!! – MercuryRising 2011-03-23 04:18:00