-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnuout.py
executable file
·117 lines (91 loc) · 3.06 KB
/
gnuout.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python2
import numpy as np
import argparse
import sys
import matplotlib.pyplot as plt
color_sequence = 'rbgmcy'
parser = argparse.ArgumentParser(description="""
Plot data from the input.
""", formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-x', type=str, default='1,2',
help='Columns to use')
parser.add_argument('-i', type=str, default=None,
help='Input file')
parser.add_argument('-t', '--title', type=str, default=None,
help='Plot title')
parser.add_argument('-d', '--delimiter', type=str, default=None,
help='Input file')
parser.add_argument('-s', '--style', type=str, default='-',
help='Plot style')
parser.add_argument('--save', type=str, default=None,
help='Output file name')
parser.add_argument('-l', '--log', type=str, default='',
help='x or y or xy for logscale')
parser.add_argument('--header', action="store_true",
default=False,
help='use header')
parser.add_argument('-lp', '--legend_position', type=str,
default='lower center',
help='legend position or none for no legend')
args = parser.parse_args()
if args.i is None:
source = sys.stdin
else:
source = open(args.i, 'r')
header = None
if args.header:
if args.delimiter is not None:
header = source.readline().strip().split(args.delimiter)
else:
header = source.readline().strip().split()
skiprows = 1
else:
skiprows = 0
if args.delimiter is None:
data = np.loadtxt(source, skiprows=skiprows)
else:
data = np.loadtxt(source, delimiter=args.delimiter, skiprows=skiprows)
def get_color(icolor):
while icolor >= len(color_sequence):
icolor = icolor - len(color_sequence)
return color_sequence[icolor]
def get_style(icolor):
return '%s%s' % (get_color(icolor), args.style)
def convert_column(column):
# TODO: Add verification here.
if '-' in column:
low_column, up_column = column.split('-')
else:
low_column, up_column = column, column
low_column = int(low_column)
up_column = int(up_column)
return range(low_column, up_column + 1)
columns = []
for colpar in args.x.split(','):
columns.extend(convert_column(colpar))
if args.title is not None:
plt.title(args.title)
else:
plt.title(args.i)
if args.log == 'x':
plt.semilogx()
elif args.log == 'y':
plt.semilogy()
elif args.log == 'xy':
plt.loglog()
labels = []
for j, column in enumerate(columns[1:]):
plt.plot(data[:, columns[0]-1], data[:, column-1], get_style(j))
if args.header:
labels.append(r'$%s$' % header[column - 1])
plt.xlabel(header[columns[0] - 1])
else:
labels.append('Column %s' % (column - 1))
if args.legend_position != 'none':
plt.legend(labels, loc=args.legend_position,
fancybox=True, shadow=True, ncol=2)
if args.save is None:
plt.show()
else:
plt.tight_layout()
plt.savefig(args.save, format='png')