Enhanced the function to extract data and plot it