|
| 1 | +import argparse |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import os |
| 4 | +import pandas as pd |
| 5 | +import sys |
| 6 | + |
| 7 | +lines = [] |
| 8 | +lined = dict() |
| 9 | + |
| 10 | +def on_pick(event): |
| 11 | + # on the pick event, find the orig line corresponding to the |
| 12 | + # legend proxy line, and toggle the visibility |
| 13 | + leg_line = event.artist |
| 14 | + if len(lined) == 1: |
| 15 | + orig_line = leg_line |
| 16 | + else: |
| 17 | + orig_line = lined[legline] |
| 18 | + vis = not orig_line.get_visible() |
| 19 | + orig_line.set_visible(vis) |
| 20 | + # Change the alpha on the line in the legend so we can see what lines |
| 21 | + # have been toggled |
| 22 | + if vis: |
| 23 | + leg_line.set_alpha(1.0) |
| 24 | + else: |
| 25 | + leg_line.set_alpha(0.2) |
| 26 | + plt.draw() |
| 27 | + |
| 28 | +# Function to read data and plot |
| 29 | +def plot_data(file_paths, title, xlabel, ylabel, plot_marker): |
| 30 | + plt.figure(figsize=(10, 5)) |
| 31 | + |
| 32 | + for file_path in file_paths: |
| 33 | + # Read the data from the .dat file |
| 34 | + try: |
| 35 | + data = pd.read_csv(file_path, sep='\t', header=None, names=['Time', 'Value']) |
| 36 | + base_name = os.path.splitext(os.path.basename(file_path))[0] |
| 37 | + marker_style = '' |
| 38 | + if plot_marker == True: |
| 39 | + marker_style = 'o' |
| 40 | + line, = plt.plot(data['Time'], data['Value'], marker=marker_style, linestyle='-', label=base_name, picker=True) |
| 41 | + lines.append(line) # Store the line for toggling |
| 42 | + except Exception as e: |
| 43 | + print(f"Error reading the file {file_path}: {e}") |
| 44 | + continue |
| 45 | + |
| 46 | + plt.title(title) |
| 47 | + plt.xlabel(xlabel) |
| 48 | + plt.ylabel(ylabel) |
| 49 | + plt.grid() |
| 50 | + legends = plt.legend(loc='upper right', fontsize='medium',labelspacing=1.0) |
| 51 | + |
| 52 | + for leg_line, orig_line in zip(legends.get_lines(), lines): |
| 53 | + leg_line.set_picker(5) # 5 pts tolerance |
| 54 | + lined[leg_line] = orig_line |
| 55 | + |
| 56 | + # Connect the pick event |
| 57 | + plt.gcf().canvas.mpl_connect('pick_event', on_pick) |
| 58 | + plt.show() |
| 59 | + |
| 60 | +# Set up argument parsing |
| 61 | +if __name__ == "__main__": |
| 62 | + parser = argparse.ArgumentParser(description='Plot data from multiple .dat files.') |
| 63 | + parser.add_argument('file_paths', type=str, nargs='+', help='Paths to the .dat files') |
| 64 | + parser.add_argument('title', type=str, help='Title of the plot') |
| 65 | + parser.add_argument('xlabel', type=str, help='Label for the x-axis') |
| 66 | + parser.add_argument('ylabel', type=str, help='Label for the y-axis') |
| 67 | + parser.add_argument('--plot_markers', help='Plot markers points', action=argparse.BooleanOptionalAction) |
| 68 | + |
| 69 | + args = parser.parse_args() |
| 70 | + |
| 71 | + # Check if the required arguments are provided |
| 72 | + if len(args.file_paths) < 1: |
| 73 | + print("Error: At least one file path must be provided.") |
| 74 | + parser.print_help() |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | + plot_data(args.file_paths, args.title, args.xlabel, args.ylabel, args.plot_markers) |
0 commit comments