Question

In Python Please use the same USPopulation.txt file from Project 8 as input data. For this project, read the data into a list of lists (a 2-dimensional list, 4 decades, each decade with 10 years). Com...

In Python

Please use the same USPopulation.txt file from Project 8 as input data. For this project, read the data into a list of lists (a 2-dimensional list, 4 decades, each decade with 10 years). Compute the annual change in the population, and the year with the maximum and minimum population change. Using Anaconda, plot a graph for each decade's population change.

The attached file USPopulation.txt contains the US population in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the population for 1951, and so forth.

USPopulation.txt

151868
153982
156393
158956
161884
165069
168088
171187
174149
177135
179979
182992
185771
188483
191141
193526
195576
197457
199399
201385
203984
206827
209284
211357
213342
215465
217563
219760
222095
224567
227225
229466
231664
233792
235825
237924
240133
242289
244499
246819
249623

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

#importing the numpy to reshape into desired shape
import numpy as np
#importing matplotlib to plot the graph
import matplotlib.pyplot as plt
sublist = list()
#decade x label
decades = ["1-2","2-3","3-4"]
#opening the file to read the data
with open("USPopulation.txt","r") as rd:
   lines = rd.readlines()
   for line in lines:
       #stroing the values to list
       sublist.append(int(line))
#converting into numpy array
sublist = np.array(sublist)
#reshaping the list to list of list
sublist = np.reshape(sublist,(4,10))
diff=0
#list to hold each decade differenece
difflist = list()
for i in range(1,sublist.shape[0]):
   diff = sum(sublist[i]) - sum(sublist[i-1])
   difflist.append(diff)
#variable to hold max difference
maxdiff = 0
#variable to hold min difference
mindiff = 100000
for i in sublist:
   for j in range(1,len(i)):
       diffYear = i[j]-i[j-1]
       if(diffYear<mindiff):
           mindiff = diffYear
       if(diffYear>maxdiff):
           maxdiff = diffYear

print("the maximum difference between population",maxdiff)
print("the minimum difference between population",mindiff)
#plotting the graph
plt.bar(decades, difflist, align='center', alpha=0.5)
plt.xlabel("difference of decades")
plt.ylabel('')
#show the graph
plt.show()

Command Prompt CENUsers Dell1 Desktop>python population.py the maximum difference between po pulation 3185 the minimum differ

250000 200000 150000 100000 50000 0 1-2 2-3 difference of decades 3-4

Add a comment
Know the answer?
Add Answer to:
In Python Please use the same USPopulation.txt file from Project 8 as input data. For this project, read the data into a list of lists (a 2-dimensional list, 4 decades, each decade with 10 years). Com...
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
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