import numpy as np import matplotlib.pyplot as plt import os import sys ''' CSCI 1470.2 Spring 2026 HW #5 ''' def compute_and_plot_histogram(filename): if not os.path.exists(filename): print(f"Error: The file '{filename}' was not found.") print(f"Please ensure '{filename} is in the same directory as this script.") return data = None try: # Read float 64 data from the input data file. data = np.loadtxt(filename, dtype=np.float64) # Flatten the array in case the data is read as a 2D array except ValueError as e: print(f"Error reading file: {e}") print("Ensure the file only contains integers or floating point numbers, and standard delimiters (spaces/commas).") except Exception as e: print(f"An unexpected error occurred: {e}") flat_data = data.flatten() data_size = flat_data.size # The numbe of bins is the number of data points // 4, up to 20 bins. number_of_bins = min(data_size // 4, 20) # Compute the histogram using numpy. # numpy.histogram returns two arrays: # [1] 'counts': the number of items in each bin # [2] 'bin_edges': the bin edges. There is one more bin_edge than there are counts' values. counts, bin_edges = np.histogram(data, bins=number_of_bins) print('-' * 55) print(f'-- Histogram of Data from {filename}.') print(f'-- [range: left-inclusive, except the last]: frequency') print('-' * 55) for i in range(len(counts)): print(f"bin {i+1} => [{bin_edges[i]},{bin_edges[i+1]}]: {counts[i]}") # Plot the histogram using matplotlib plt.figure(figsize=(10, 6)) # matplolib.pyplot.hist can do the calculation and plotting in one step. plt.hist(flat_data, bins=number_of_bins, color='g', rwidth=0.8) plt.title(f'Histogram of Data from {filename}') plt.xlabel('Value') plt.ylabel('Frequency') plt.grid(axis='y', alpha=0.75) # 5. Display the plot plt.show() if __name__ == "__main__": data_file_name = input("Please input the name of your file: ") compute_and_plot_histogram(data_file_name)