Add plot script for easily plotting curves
This commit is contained in:
parent
f75b2469c4
commit
9181dc630d
50
bin/plot
Executable file
50
bin/plot
Executable file
@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def parse_input(lines):
|
||||
x_vals = []
|
||||
y_vals = []
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
parts = line.replace(',', ' ').split()
|
||||
if len(parts) == 1:
|
||||
y_vals.append(float(parts[0]))
|
||||
x_vals.append(len(x_vals))
|
||||
elif len(parts) >= 2:
|
||||
x_vals.append(float(parts[0]))
|
||||
y_vals.append(float(parts[1]))
|
||||
else:
|
||||
continue
|
||||
|
||||
return x_vals, y_vals
|
||||
|
||||
|
||||
def parse_file(file):
|
||||
with open(file, "r") as f:
|
||||
lines = f.readlines()
|
||||
return parse_input(lines)
|
||||
|
||||
|
||||
def parse_stdin():
|
||||
lines = sys.stdin.readlines()
|
||||
return parse_input(lines)
|
||||
|
||||
|
||||
def plot(x_vals, y_vals):
|
||||
plt.plot(x_vals, y_vals)
|
||||
plt.grid(True)
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) == 2:
|
||||
x_vals, y_vals = parse_file(sys.argv[1])
|
||||
else:
|
||||
x_vals, y_vals = parse_stdin()
|
||||
|
||||
plot(x_vals, y_vals)
|
||||
Loading…
x
Reference in New Issue
Block a user