【问题标题】:Build a 3D array for 754 spectra为 754 个光谱构建 3D 阵列
【发布时间】:2019-01-14 21:36:50
【问题描述】:

我的问题是我有两个数组,一个包含我的数据(光谱),一个刚刚从 1 填充到 128(一维数组)。我想知道如何从这些构建一个 3D 数组。我尝试使用 numpy.vstack 但似乎我必须在不同数组的参数中精确。我敢肯定这并不复杂,但我现在有点卡住了。这个想法是建立一个这样的数组(然后打印 3D 曲线):

目标是在我向下移动波数时获得正确的光谱。例如,在 2D 中:

使用以下代码:

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

num_lon = 128
num_lat = 128

tmpfile = "180523_WT_striatum_#1.dat"

fileobj = open(tmpfile, mode='rb')
fileobj.seek(1020)
binvalues = array.array('f')
binvalues.read(fileobj, num_lon * num_lat)

data = np.array(binvalues)
data = np.reshape(data, (num_lat, num_lon))


L = [i for i in range(len(data))]


fileobj.close()
plt.plot(L,data[0])
plt.plot(L,data[1])
plt.show()

你们有什么线索吗?非常感谢。

【问题讨论】:

    标签: python arrays python-2.7 numpy matplotlib


    【解决方案1】:
    import numpy as np
    import array
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from mpl_toolkits.mplot3d import Axes3D
    
    num_lon = 128
    num_lat = 754
    
    tmpfile = "180523_WT_striatum_#1.dat"
    
    fileobj = open(tmpfile, mode='rb')
    fileobj.seek(1020)
    binvalues = array.array('f') # 'f' stands for float 4 bytes
                             # It would be 'd' for float 8 bytes size
    binvalues.read(fileobj, 128 * 128 )
    
    data = np.array(binvalues)
    
    data = np.reshape(data, (128,128))
    
    
    L = np.array([[i for i in range(128)]for j in range(754)])
    
    fileobj.close()
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    for i in range(len(data)):
        ax.plot(L[i], data[i], i)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      相关资源
      最近更新 更多