Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Last active December 11, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloaj/4535991 to your computer and use it in GitHub Desktop.
Save juanpabloaj/4535991 to your computer and use it in GitHub Desktop.
numpy para archivos y columnas
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import numpy as np
import matplotlib.pyplot as plt
files = {}
# almacenar nombres de archivo y contenido
dname = 'input/'
for f in os.listdir(dname):
fname = dname + f
files[f] = np.loadtxt(fname)
# recorrer todos los archivos
for fi in files.keys():
for fj in files.keys():
if fi != fj:
namei = fi.replace('.txt','')
namej = fj.replace('.txt','')
outfile = 'sum_' + namei + '_' + namej
# primera columna del archivo
c1 = files[fi][:,0]
# segunda columna del archivo
fi_c2 = files[fi][:,1]
fj_c2 = files[fj][:,1]
# suma de columnas
sum_c2 = fi_c2 + fj_c2
# guardar suma en texto
np.savetxt(outfile + '.txt',
np.column_stack([c1,sum_c2]),
fmt = '%10.2f')
plt.clf()
# curva archivo i
plt.plot(c1,fi_c2)
# curva archivo j
plt.plot(c1,fj_c2)
# curva archivo suma
plt.plot(c1,sum_c2)
# guarda curvas en png
plt.savefig(outfile)
0.0 0.9
1.0 1.2
2.0 1.8
3.0 1.3
4.0 1
5.0 0.9
6.0 0.8
7.0 0.7
8.0 0.7
9.0 0.6
0.0 0.4
1.0 0.4
2.0 0.5
3.0 0.5
4.0 1.2
5.0 1.3
6.0 2.1
7.0 2.4
8.0 2.1
9.0 1.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment