-
Notifications
You must be signed in to change notification settings - Fork 2
/
plotdata.py
executable file
·81 lines (69 loc) · 2.12 KB
/
plotdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
""" Plot data from csv files.
This script generates plots from data retrieved
from csv or similar text based files.
Author: Uthpala Herath
Usage:
Enter plotdata.py -h for help.
"""
import argparse
import os
import sys
from argparse import RawTextHelpFormatter
import matplotlib.pyplot as plt
import numpy as np
def dataplotter(args):
infile = args.infile
xcolumn = args.xcolumn
ycolumn = args.ycolumn
headerlines = args.headerlines
xlabel = args.xlabel
ylabel = args.ylabel
title = args.title
savefig = args.savefig
file = open(infile, "r")
if headerlines > 0:
for i in range(headerlines): # skipping header lines
file.readline()
data = file.readlines()
file.close()
xdata = []
ydata = []
for i in range(len(data)):
if xcolumn is not None:
xdata.append(float(data[i].split()[xcolumn - 1]))
ydata.append(float(data[i].split()[ycolumn - 1]))
if xcolumn is not None:
plt.plot(xdata, ydata)
else:
plt.plot(ydata)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.savefig(savefig)
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=RawTextHelpFormatter
)
parser.add_argument("-infile", type=str, help="Input data file.")
parser.add_argument(
"-xcolumn",
type=int,
help="Column number for x value. Leave empty to plot only y values.",
default=None,
)
parser.add_argument(
"-ycolumn", type=int, help="Column number for y value.", default=2
)
parser.add_argument(
"-headerlines", type=int, help="Number of header lines.", default=0
)
parser.add_argument("-title", type=str, help="Plot title.", default="X vs Y plot")
parser.add_argument("-xlabel", type=str, help="xlabel.", default="X")
parser.add_argument("-ylabel", type=str, help="ylabel.", default="Y")
parser.add_argument(
"-savefig", type=str, help="Save figure name.", default="plot.png"
)
args = parser.parse_args()
dataplotter(args)