【问题标题】:Python plotting comma separated coordinates (lat, long) in matplotlib basemapPython在matplotlib底图中绘制逗号分隔的坐标(纬度,经度)
【发布时间】:2017-03-17 06:21:07
【问题描述】:

我正在处理 Alienvault 信誉数据文件。它是一个 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。因此,世界地图是从 Basemap 编码的

#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

为什么没有散点图?感谢您提供任何帮助。

【问题讨论】:

  • 欢迎来到 Stackoverflow。 “没有快乐”不是一个充分的问题描述。问题是什么?如果你运行你的代码会发生什么?如果有错误,请包括回溯。 Edit您的问题。

标签: python pandas matplotlib matplotlib-basemap


【解决方案1】:

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

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']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多