简体   繁体   English

在python上绘制坐标-使用哪种数据类型?

[英]Plotting co-ordinates on python - what data type to use?

I'm working on a task that involves isolating information from a CSV document (to be specific, the timestamp in nanoseconds and the latitude and longitude). 我正在进行一项工作,涉及从CSV文档中隔离信息(具体来说,时间戳以纳秒为单位,并且包含纬度和经度)。 I have managed to print these values successfully, but now I would like to plot it using matplotlib. 我已经成功打印了这些值,但是现在我想使用matplotlib对其进行绘制。 This is my code so far: 到目前为止,这是我的代码:

import csv
from matplotlib import pyplot as plt

midnight = 1429574400
nsmidnight = 1429574400000000000
ifile  = open('(short file)Converted_radar_tracks_20150421_053822.csv', "rb")
reader = csv.DictReader(ifile, delimiter=';')

rownum = 0

for row in reader:#for each row
    if rownum == 0:
        header = row #define header row
    else:
        print "Timestamp:", long(row['timestamp'])*1000000000+nsmidnight+ int(row['timestamp_nsecs']),"Latitude:", float(row['lat']), "Longitude:", float(row['lon'])
        plt.plot('lat','lon')

    rownum += 1

plt.show()
ifile.close

However, when I run it, I get this error message: 但是,当我运行它时,出现以下错误消息:

ValueError: Unrecognized character l in format string

I think it may be because I am using the wrong data type for latitude and longitude. 我认为可能是因为我为纬度和经度使用了错误的数据类型。 Does anyone have any suggestions to help me? 有人对我有什么建议吗?

When you write plt.plot('lat','lon') , it is trying to plot the string lat with a format string lon . 当您编写plt.plot('lat','lon') ,它将尝试以格式字符串lon绘制字符串lat This does not work and gives you the error you see. 这不起作用,并给您看到的错误。 What plt.plot() wants as arguments are lists of latitudes and longitudes. plt.plot()想要作为参数的是纬度和经度列表。 For example, 例如,

lat = [13, 26, 48]
lon = [51, 17, 19]
plt.plot(lat, lon)
plt.show()

plt.plot() takes a list or array of x and y pairs, not strings as you are passing with plt.plot('lat', 'lon') . plt.plot()采用x和y对的列表或数组,而不是字符串,因为您通过plt.plot('lat', 'lon')进行传递。 The ValueError you are getting is due to this. 您得到的ValueError是由于此。 Instead, put your values for latitude and longitude into a list or numpy array first, then pass them to plt.plot() like so: 而是将纬度和经度的值首先放入列表或numpy数组中,然后将其传递给plt.plot()如下所示:

lat = [41, 42, 43, 44, 45]
lon = [70, 71, 72, 73, 74]
plt.plot(lat, lon)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM