Question

In: Computer Science

Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...

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.

Solutions

Expert Solution

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:-


Related Solutions

In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given...
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given file that contains words separated by spaces (perhaps multiple words on a line) and will create a dictionary whose keys are the words and the value is the number of times the word appears. Convert each word to lower case before processing.
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row = next(data_from_file) for index,column_header in enumerate(header_row): print(index,column_header) How many COUMNS (not rows!) will be printed in the above code?
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
Add an item to a Text File / C++ the program will prompt for a filename...
Add an item to a Text File / C++ the program will prompt for a filename AND then prompt for a text file item. text file items always contain an unknown amount of spaces, (see getline). Allow the user to put both the filename and the extensions, like groceries.txt or retirementToDo.clist Don't add any directory information to the filenames, use them as typed. If the file does exist, the item will be APPENDED to the end of the file. If...
Write a code in python to read in a file of spice like component and/or control...
Write a code in python to read in a file of spice like component and/or control data. break the data up into fields in a class object.
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT