Plot 1D Density
From Computational Biophysics and Materials Science Group
I think you guys can use the code in this way:
python plot1d.py -i ~/data.txt -o result.png --ignore=',' --ignore='(' --ignore=')' \
--format='* * x'
To dynamically read your data file. :-)
import sys, getopt #parameters
import numpy as np
from pylab import *
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
def plot(input_file, ignore, line_format, output_png, silent):
xs = []
index_x = line_format.split().index('x')
with open(input_file) as f:
for line in f:
for _ignore in ignore:
line = line.replace(_ignore, ' ')
_line = line.split()
xs.append(_line[index_x])
#only for generate two array
x = randn(len(xs))
# Translate into x, y from xs, ys
for i in range(0,1000000):
x[i] = xs[i]
# hist and plot
plt.hist(x, bins=x.max() - x.min() + 1)
if output_png is None:
pass
else:
savefig(output_png, dpi=400)
if silent:
pass
else:
plt.show()
return plt
def main(argv):
# By Xinhong
help_str = '''==============================================
plot1d.py -i <inputfile> [-s] [-o <output.png>] [--ignore] [--format]
=============================================='''
input_file = None
ignore = []
line_format ='* * x'
output_png = None
silent = False
try:
opts, args = getopt.getopt(argv,"hi:o:s",["ifile=", "ignore=", "format="])
except getopt.GetoptError:
print(help_str)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(help_str)
sys.exit()
elif opt == '-o':
output_png = arg
elif opt in ("-i", "--ifile"):
input_file = arg
elif opt == '--ignore':
ignore.append(arg)
elif opt == '--format':
line_format = arg
elif opt == '-s':
silent = True
if input_file is None:
print('Please specify input file.')
sys.exit(2)
figure = plot(input_file, ignore, line_format, output_png, silent)
if __name__ == "__main__":
main(sys.argv[1:])
if you want to process a batch of files and only output the image without open it, try this:
FILES=/Users/Hung/Desktop/result/*
for f in $FILES;
do
python3.3 /Users/Hung/Documents/git/random_walk/draw/plot1D.py -s -i $f -o ${f}_1d.png --format="* * x";
done