【问题标题】:Numpy Array: Storing coordinates based on conditionsNumpy Array:根据条件存储坐标
【发布时间】:2017-06-30 13:52:25
【问题描述】:

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

我的尝试是在以下代码中:

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()

非常感谢任何帮助!

【问题讨论】:

  • 我很困惑。您是否打算为输入的每个点调用此函数?请记住,函数内部的变量对于函数来说是本地的,并且在函数之外不可用。另外,我认为您的缩进已经关闭,并且在可读性方面让我感到震惊。
  • @mauve 坐标将被分类和存储。目前,我认为它们可以保持本地化,因为该功能只需要运行一次。我修复了缩进。谢谢。
  • @P. Camilleri 重新复制并粘贴代码。希望它是固定的。

标签: python arrays database numpy matplotlib


【解决方案1】:

我认为将此添加到您的代码中将有助于澄清:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多