-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_plotter.py
More file actions
74 lines (55 loc) · 1.75 KB
/
csv_plotter.py
File metadata and controls
74 lines (55 loc) · 1.75 KB
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
import argparse
import sys
import pandas as pd
import matplotlib.pyplot as plt
def parse_column_input(user_input, available_columns):
"""
Parse a comma-separated list of column names and validate them.
"""
columns = [col.strip() for col in user_input.split(",") if col.strip()]
invalid = [col for col in columns if col not in available_columns]
if invalid:
raise ValueError(f"Invalid column(s): {', '.join(invalid)}")
if not columns:
raise ValueError("No columns selected.")
return columns
def main():
parser = argparse.ArgumentParser(description="Interactive CSV plotting tool")
parser.add_argument("csv_file", help="Path to the CSV file")
args = parser.parse_args()
try:
df = pd.read_csv(args.csv_file)
except Exception as e:
print(f"Failed to read CSV file: {e}")
sys.exit(1)
if df.empty:
print("The CSV file is empty.")
sys.exit(1)
print("\nAvailable columns:")
for col in df.columns:
print(f" - {col}")
try:
x_input = input(
"\nEnter X column(s) (comma-separated): "
)
x_columns = parse_column_input(x_input, df.columns)
y_input = input(
"Enter Y column(s) (comma-separated): "
)
y_columns = parse_column_input(y_input, df.columns)
except ValueError as e:
print(f"Input error: {e}")
sys.exit(1)
# Plotting
plt.figure()
for x_col in x_columns:
for y_col in y_columns:
plt.plot(df[x_col], df[y_col], label=f"{y_col} vs {x_col}")
plt.xlabel(", ".join(x_columns))
plt.ylabel(", ".join(y_columns))
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
if __name__ == "__main__":
main()