简体   繁体   English

Python在matplotlib底图中绘制逗号分隔坐标(lat,long)

[英]Python plotting comma separated coordinates (lat, long) in matplotlib basemap

I am working on the Alienvault reputation.data file. 我正在处理Alienvault的声誉.data文件。 It is a list of 40k malicious IP address and their locations. 它是40k恶意IP地址及其位置的列表。 I have read the file in like so 我已经读过这样的文件了

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

I want to take out the Coords column and use the lat long figures to plot them as a scatter plot on a world map. 我想取出Coords列并使用lat long数字将它们绘制成世界地图上的散点图。 The coordinates are latitude and longitude, comma separated in the columns, they are floats like 21.0333003998,105.849998474. 坐标是纬度和经度,在列中以逗号分隔,它们是浮点数,如21.0333003998,105.849998474。 The world map is coded from Basemap thus 因此,世界地图从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

So now I want to plot the lat long figures onto the map. 所以现在我想把拉长的数字绘制到地图上。 This is what I have. 这就是我所拥有的。

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)

Here is the output 这是输出

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

Why is there no scatter plot? 为什么没有散点图? Any assistance is appreciated. 任何帮助表示赞赏。

The error can be reproduced using the following example. 可以使用以下示例重现该错误。

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)

The problem is the line coordinates = df[['Coords']] where a column indexing is attempted using a single element list. 问题是line coordinates = df[['Coords']] ,其中使用单个元素列表尝试列索引。 This is not possible. 这是不可能的。
Instead use 而是使用

coordinates = df['Coords']

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

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