1+ #!/usr/bin/env python3
2+
3+ from pathlib import Path
4+ import argparse
5+ import pandas as pd
6+ import matplotlib .pyplot as plt
7+ from typing import list , tuple
8+
9+ def read_csv_data (file_path : Path , x_col : str , y_col : str ) -> tuple [list [float ], list [float ]]:
10+ """Read data from a CSV file and return specified columns.
11+
12+ Args:
13+ file_path: Path to the CSV file
14+ x_col: Name of the column to use for x-axis
15+ y_col: Name of the column to use for y-axis
16+
17+ Returns:
18+ Tuple of x and y data as lists
19+ """
20+ df = pd .read_csv (file_path )
21+ return df [x_col ].tolist (), df [y_col ].tolist ()
22+
23+ def create_plot (x_data : list [float ], y_data : list [float ],
24+ x_label : str , y_label : str , title : str ) -> plt .Figure :
25+ """Create a plot from the provided data.
26+
27+ Args:
28+ x_data: Data for x-axis
29+ y_data: Data for y-axis
30+ x_label: Label for x-axis
31+ y_label: Label for y-axis
32+ title: Plot title
33+
34+ Returns:
35+ matplotlib Figure object
36+ """
37+ fig , ax = plt .subplots ()
38+ ax .plot (x_data , y_data )
39+ ax .set_xlabel (x_label )
40+ ax .set_ylabel (y_label )
41+ ax .set_title (title )
42+ return fig
43+
44+ def main () -> None :
45+ parser = argparse .ArgumentParser (description = "Create plots from CSV data" )
46+ parser .add_argument ("file_path" , type = Path , help = "Path to the CSV file" )
47+ parser .add_argument ("x_column" , type = str , help = "Column name for x-axis" )
48+ parser .add_argument ("y_column" , type = str , help = "Column name for y-axis" )
49+ parser .add_argument ("--output" , "-o" , type = Path , default = Path ("plot.png" ),
50+ help = "Output file path (default: plot.png)" )
51+ parser .add_argument ("--title" , "-t" , type = str , default = "Data Plot" ,
52+ help = "Plot title (default: Data Plot)" )
53+
54+ args = parser .parse_args ()
55+
56+ x_data , y_data = read_csv_data (args .file_path , args .x_column , args .y_column )
57+ fig = create_plot (x_data , y_data , args .x_column , args .y_column , args .title )
58+ fig .savefig (args .output )
59+ plt .close (fig )
60+
61+ if __name__ == "__main__" :
62+ main ()
0 commit comments