import matplotlib.pyplot as plt ''' CSCI 1470.2 Spring 2026 HW #4 ''' def plot_from_file(filename): """ Reads data from a specified file and generates a matplotlib figure. The file must have exactly seven lines: 1. Title 2. X coordinate label 3. Y coordinate label 4. X values (comma-separated) 5. Y values (comma-separated) 6. Figure type ('line', 'scatter', or 'bar') 7. Plot color ('red', 'green' or 'blue') There is no error checking in this simple programming assignment. """ with open(filename, 'r') as f: lines = f.readlines() # Extract data from lines title = lines[0].strip() xlabel = lines[1].strip() ylabel = lines[2].strip() # Convert comma-separated string values to numeric lists try: x_values = [float(x) for x in lines[3].strip().split(',')] y_values = [float(y) for y in lines[4].strip().split(',')] except ValueError: print("Error: X or Y values are not valid numbers. Ensure they are comma-separated floats or integers.") sys.exit(1) plot_type = lines[5].strip().lower() plot_color = lines[6].strip().lower() if not (plot_color == 'red' or plot_color == 'green' or plot_color == 'blue'): print(f"Warning: Possibly unknown plot type specified: '{plot_color}'. Choose 'red', 'green', or 'blue'") # Create the figure and axes fig, ax = plt.subplots() # Plot the data based on the specified type if plot_type == 'line': ax.plot(x_values, y_values, color=plot_color) elif plot_type == 'scatter': ax.scatter(x_values, y_values, color=plot_color) elif plot_type == 'bar': ax.bar(x_values, y_values, color=plot_color) else: print(f"Error: Unknown plot type specified: '{plot_type}'. Choose 'line', 'scatter', or 'bar'.") # Customize the plot ax.set_title(title) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.grid(False) # Display the plot plt.show() if __name__ == "__main__": # Specify the name of your data file plot_file_name = input("Please input the name of your file: ") plot_from_file(plot_file_name)