In: Computer Science
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be "x", y should be "Fourier Sum" And the title be Fourier Series. Remember, you should have your first.last.yy in parentheses in the title. For full credit you must also include a legend with the dataset labels as shown above. Upload the resulting plot (.png) to scholar for credit. No unit tests here. :param file_path: :return: """
I am kinda confused on this one. the only txt file i think i need to use is called fourier_series.txt but I may not need it at all.
NOTE: The meaning of "first.last.yy" was not given, but otherwise the solution should be ok. The file named "fourier_series.txt" musr be in the same directory as the program. You must change the data in the file with actual data.
Solution:-
1. The content of the file used, is given below:-
//------------------------------------------------------------------
1:2:3:4
5:6:7:8
9:10:11:12
13:14:15:16
//------------------------------------------------------------------
2. The requird source-code is given below:-
#!/usr/bin/python3
import matplotlib.pyplot as plt
def plot_dataset(file_path):
# The Lists of Data
c1=[]
c2=[]
c3=[]
c4=[]
# Opening the File
file = open(file_path,'r')
while True:
# Get Next Line from File
line = file.readline()
# If line is Empty ,
# end of File is Reached
if not line:
break
# Extracting the Data from File
token=line.split(":")
c1.append(token[0])
c2.append(token[1])
c3.append(token[2])
c4.append(token[3])
# Closing the File
file.close()
# Setting the Title and axes
plt.title('Fourier Series')
plt.xlabel('x')
plt.ylabel('Fourier Sum')
# Plotting the first graph
plt.scatter(c1,c2)
plt.show()
# Setting the Title and axes
plt.title('Fourier Series')
plt.xlabel('x')
plt.ylabel('Fourier Sum')
# Plotting the second graph
plt.scatter(c1,c3)
plt.show()
# Setting the Title and axes
plt.title('Fourier Series')
plt.xlabel('x')
plt.ylabel('Fourier Sum')
# Plotting the third graph
plt.scatter(c1,c4)
plt.show()
# The Main Function
if __name__ == "__main__":
plot_dataset('fourier_series.txt')
3. The first plot is given below:-
4. The second plot is given below:-
5. The third plot is given below:-