我的问题是我有两个数组,一个数组包含我的数据(光谱),一个数组刚刚从1填充到128(一维数组)。我想知道如何才能从这些构建3D阵列。我尝试使用numpy.vstack,但似乎我必须在参数上精确到不同的数组。我确信这没什么复杂,但我现在有点卡住了。这个想法是建立一个像这样的数组(然后打印3D曲线):
使用以下代码:
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()
你们有什么领先优势吗?非常感谢。
答案 0 :(得分: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()