python - 使用 x 轴偏移在 python (5GB) 中绘制非常大的文件

标签 python numpy matplotlib figure

我正在尝试使用 python 和 matplotlib 绘制一个非常大的文件(~5 GB)。我能够将整个文件加载到内存中(机器中的总可用空间为 16 GB),但是当我使用简单的 imshow 绘制它时,我遇到了段错误。这最有可能是我设置为 15000 的 ulimit,但我不能设置得更高。我得出的结论是我需要分批绘制我的数组,因此编写了一个简单的代码来做到这一点。我的主要问题是,当我绘制一批大数组时,x 坐标总是从 0 开始,我无法叠加图像来创建最终的大数组。如果您有任何建议,请告诉我。此外,由于管理权限,我无法在这台机器上安装像“Image”这样的新软件包。下面是读取数组前 12 行并制作 3 个绘图的代码示例。

import os
import sys
import scipy
import numpy as np
import pylab as pl
import matplotlib as mpl
import matplotlib.cm as cm
from optparse import OptionParser
from scipy import fftpack
from scipy.fftpack import *
from cmath import *
from pylab import *
import pp
import fileinput
import matplotlib.pylab as plt
import pickle

def readalllines(file1,rows,freqs):
    file = open(file1,'r')
    sizer = int(rows*freqs)
    i = 0
    q = np.zeros(sizer,'float')
    for i in range(rows*freqs):
        s =file.readline()
        s = s.split()
        #print s[4],q[i]
        q[i] = float(s[4])
        if i%262144 == 0:
            print '\r ',int(i*100.0/(337*262144)),'  percent complete',
        i += 1
    file.close()
    return q

parser = OptionParser()
parser.add_option('-f',dest="filename",help="Read dynamic spectrum from FILE",metavar="FILE")
parser.add_option('-t',dest="dtime",help="The time integration used in seconds, default 10",default=10)
parser.add_option('-n',dest="dfreq",help="The bandwidth of each frequency channel in Hz",default=11.92092896)
parser.add_option('-w',dest="reduce",help="The chuncker divider in frequency channels, integer default 16",default=16)
(opts,args) = parser.parse_args()
rows=12
freqs = 262144

file1 = opts.filename

s = readalllines(file1,rows,freqs)
s = np.reshape(s,(rows,freqs))
s = s.T
print s.shape
#raw_input()

#s_shift = scipy.fftpack.fftshift(s)


#fig = plt.figure()

#fig.patch.set_alpha(0.0)
#axes = plt.axes()
#axes.patch.set_alpha(0.0)
###plt.ylim(0,8)

plt.ion()

i = 0
for o in range(0,rows,4):

    fig = plt.figure()
    #plt.clf()

    plt.imshow(s[:,o:o+4],interpolation='nearest',aspect='auto', cmap=cm.gray_r, origin='lower')
    if o == 0:
        axis([0,rows,0,freqs])
    fdf, fdff = xticks()
    print fdf
    xticks(fdf+o)
    print xticks()
    #axis([o,o+4,0,freqs])
    plt.draw()

    #w, h = fig.canvas.get_width_height()
    #buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)
    #buf.shape = (w,h,4)

    #buf = np.rol(buf, 3, axis=2)
    #w,h,_ = buf.shape
    #img = Image.fromstring("RGBA", (w,h),buf.tostring())

    #if prev:
    #    prev.paste(img)
    #    del prev
    #prev = img
    i += 1
pl.colorbar()
pl.show()

最佳答案

如果您在图形链中的某处绘制超过 ~2k 像素的任何阵列,以某种方式对图像进行向下采样,以将其显示在您的显示器上。我建议以可控的方式进行下采样,比如

data = convert_raw_data_to_fft(args) # make sure data is row major
def ds_decimate(row,step = 100):
    return row[::step]
def ds_sum(row,step):
    return np.sum(row[:step*(len(row)//step)].reshape(-1,step),1)
# as per suggestion from tom10 in comments
def ds_max(row,step): 
    return np.max(row[:step*(len(row)//step)].reshape(-1,step),1)
data_plotable = [ds_sum(d) for d in data] # plug in which ever function you want

interpolation .

关于python - 使用 x 轴偏移在 python (5GB) 中绘制非常大的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13183460/

相关文章:

python - 从分类数据类型列中提取平均值

python - 使用 matplotlib Polycollection 绘制 csv 文件中的数据

python - 当时间数据已作为索引确定时,如何绘制时间相关数据?

python - 如何显示一组图像随时间变化的像素变化? (随着时间的推移成像物体的亮度变化)

python - 尝试将带有文件的数据表单转发到另一个网络服务

python - 使用 websocket 实时流 stdout 和 stdin

python - 为什么这段Theano代码运行成功没有任何错误?

python - 具有相同行数的两个 CSV 文件按列串联

python - 根据纬度和经度过滤数据 - Numpy

python - 如何向 3D 表面添加文本