PYTHON Question: So, I want to plot the latitude and longitude from the csv file
ID: 3780205 • Letter: P
Question
PYTHON Question:
So, I want to plot the latitude and longitude from the csv file in turtle graphics
file looks like this:
Count,country,temperature,latitude,longitude
1,Japan,65.5,-88.99,0.55
2,Combodia,77.8,-99.43,1.34
3,Brazil,90.3,-88.34,2.5
................................... list goes on
heres the code to start with:
import turtle
canvas = turtle.Screen()
t=turtle.Turtle()
listZipX= [-20,30, 50, -85]
listZipY= [20,-30, 100, -200]
t.color("red")
for i in range(len(listZipX)):
t.penup()
t.setx(listZipX[i])
t.sety(listZipY[i])
t.pendown()
t.dot()
canvas.mainloop()
Thank you!
Explanation / Answer
import turtle
canvas = turtle.Screen()
t=turtle.Turtle()
fname = 'input.csv'
with open(fname) as f:
input_data = f.read().splitlines()
#initialising data
data = []
#iterating through the list containing input data and storing each row as a list of floats.
for row in input_data:
row_data = [x for x in row.split(',')]
data.append(row_data)
X = []
Y = []
for country in data:
X.append(float(country[-1]));
Y.append(float(country[-2]));
#listZipX= [-20,30, 50, -85]
#listZipY= [20,-30, 100, -200]
t.color("red")
print X, Y
for i in range(len(X)):
t.penup()
t.setx(X[i])
t.sety(Y[i])
t.pendown()
t.dot()
canvas.mainloop()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.