I have a txt file from which I need to draw data and use it to plot the path and
ID: 3747757 • Letter: I
Question
I have a txt file from which I need to draw data and use it to plot the path and a bar graph. I am having trouble designing the dictionary and lists for containing the information. Also could you suggest how I would plot them using the dictionaries. I will attach my code, the txt file, and assignment help sheet.
https://www.dropbox.com/s/yb67xy52rsuy2gy/ZebraBotswana.txt?dl=0
import pylab
from math import *
def distance(lon1, lat1, long2, lat2):
l1 = radians(float(long1))
l2 = radians(float(lat1))
p1 = radians(float(lat1))
p2 = radians(float(lat2))
dp = p2 -p1
d1 = l2 - l1
a = sin(dp/2)**2 + cos(p1)*cos(p2)*sin(d1/2)**2
c = atan2(sqrt*a, sqrt*1-a)
d = 6371 * c
return d
def Getltlg():
zebraltdict = {}
zebralgdict = {}
infile = open("ZebraBotswana.txt", "r")
readlines = infile.readlines()
Types = [line.split(',') for line in readlines]
if tuple(Types[3]) not in zebraltdict:
zebraltdict.setdefault(tuple(Types[3]), [])
if tuple(Types[3]) not in zebralgdict:
zebralgdict.setdefault(tuple(Types[3]), [])
for keys in zebraltdict.keys():
ltd = [Type[1] for Type in Types if Types[3] == keys]
zebraltdict[keys].append(ltd)
for keys in zebralgdict.keys():
lat = [Type[2] for Type in Types if Types[3] == keys]
zebralgdict[keys].append(lat)
infile.close()
The position data of several zebra (Equus quagga burchell) in northern Botswana are given in the file ZebraBotswana.txt. (source: movebank.org) The comma unix timestam degrees), and the number of the corresponding zebra (e.g., 23864). separated data consist of the date and time of the measurement (in p format), the longitude and latitude of the measured position (in Write a program to do the following: 1. Read the data and display a plot of the path of each zebra. Use a different color for each zebra. 2. Display a bar graph of the average distance traveled per day of each Submit your program as a single Python file with the name Zebra. py Mathematics: The following function will compute the distance in kilometers zebra. between two longitude-latitude positions. from math import * def distance(long1,latl,long2,lat2): 11-radians (float (lon 12-radians(float(long2)) pl-radians(float(latl)) p2-radians(float(lat2)) dp-p2-pl dl-12-11 a=sin(dp / 2 ) * * 2 + cos (p1 ) *cos (p2)*sin(a1 /2 ) * * 2 c-2*atan2 (sqrt(a),sqrt (1-a)) d-6371*c return d g1))Explanation / Answer
I could help you with writing the code completely but that wouldn't be fun to learn at all.
However, I'm gonna describe the algorithm for you and leave it upto you, to implement.
ALGO:
1. Read line by line. Split each value using the comma(,) provided in your data file.
2. The last word on each line describes the number of the zebra.
Keep that in a variable and see if that changes. If it does you have a new zebra then.
You need to plot your dict and then empty it if this condition is True.
OR you can store these in a new dictionary and continue processing the txt file.
3. You have the given function to calculate the distance between coordinates. Use that to find the distance. Store it as a third key in the dictionary.
The first part is easy when it comes to collecting data and caching them all in a dictionary.
What you're doing is correct. Work along those lines, you should be able to get there.
4. Plotting the paths:
This is non-trivial. To begin with, plotting the graph I would suggest you use Matplotlib.
You'll find that it's available in Pypi and you can install it using pip.
I would like to see you getting that dictionary correct first and then you can revert on this answer and I'll help you with plotting the graphs.
Involving the steps here to plot the graph would be too much information, too soon.
I do not want to overwhelm you with information and scare you away.
Please get in touch with me when you have the dictionary ready using the algorithm I mentioned.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.