Question

Write a program using python that reads from values from a text file and plots them...

Write a program using python that reads from values from a text file and plots them using matplotlib. The input data for each graph is on three lines of the input textfile. The first line contains the x-coordiates of the points of the graph, and the second line contains the y-coordinates of the points of the graph that correspond, in the same order, to the x-coordinates on the first line. The third line in each group of three lines may be blank, in which case the graph will be drawn using all of the defaults. Or that third line may contain the stying information to be used when drawing the graph. This styling info must consist of a string containing from one to four characters giving the line style, the line color, and/or the symbol to be used to mark the points of the graph. Here are the possibilities: Line markers: solid (-), dashed (--), dash-dot (-.), dotted (:) Point markers: . o v ^ < > * + x d D Colors: b g r c m y k w. For example, if three lines of the file contain the following data

1 2 3

2 4 6

--rD

this will produce a straight-line graph determined by the three points shown, with a "large" diamond symbol marking each point and with the points joined by a dashed red line. On the other hand, if the following three lines (the third line is blank) are one group of three lines in the file then the graph will consist of a solid blue line (the defaults) joining the points, and the points themselves will not have any marker.

1 2 3

2 4 6

*Blank line*

The name of the file is read in as the single command-line input, and there is no error-checking required for the existence of the file. Any input file may contain any number of three-line groups of data, so the output of any input file can be any number of graphs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import matplotlib.pyplot as plt
%matplotlib inline

#intializing the variables
marker = ''
linestyle = '-'
color = 'b'
lines = []

#open file
file = open("C:/Users/Win10/Documents/input.txt")

#get the lines
lines = file.readlines()

#remove \n from each line and add ' ' to identify and empty line
lines = list(map(lambda x: x.split('\n')[0]+' ', lines))
#getting the groups of 3
plots = int(len(lines)/3)

#plotting for the number of groups
for i in range(plots):

#populating x and y lists
x = list(map(lambda x: int(x),lines[(3*i)+0].split()))
y = list(map(lambda x: int(x), lines[(3*i)+1].split()))

#if the 3rd line is an empty line in the group
if lines[3*i + 2] == ' ':
plt.figure(figsize = (3,3))
plt.plot(x, y)
plt.show()
else:
#if the length of the 3rd line is 1 that means only the linestyle is provided
if len(lines[3*i+2].replace(' ','')) == 1:
linestyle = lines[(3*i)+2][0]
  
plt.figure(figsize = (3,3))
plt.plot(x, y,linestyle = linestyle)
plt.show()
#if the length of the third line is 2 that means either only the linestyle is given
#or the linestyle and the color is given
elif len(lines[3*i+2].replace(' ','')) == 2:
#if the second character is not a - or . the second char is the color
if lines[(3*i)+2][1] != '-' and lines[(3*i)+2][1] != '.':
linestyle = lines[(3*i)+2][0]
color = lines[(3*i)+2][1]
#else only the linestyle is given
else:
linestyle = lines[(3*i)+2][0:2]
#since b is the default color
color = 'b'
  
plt.figure(figsize = (3,3))
plt.plot(x, y, color = color, linestyle = linestyle)
plt.show()
#if the length is 3 that means either the linestyle, color and marker are given or only
#linestyle and color
elif len(lines[3*i+2].replace(' ','')) == 3:
#similar as above
if lines[(3*i)+2][1] != '-' and lines[(3*i)+2][1] != '.':
linestyle = lines[(3*i)+2][0]
color = lines[(3*i)+2][1]
marker = lines[(3*i)+2][2]
else:
linestyle = lines[(3*i)+2][0:2]
color = lines[(3*i)+2][2]
marker = ''
  
plt.figure(figsize = (3,3))
plt.plot(x, y, color = color ,marker = marker, linestyle = linestyle)
plt.show()
#else all parameters are provided
else:
linestyle = lines[(3*i)+2][0:2]
color = lines[(3*i)+2][2]
marker = lines[(3*i)+2][3]
  
plt.figure(figsize = (3,3))
plt.plot(x, y, color = color ,marker = marker, linestyle = linestyle)
plt.show()
  
#the variables are initialized
x = []
y = []
marker = ''
linestyle = '-'
color = 'b'

______________________________________________________________

CODE IMAGES:

__________________________________________________________________

OUTPUT:

____________________________________________________________________

INPUT FILE:

input.txt

1 2 3
2 4 6
--rD
1 2 3
6 2 9

1 2 3 4 5
2 4 6 12 15
:r
__________________________________________________________________

Feel free to ask any questions in the comments section
Thank You!

Add a comment
Know the answer?
Add Answer to:
Write a program using python that reads from values from a text file and plots them...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a program in python that reads each line in a file, reverses its lines, and...

    Write a program in python that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had...

  • Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a...

    Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a file called Numbers.txt in your the same folder as your Python program. This file should contain a list on floating point numbers, one on each line. The number should be greater than zero but less than one million. Your program should read the following and display: The number of numbers in the file (i.e. count them) (counter) The maximum number in the file (maximum)...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...

    (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons (;) or pound signs (#) should be ignored. All empty lines must be ignored Lines containing a string between square brackets are sections. Within each section, lines may contain numbers or strings. For each section, calculate the lowest, highest and modal (most common) number, and print it. In each section, also count all non-whitespace characters, and print that. Example input: ; this is a...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT