Python 在 matplotlib 底图中绘制逗号分隔的坐标(纬度、经度)



我正在处理Alienvault reputation.data文件。它是 40k 恶意 IP 地址及其位置的列表。我已经这样阅读了文件

addresses_columns = ["IP", "Reliability", "Risk", "Type", "Country", "Locale", "Coords", "x"]
ip_addresses = pd.read_csv('reputation.data', sep='#', names=addresses_columns)

我想去掉坐标列,并使用经度长的数字将它们绘制为世界地图上的散点图。坐标是纬度和经度,逗号分隔在列中,它们是浮点数,如 21.0333003998,105.849998474。世界地图是从底图编码的,因此

#import the world map from basemap
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
# Define the projection, scale, the corners of the map, and the resolution.
m = Basemap(projection='merc',llcrnrlat=-80,urcrnrlat=80,
        llcrnrlon=-180,urcrnrlon=180,lat_ts=20,resolution='c')
# Draw the coastlines
m.drawcoastlines()
# Color the continents
m.fillcontinents(color='#ffcc99',lake_color='#ccffff')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,91.,30.))
m.drawmeridians(np.arange(-180.,181.,60.))
# fill in the oceans
m.drawmapboundary(fill_color='#ccffff')
plt.title("Map of IP Addresses")
plt.show

所以现在我想在地图上绘制经纬度数字。这就是我所拥有的。

coordinates = ip_addresses[['Coords']]
for index in range(len(coordinates)):
    lat, lon = coordinates[index].split(",")
    print "lat=%s, lon=%s" % (lat, lon)
    x,y = map(lon, lat)
    map.plot(x, y, 'bo', markersize=2)

这是输出

Traceback (most recent call last):   File "./assignment.py", line 85, in <module>
    lat, lon = coordinates[index].split(",")   File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2059, in __getitem__
    return self._getitem_column(key)   File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2066, in _getitem_column
    return self._get_item_cache(key)   File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1386, in _get_item_cache
    values = self._data.get(item)   File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3543, in get
    loc = self.items.get_loc(item)   File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 2136, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
   File "pandas/index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas/index.c:4145)
   File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4009)
   File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13166)
   File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13120)
   KeyError: 0

为什么没有散点图?任何帮助将不胜感激。

可以使用以下示例重现该错误。

import pandas as pd
import numpy as np
x = np.random.rand(10, 2)
d = ["{},{}".format(x[i,0], x[i,1]) for i in range(x.shape[0])]
df = pd.DataFrame({"Coords": d})
coordinates = df[['Coords']]
for index in range(len(coordinates)):
    lat, lon = coordinates[index].split(",")
    print "lat=%s, lon=%s" % (lat, lon)

问题是尝试使用单个元素列表进行列索引的行coordinates = df[['Coords']]。这是不可能的。
而是使用

coordinates = df['Coords']

相关内容

  • 没有找到相关文章

最新更新