功能覆盖数据

时间:2013-09-30 18:21:28

标签: python matplotlib

我正在尝试使用数据读取许多不同的文本文件,然后绘制数据图形。我的函数在读取数据时起作用,我可以将其绘制成一个小问题:当我运行程序时,它似乎用当前函数的数据覆盖前一个函数调用中的数据,从而创建一个包含所有数据的图形相同的数据。我已经验证了数据实际上是不同的,因为在我调用每个函数并在图1中绘制所有函数之后调用matplotlib来绘制数据时可以看到。但是图2中最后的图表产生了4行相同。任何人都可以指出为什么会发生这种情况以及如何缩短我的代码以便我只在最后调用matplotlib来绘制所有数据?可以在此处找到数据文件的zip文件,以便程序运行:

#import numpy as np
import matplotlib.pyplot as plt

flux = []

SEM = []

depth = []

c = 0

def data_reader(file_name, first_surface):

    del flux[:]

    del SEM[:]

    del depth[:]

    with open (file_name) as inf:

        lines = inf.readlines()

        for (i, line,) in enumerate(lines):

            if ' surface  %d' %first_surface in line:

                data = (lines[i+1].strip())

                fields = data.split()

                numbers = list(map(float,fields))  

                flux.append(numbers[0])

                SEM.append(numbers[1])

                first_surface += 1

                depth.append(first_surface - 101)                



    return (flux, SEM, first_surface)

data1 = data_reader('concrete_40billion.txt', 101)
flux_1 = data1[0]
SEM_1 = data1[1]

plt.figure(1)
plt.rcParams['legend.loc'] = 'best'
plt.autoscale(enable=True, axis='y', tight=None)
plt.autoscale(enable=True, axis='x', tight=None)
plt.plot(depth, flux_1, 'k')
plt.loglog()
plt.hold(True)
print('plots created')
plt.show()

data2 = data_reader('du_1m_20billion.txt', 101)
flux_2 = data2[0]
SEM_2 = data2[1]

plt.figure(1)
plt.rcParams['legend.loc'] = 'best'
plt.autoscale(enable=True, axis='y', tight=None)
plt.autoscale(enable=True, axis='x', tight=None)
plt.plot(depth, flux_2, 'g')
plt.loglog()
plt.hold(True)
print('plots created')
plt.show()

data3 = data_reader('du_2m_20billion.txt', 101)
flux_3 = data3[0]
SEM_3 = data3[1]

plt.figure(1)
plt.rcParams['legend.loc'] = 'best'
plt.autoscale(enable=True, axis='y', tight=None)
plt.autoscale(enable=True, axis='x', tight=None)
plt.plot(depth, flux_3, 'b')
plt.loglog()
plt.hold(True)
print('plots created')
plt.show()

data4 = data_reader('du_3cm_20billion.txt', 101)
flux_4 = data4[0]
SEM_4 = data4[1]

plt.figure(1)
plt.rcParams['legend.loc'] = 'best'
plt.autoscale(enable=True, axis='y', tight=None)
plt.autoscale(enable=True, axis='x', tight=None)
plt.plot(depth, flux_4, 'r')
plt.loglog()
plt.hold(True)
print('plots created')
plt.show()

#difference = [a - b for a,b in zip(flux_1, flux_2)]
#print difference

plt.rcParams['legend.loc'] = 'best'
plt.figure(2)
#plt.xlim(0, 0.04)
#plt.ylim(0, 7000)
plt.autoscale(enable=True, axis='y', tight=None)
plt.autoscale(enable=True, axis='x', tight=None)
plt.plot(depth, flux_1, 'k',label='flux 1')
plt.plot(depth, flux_2, 'g', label='flux 2')
plt.plot(depth, flux_3, 'b', label='flux 3')
plt.plot(depth, flux_4, 'r', label='flux 4')
plt.xlabel('Depth (cm)')
plt.ylabel('Flux (particles/cm^2*s)')
plt.loglog()
plt.legend()
print('plots created')
plt.show()

1 个答案:

答案 0 :(得分:0)

由于您的功能正在关闭fluxSEMdepth,因此您正在使用del s来查找返回的值,因此您遇到了问题。

flux是一个全局可变列表,每个都通过您的函数清除flux的内容,重新填充它,然后返回对flux的引用然后存储为flux_n。这是{em>所有你flux_n点在同一个底层list对象。这同样适用于SEM_n

当您使用matplotlib进行绘图时,数据会被复制,因此您的现有图表一旦生成就不会受到影响。

尝试:

def data_reader(file_name, first_surface):
    flux = []
    SEM = []
    depth = []

    with open (file_name) as inf:
        lines = inf.readlines()
        for (i, line,) in enumerate(lines):
            if ' surface  %d' %first_surface in line:
                data = (lines[i+1].strip())
                fields = data.split()
                numbers = list(map(float,fields))  
                flux.append(numbers[0])
                SEM.append(numbers[1])
                first_surface += 1
                depth.append(first_surface - 101)                

    return (flux, SEM, first_surface)
相关问题