0

我在一个文件夹中有很多二进制和 ascii 文件。我正在使用 glob 模块阅读它们。处理二进制数据,以便我可以绘制它们。最后,我试图在一个子图中绘制简化的二进制数据,在另一个子图中绘制普通的 ascii 文件。我面临的问题是它可以为相应的二进制文件生成图。但是对于 ascii 文件,它只是简单地覆盖以前的文件并始终生成相同的图。这是示例代码的简化版本-

import glob 
import numpy as np
from struct import unpack
import matplotlib.pyplot as plt

chi = sorted(glob.glob('C:/Users/Desktop/bin/*.chi'))

for index,fh in enumerate(chi):
    data = np.genfromtxt(fh, dtype = float)
    x = [row[0] for row in data]
    y = [row[1] for row in data]

binary = sorted(glob.glob('C:/Users/Desktop/bin/*.bin'))

for count,FILE in enumerate(binary):
    F = open(FILE,'rb')
    B = unpack('f'*1023183, F.read(4*1023183))
    A = np.array(B).reshape(1043, 981)
    F.close()

    #a = something column 1   # some further processing
    #b =  something column 2  # and generates 1D data

    fig = plt.figure(figsize=(11, 8.0))

    ax1 =fig.add_subplot(211,axisbg='w')
    ax1.plot(a,b)

    ax2 =fig.add_subplot(212, axisbg ='w')
    ax2.plot(x,y)

    plt.show()

有人可以解释为什么文件在绘制期间仅针对另一组正确绘制的一组数据相互替换吗?

4

1 回答 1

1

在您的示例中,循环的结构不正确,您必须在 ascii 文件的循环中使用 plot 命令,否则只绘制最后一个。这应该有效:

试试这样:

import glob 
import numpy as np
from struct import unpack
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(11, 8.0))

chi = sorted(glob.glob('C:/Users/Desktop/bin/*.chi'))

for index,fh in enumerate(chi):
    data = np.genfromtxt(fh, dtype = float)
    x = [row[0] for row in data]
    y = [row[1] for row in data]

    ax1 =fig.add_subplot(211, axisbg ='w')
    ax1.plot(x,y)


binary = sorted(glob.glob('C:/Users/Desktop/bin/*.bin'))

for count,FILE in enumerate(binary):
    F = open(FILE,'rb')
    B = unpack('f'*1023183, F.read(4*1023183))
    A = np.array(B).reshape(1043, 981)
    F.close()

    #a = something column 1   # some further processing
    #b =  something column 2  # and generates 1D data


    ax2 =fig.add_subplot(212,axisbg='w')
    ax2.plot(a,b)

plt.show()
于 2013-06-29T05:26:17.917 回答