简体   繁体   English

如何在Python 2中从20个CSV文件创建叠加图?

[英]How to create overlay plots from 20 CSV files in Python 2?

I generate 20 CSV files per each long calculation I do in python 2. From all data sets in each file, there are 2 set that are important here. 我在python 2中为每个长计算生成20个CSV文件。从每个文件中的所有数据集中,有2个集合在这里很重要。 First set, as row[0] on x-axis, and the third set as row[2] on y-axis. 首先设置为x轴上的行[0],第三个设置为y轴上的行[2]。 First set, (row[0]) is always the same, so one entry should suffice, but the third set (row[2]) changes by the file and needs to be plotted against (row[0]) as I need to plot all the third sets from all files superimposed. 第一个集合,(row [0])总是相同的,所以一个条目应该足够了,但是第三个集合(row [2])由文件改变,需要根据我的需要绘制(row [0])从所有叠加的文件中绘制所有第三组。

I am not a programmer but I can plot them individually as per: 我不是程序员,但我可以按照以下方式单独绘制它们:

import matplotlib.pyplot as plt
import csv

x = []
y = []

with open('energy.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        x.append(row[0])
        y.append(row[2])

plt.plot(x,y, label='Energies')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Energy Graph\n per particle')
plt.legend()
plt.savefig('energy.png')
plt.savefig('energy.pdf')

plt.show()

But to superimpose all data sets I wanted to try "for file_name in files:" to append the data set to the filename as the variable and the plot them all at the end in one graph: 但是要叠加所有数据集,我想尝试“在文件中使用file_name:”,将数据集作为变量附加到文件名中,并在一张图中将它们全部绘制在末尾:

for dirpath, dirnames, files in os.walk('.'):
    for file_name in files:
            if file_name.endswith('.csv'):
                    print(file_name)
                    with open(file_name,'r') as csvfile:
                            plots = csv.reader(csvfile, delimiter=',')
                            for row in plots:
                                    x.append(row[0])
                                    file_name.append(row[2])
            plt.plot(x,file_name, label='Loaded from file!')
            plt.xlabel('x')
            plt.ylabel('y')
            plt.title('Energy')
            plt.legend()
            plt.savefig('1.png')
            plt.savefig('1.pdf')

            plt.show()

Then I get this error: 然后我收到这个错误:

file_name.append(row[1]) AttributeError: 'str' object has no attribute 'append' file_name.append(row [1])AttributeError:'str'对象没有属性'append'

Any help would be appreciated 任何帮助,将不胜感激

To create overlay plots, call plt.plot() iteratively. 要创建叠加图,请迭代调用plt.plot()


for dirpath, dirnames, files in os.walk('.'):
    for file_name in files:
        if file_name.endswith('.csv'):
            print(file_name)
            x = []
            y = []
            with open(file_name,'r') as csvfile:
                plots = csv.reader(csvfile, delimiter=',')
                for row in plots:
                    x.append(row[0])
                    y.append(row[2])
            plt.plot(x,y, label=file_name)

plt.xlabel('x')
plt.ylabel('y')
plt.title('Energy')
plt.legend()
plt.show()

Example: 例:
a.csv

1,3,4,3,5
3,2,5,2,5

b.csv

1,3,2,3,5
3,2,6,2,5

then you'll get this plot: 然后你会得到这个情节:

情节例子

But if the CSV files consist of numeric data, I recommend using NumPy because it is fast. 但是,如果CSV文件包含数字数据,我建议使用NumPy,因为它速度很快。 :

import os
import numpy as np
import matplotlib.pyplot as plt

for dirpath, dirnames, files in os.walk('.'):
    for file_name in files:
        if file_name.endswith('.csv'):
            print(file_name)
            data = np.loadtxt(file_name, delimiter=',').transpose()
            plt.plot(data[0], data[2], label=file_name)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Energy')
plt.legend()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM