- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我点击此链接 How to append many numpy files into one numpy file in python将我所有的 numpy 文件放在一个文件中。现在,我需要绘制包含许多数组的文件,每个数组包含一些 float :这是我将数组附加到一个大数组中的最终代码:
import matplotlib.pyplot as plt
import numpy as np
import glob
import os, sys
fpath ="/home/user/Desktop/OutFileTraces.npy"
npyfilespath="/home/user/Desktop/test"
os.chdir(npyfilespath)
npfiles= glob.glob("*.npy")
npfiles.sort()
all_arrays = []
with open(fpath,'ab') as f_handle:
for npfile in npfiles:
#Find the path of the file and Load file
all_arrays.append(np.load(os.path.join(npyfilespath, npfile)))
np.save(f_handle, all_arrays)
data = np.load(fpath)
print data
这段代码给我这样的结果:
[[[[-0.00824758 -0.0081808 -0.00811402 ..., -0.0077236 -0.00765425
-0.00762086]]]
[[[-0.00141527 -0.00160791 -0.00176716 ..., -0.00821419 -0.00822446
-0.0082296 ]]]
[[[ 0.01028957 0.01005326 0.0098298 ..., -0.01043341 -0.01050019
-0.01059523]]]
...,
[[[ 0.00614908 0.00581004 0.00549154 ..., -0.00814741 -0.00813457
-0.00809347]]]
[[[-0.00291786 -0.00309509 -0.00329287 ..., -0.00809861 -0.00797789
-0.00784175]]]
[[[-0.00379887 -0.00410453 -0.00438963 ..., -0.03497837 -0.0353842
-0.03575151]]]]
我需要绘制包含大数组的最终文件 OutFileTraces.npy。为此,我使用了这段代码:
import matplotlib.pyplot as plt
import numpy as np
dataArray1= np.load(r'/home/user/Desktop/OutFileTraces.npy')
print(dataArray1)
plt.plot(dataArray1.T )
plt.show()
它给我这个错误:
raise ValueError("x and y can be no greater than 2-D") ValueError: x and y can be no greater than 2-D
所有这些值代表 y_axe,但是我的 x 轴代表从 1 到 8000 的点。所以,据我所知,为了绘制我最终的大数组,它必须看起来像这样(区别在于 []
):
[[-0.00824758 -0.0081808 -0.00811402 ..., -0.0077236 -0.00765425
-0.00762086]
[-0.00141527 -0.00160791 -0.00176716 ..., -0.00821419 -0.00822446
-0.0082296 ]
[ 0.01028957 0.01005326 0.0098298 ..., -0.01043341 -0.01050019
-0.01059523]
...,
[0.00614908 0.00581004 0.00549154 ..., -0.00814741 -0.00813457
-0.00809347]
[-0.00291786 -0.00309509 -0.00329287 ..., -0.00809861 -0.00797789
-0.00784175]
[-0.00379887 -0.00410453 -0.00438963 ..., -0.03497837 -0.0353842
-0.03575151]]
我可以很容易地绘制这个文件。
所以我真的无法理解这个问题。
如果你能帮助我,我将不胜感激。
最佳答案
如果你给 plot 一个二维数组matplotlib 的功能它将假定列为线:
If x and/or y is 2-dimensional, then the corresponding columns will be plotted.
在您的情况下,您的形状不被接受(100、1、1、8000)。这样你就可以使用 numpy squeeze快速解决问题:
np.squeez doc: Remove single-dimensional entries from the shape of an array.
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randint(3, 7, (10, 1, 1, 80))
newdata = np.squeeze(data) # Shape is now: (10, 80)
plt.plot(newdata) # plotting by columns
plt.show()
但请注意,100 组 80 000 个点对于 matplotlib 来说是很多数据。我建议您寻找替代方案。代码示例(在 Jupyter 中运行)的结果是:
关于python - 如何在 python 中绘制数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42227997/
我是一名优秀的程序员,十分优秀!