如何在python中绘制数组? [英] How to plot an array in python?

查看:888
本文介绍了如何在python中绘制数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我点击此链接

I follow this links How to append many numpy files into one numpy file in python to 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:

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]]]]

我需要绘制包含最终文件OutFileTraces.npy的图表,该文件包含大数组.为此,我使用以下代码:

I need to plot the plot the final file OutFileTraces.npy which contains the big array. For that I use this code:

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

它给了我这个错误:

提高ValueError("x和y不能大于2-D")ValueError:x 和y不能大于2-D

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的点.因此,据我了解,为了绘制最终的大数组,它必须看起来像这样(区别在于[] ):

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 []):

[[-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.

推荐答案

如果将2D数组提供给 plot 函数,它将假定列为行:

if you give a 2D array to the plot function of matplotlib it will assume the columns to be lines:

如果x和/或y是二维的,则相应的列将 绘制.

If x and/or y is 2-dimensional, then the corresponding columns will be plotted.

在您的情况下,您的形状不被接受(100、1、1、8000).因此,您可以使用numpy 挤压来解决问题快速:

In your case your shape is not accepted (100, 1, 1, 8000). As so you can using numpy squeeze to solve the problem quickly:

np.squeez文档:从数组形状中删除一维条目.

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

但是请注意,对于matplotlib,100套80 000点是很多数据.我建议您寻找替代方案.该代码示例的结果(在 Jupyter 中运行)是:

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:

这篇关于如何在python中绘制数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆