保存和使用python绘制多个数据集 [英] save and plot multiple datasets using python

查看:705
本文介绍了保存和使用python绘制多个数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个跑了问题 - 我有以下格式多个文件:

I ran in one more problem - I have multiple files with the following format:

频率A B

1000 1.2 0.0014

1000 1.2 0.0014

100 1.2 0.00013

100 1.2 0.00013

10 1.2 0.0012

10 1.2 0.0012

中的所有文件都在同一个文件夹中;到现在为止,我能够读取所有文件,做我想做的计算,然后保存一个大文件所有需要的数据(见下文code)

all files are in the same folder; up to now I am able to read all files, do the calculations I want, and then save one large file with all the needed data (see below for code)

在我保存到我想要的数据:

before I save the data I want to:

打破数据到原始文件的数量,与新的数据,并使用相同的名称作为输入名称(N的code是每个文件的行数,我使用的能够以确定从线的总数我有多少个文件)

Break the data into the number of the original files, with the new data, and use the same name as the input name (N in the code is the number of lines per file, I use that to be able to identify how many files I have from the total number of lines)

和,积积于一身的所有不同的数据(假设我有3个文件,如上述,我想有3条线路,都在同一情节图) - 我似乎没有能够做到这一点,因为我所有的尝试给我一个单行

and, to plot all different data in one plot (assume that I have 3 files like the above, I want a plot with with 3 lines, all on the same plot) - I cannot seem to be able to do that since all my tries give me just one single line

*更新 - 我可以读取数据,并把everithing在第一循环中,那么我就可以去了所有的数据 - 现在我要自动保存功能,但code(请参阅下面的一个简化版)只保存一个数据集(可能会覆盖文本文件?)
*
更新2 - 这个现在工作 - 需要添加保存功能在第二循环

* UPDATE - I can read the data, and putting everithing in the first loop, then I can go over all data - now I want to automate the saving function, but the code (see below a simplified version) saves only one data set (maybe overwrites the text files?) * UPDATE 2 - this now works - needed to add the save function in the 2nd loop

import os
import numpy as np

datadirectory = '/media/data'
os.chdir( datadirectory)

listing = os.listdir(datadirectory) 
my_array=np.zeros(shape=(0,3))

for infile in listing: 
   dataset = open(infile).readlines()
   data = np.genfromtxt(dataset, usecols=(0,1,2))
   my_array = np.vstack((my_array, data))
   lta= my_array

一些处理这里 - LTA现在有5列

   Results=np.column_stack((lta[:,0], lta[:,1], lta[:,2], lta[:,3], lta[:,4]))
   for i in listing:
       date = i
       np.savetxt((os.path.join(resultpath, date)) + '.txt', Results, fmt='%s', delimiter='\t') 
   my_array = np.vstack((my_array, Results))

绘图数据 - my_array数组我有所有的数据,每N行(给定数量)我有一个不同的数据集 - Iwant绘制在同一张图上的所有数据,并每隔N行我要改变符号颜色

import matplotlib.pylab as plt

plt.figure(figsize=(10,5))
#graph_axes = plt.subplot(N,2,1)
graph = plt.semilogx(my_array[:,0], my_array[:,2])
plt.ylim(0, 25)
plt.xlim(0.1, 1000)
plt.show()
plt.savefig(os.path.join(resultpath, 'image.png'))

任何帮助/指导AP preciated!

any help / guidance is appreciated!

推荐答案

它会更好,如果你能在 my_array 提供的内容的样本,但对于什么我明白了,这code可以帮助你实现你所需要的。首先,这里的code我写伪造数据。

It'd be better if you could provide a sample of the contents in my_array, but for what I understand, this code could help you achieve what you need. First, here's the code I wrote to fake the data.

import numpy as np
from random import random

# generate fake data
num_files = 5
N = 20
my_array = []
for f in range(num_files):  # simulate multiple files
    for n in range(N):  # simulate multiple samples per file
        # fake data
        my_array.append([10 ** n / N, random() + f, 10 + f + random()])
my_array = np.asarray(my_array)

希望,在这一点my_array的内容是类似于已(Ⅰ仅模拟3列,而不是必须处理该数据后的5,但不应该影响到绘图code是如下:

Hopefully, at this point the contents of my_array are similar to what you have (I only simulated 3 columns instead of the 5 you have after processing the data, but that shouldn't affect the plotting code that follows:

import matplotlib.pyplot as plt

# now plot it
for i in range(0, num_files * N, N):
    # plot column-0 vs column-1 in one subplot
    plt.subplot(2, 1, 1)
    plt.semilogx(my_array[i:i + N, 0], my_array[i:i + N, 1])
    plt.hold(True)
    # plot column-0 vs column-2 in another subplot
    plt.subplot(2, 1, 2)
    plt.semilogx(my_array[i:i + N, 0], my_array[i:i + N, 2])
    plt.hold(True)
plt.show()

诀窍是使用 plt.hold(真),这将preserve无论是之前绘制,并将新的颜色分配给下一个数据绘制。该解决方案也将工作,如果N不是恒定的(但你必须修改通过数据如何循环)。

The trick is to use plt.hold(True), which will preserve whatever was plotted before, and will assign a new color to the next data to be plotted. This solution will also work if N is not constant (but you'll have to modify how you loop through the data).

希望帮助!

这篇关于保存和使用python绘制多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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