2017-06-30 50 views
0

我想根据条件在数据库中存储x,y,z点。这些条件来自NACL structure。例如,当x是偶数,y和z是奇数时,有一个NA原子 - 该坐标将被存储为NA原子。所以当我搜索所有的NA原子时,我得到一个坐标列表。另一个线程询问similar question答案是基于使用np.where,但我不知道如何在此实现。Numpy Array:根据条件存储坐标

我尝试下面的代码:

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 

#dimension/shape of grid and array 
n = int (input("Enter CUBE dimension: ")) 

def cube(ax, n): 
    #Creating Arrays 
    parameter = np.arange(0,n,1) 
    xx, yy, zz = np.meshgrid(parameter, parameter, parameter) 
    valuesrange = np.zeros((n,n,n)) 
    #list of arrays 
    a = [(xx,yy,zz)] 
    # my attempt at the problem 
    # if xx % 2 == 0 and yy % 2 != 0 and zz % 2 !=0: 
    #  NA = [(xx,yy,zz)] 
    # elif xx % 2 != 0 and yy % 2 != 0 and zz % 2 ==0: 
    #  NA = [(xx,yy,zz)] 
    # Etc... 

    print (a) 
    scatterplot = ax.scatter(xx, yy, zz, c = valuesrange, cmap=plt.cm.spectral_r, vmin=0, vmax=1,edgecolor = 'black', alpha = .7)  
    return scatterplot 


fig = plt.figure() 
ax = fig.add_subplot(111, projection = '3d') 
ax.set_xlabel('X') 
ax.set_ylabel('Y') 
ax.set_zlabel('Z') 

scatterplot1 = cube(ax,n) 
plt.colorbar(scatterplot1) 
plt.show() 

任何帮助,不胜感激!

+1

我很困惑。你打算为输入的每个点调用这个函数吗?请记住,函数内部的变量对函数是本地的,并且不会在其外部使用。另外,我认为你的缩进已经关闭了,而且它的可读性也很糟糕。 – mauve

+0

@mauve坐标将被分类和存储。现在,我认为他们可以保持本地化,因为该功能只需要运行一次。我修好了缩进。谢谢。 – Astupidhippo

+0

@P。 Camilleri重新复制并粘贴代码。希望它是固定的。 – Astupidhippo

回答

0

我想加入这个以您的代码将有助于澄清:

def cube(ax, n): 
# Creating Arrays 
    parameter = np.arange(0, n, 1) 
    xx, yy, zz = np.meshgrid(parameter, parameter, parameter) 
    for coordx in range(n): 
     for coordy in range(n): 
      for coordz in range(n): 
       print('xx') 
       print(xx[coordx, coordy, coordz]) 
       print('yy') 
       print(yy[coordx, coordy, coordz]) 
       print('zz') 
       print(zz[coordx, coordy, coordz]) 
    return 

我认为你缺少的是xx是不是一个价值,它的价值矩阵。你将需要比较矩阵中每个位置的值。这里的a link about iterating over NumPy Arrays

+0

将阅读本文。谢谢! – Astupidhippo