我正在尝试按照这里的教程:
但是,我使用pandas而不是内置的csv模块用于python。我的代码如下:
import pandas as pd
eq_data = pd.read_csv('earthquake_data.csv')
map2 = Basemap(projection='robin'
, resolution='l'
, area_thresh=1000.0
, lat_0=0
, lon_0=0)
map2.drawcoastlines()
map2.drawcountries()
map2.fillcontinents(color = 'gray')
map2.drawmapboundary()
map2.drawmeridians(np.arange(0, 360, 30))
map2.drawparallels(np.arange(-90, 90, 30))
x,y = map2(eq_data['longitude'].values, eq_data['latitude'].values)
map2.plot(x,y, marker='0', markercolor='red', markersize=6)
这会产生AssertionError但没有描述:
AssertionError Traceback (most recent call last)
<ipython-input-64-d3426e1f175d> in <module>()
14 x,y = map2(range(20), range(20))#eq_data['longitude'].values, eq_data['latitude'].values)
15
---> 16 map2.plot(x,y, marker='0', markercolor='red', markersize=6)
c:\Python27\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in with_transform(self, x, y, *args, **kwargs)
540 # convert lat/lon coords to map projection coords.
541 x, y = self(x,y)
--> 542 return plotfunc(self,x,y,*args,**kwargs)
543 return with_transform
544
c:\Python27\lib\site-packages\mpl_toolkits\basemap\__init__.pyc in plot(self, *args, **kwargs)
3263 ax.hold(h)
3264 try:
-> 3265 ret = ax.plot(*args, **kwargs)
3266 except:
3267 ax.hold(b)
c:\Python27\lib\site-packages\matplotlib\axes.pyc in plot(self, *args, **kwargs)
4135 lines = []
4136
-> 4137 for line in self._get_lines(*args, **kwargs):
4138 self.add_line(line)
4139 lines.append(line)
c:\Python27\lib\site-packages\matplotlib\axes.pyc in _grab_next_args(self, *args, **kwargs)
315 return
316 if len(remaining) <= 3:
--> 317 for seg in self._plot_args(remaining, kwargs):
318 yield seg
319 return
c:\Python27\lib\site-packages\matplotlib\axes.pyc in _plot_args(self, tup, kwargs)
303 ncx, ncy = x.shape[1], y.shape[1]
304 for j in xrange(max(ncx, ncy)):
--> 305 seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
306 ret.append(seg)
307 return ret
c:\Python27\lib\site-packages\matplotlib\axes.pyc in _makeline(self, x, y, kw, kwargs)
255 **kw
256 )
--> 257 self.set_lineprops(seg, **kwargs)
258 return seg
259
c:\Python27\lib\site-packages\matplotlib\axes.pyc in set_lineprops(self, line, **kwargs)
198 raise TypeError('There is no line property "%s"' % key)
199 func = getattr(line, funcName)
--> 200 func(val)
201
202 def set_patchprops(self, fill_poly, **kwargs):
c:\Python27\lib\site-packages\matplotlib\lines.pyc in set_marker(self, marker)
851
852 """
--> 853 self._marker.set_marker(marker)
854
855 def set_markeredgecolor(self, ec):
c:\Python27\lib\site-packages\matplotlib\markers.pyc in set_marker(self, marker)
231 else:
232 try:
--> 233 Path(marker)
234 self._marker_function = self._set_vertices
235 except ValueError:
c:\Python27\lib\site-packages\matplotlib\path.pyc in __init__(self, vertices, codes, _interpolation_steps, closed, readonly)
145 codes[-1] = self.CLOSEPOLY
146
--> 147 assert vertices.ndim == 2
148 assert vertices.shape[1] == 2
149
AssertionError:
我认为我遇到了问题,因为对pandas的更新不再像以前那样允许传递系列:
Runtime error using python basemap and pyproj?
但正如您所看到的,我为此调整了代码并且没有解决问题。此时我迷失了。
我在Windows Server 2012 x64上使用Python 2.7.6,pandas 0.15.2和basemap 1.0.7。
答案 0 :(得分:2)
我的代码有两个问题。首先,map2对象的plot函数继承自matplotlib。因此,marker
属性不能是'0'
,而是'o'
。此外,没有markercolor
属性。它被称为color
。以下代码应该有效。
import pandas as pd
eq_data = pd.read_csv('earthquake_data.csv')
map2 = Basemap(projection='robin'
, resolution='l'
, area_thresh=1000.0
, lat_0=0
, lon_0=0)
map2.drawcoastlines()
map2.drawcountries()
map2.fillcontinents(color = 'gray')
map2.drawmapboundary()
map2.drawmeridians(np.arange(0, 360, 30))
map2.drawparallels(np.arange(-90, 90, 30))
x,y = map2(eq_data['longitude'].values, eq_data['latitude'].values)
map2.plot(x,y, marker='o', color='red', markersize=6, linestyle='')