尝试在名为test_gps.csv的CSV文件中绘制地图上的gps坐标时,我收到错误:
ValueError: Some errors were detected !
Line #1 (got 2 columns instead of 2)
Line #2 (got 2 columns instead of 2)
...
我的代码是:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
airports = np.genfromtxt("test_gps.csv",
delimiter=';',
dtype=[('lat', np.float32), ('lon', np.float32)],
usecols=(1, 2))
fig = plt.figure()
themap = Basemap(projection='gall',
llcrnrlon = -15,
llcrnrlat = 28,
urcrnrlon = 45,
urcrnrlat = 73,
resolution = 'l',
area_thresh = 100000.0,
)
themap.drawcoastlines()
themap.drawcountries()
themap.fillcontinents(color = 'gainsboro')
themap.drawmapboundary(fill_color='steelblue')
x, y = themap(airports['lon'], airports['lat'])
themap.plot(x, y,
'o',
color='Indigo',
markersize=4
)
plt.show()
CSV的格式为:
-344.586.792;-585.306.702
-314.071.598;-641.856.689
-3.435.215;-587.938.194
-346.999.893;-583.838.615
-517.951.889;-594.954.567
-517.951.889;-594.954.567
474.808.006;97.561.398
我试图将数据格式更改为其他扩展名和分隔符,但我仍然得到相同的错误。任何想法我可以做错什么?!感谢
答案 0 :(得分:0)
这是因为usecols中的索引从0开始而不是1.并且代码期望另一列读取数据并抛出错误。
代码中的这一变化能够读取值。
airports = np.genfromtxt("test_gps.csv",
delimiter=';',
dtype=[('lat', np.float32), ('lon', np.float32)],
usecols=(0, 1))
另外,请尝试将.csv文件中的值更改为只有一个小数点,因为值为nan
,如果它有两个小数点。