如何在python中绘制数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42227997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot an array in python?
提问by nass9801
I follow this links How to append many numpy files into one numpy file in pythonto put all my numpy files in one file. Now, I need to plot my file which contains many arrays, each array contain some float number: this is my final code to append arrays in one big array:
我按照这个链接如何将许多 numpy 文件附加到 python 中的一个 numpy 文件中,以将我所有的 numpy 文件放在一个文件中。现在,我需要绘制包含许多数组的文件,每个数组都包含一些浮点数:这是我将数组附加到一个大数组中的最终代码:
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
This code gives me results like this:
这段代码给了我这样的结果:
[[[[-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]]]]
I need to plot the plot the final file OutFileTraces.npy which contains the big array. For that I use this code:
我需要绘制包含大数组的最终文件 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()
It gives me this error:
它给了我这个错误:
raise ValueError("x and y can be no greater than 2-D") ValueError: x and y can be no greater than 2-D
raise ValueError("x 和 y 不能大于 2-D") ValueError: x 和 y 不能大于 2-D
All that values represents the y_axe, however my x-axe represents points from 1 to 8000. So, as I understand,in order to plot my final big array, it must looks like this (The difference is on []
):
所有这些值都代表 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]]
I can easily plot this file.
我可以轻松地绘制此文件。
So I can't really understand the problem.
所以我真的无法理解这个问题。
I would be very grateful if you could help me.
如果你能帮助我,我将不胜感激。
回答by armatita
if you give a 2D array to the plotfunction of matplotlib it will assume the columns to be lines:
如果你给matplotlib的plot函数提供一个二维数组,它会假设列是线:
If x and/or y is 2-dimensional, then the corresponding columns will be plotted.
如果 x 和/或 y 是二维的,则将绘制相应的列。
In your case your shape is not accepted (100, 1, 1, 8000). As so you can using numpy squeezeto solve the problem quickly:
在您的情况下,您的形状不被接受(100、1、1、8000)。因此,您可以使用 numpy挤压来快速解决问题:
np.squeez doc:Remove single-dimensional entries from the shape of an array.
np.squeez doc:从数组的形状中删除一维条目。
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()
But notice that 100 sets of 80 000 points is a lot of data for matplotlib. I would recommend that you look for an alternative. The result of the code example (run in Jupyter) is:
但是请注意,对于 matplotlib 来说,100 组 80 000 点是很多数据。我建议您寻找替代方案。代码示例(在Jupyter 中运行)的结果是: