Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何将matplotlib basemap与ShapeFile一起使用_Python_Google Maps Api 3_Shapefile_Matplotlib Basemap - Fatal编程技术网

Python:如何将matplotlib basemap与ShapeFile一起使用

Python:如何将matplotlib basemap与ShapeFile一起使用,python,google-maps-api-3,shapefile,matplotlib-basemap,Python,Google Maps Api 3,Shapefile,Matplotlib Basemap,对于我大学的一个项目,我想使用Python 2.7显示一个(德国)城市的地图,主要是道路,最后包括几个服务点的地理位置。我有Python的基本知识,但我觉得我无法获得实现目标所需的步骤 我使用basemap来限制城市所在的lon和lat区域。然后我想使用一个shapefile来包含已经提供的道路,但是我得到了错误消息“RuntimeError:不能将单个艺术家放在多个图形中”。尽管搜索了一下,我还是找不到解决办法。以下是我使用的代码: import matplotlib.pyplot as pl

对于我大学的一个项目,我想使用Python 2.7显示一个(德国)城市的地图,主要是道路,最后包括几个服务点的地理位置。我有Python的基本知识,但我觉得我无法获得实现目标所需的步骤

我使用basemap来限制城市所在的lon和lat区域。然后我想使用一个shapefile来包含已经提供的道路,但是我得到了错误消息“RuntimeError:不能将单个艺术家放在多个图形中”。尽管搜索了一下,我还是找不到解决办法。以下是我使用的代码:

import matplotlib.pyplot as plt
import matplotlib.cm
import numpy as np
import shapefile as shp

from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.collections import LineCollection
from matplotlib.collections import PolyCollection
from matplotlib.colors import Normalize

fig, ax=plt.subplots(figsize=(10,20))

m = Basemap(resolution='c', # c, l, i, h, f or None
            projection='merc',
           #lat lowleft: llcrnrlon, lon low right llcrnrlat
            #lat upleft urcrnrlon=47.81,, lon up right urcrnrlat=10.5
            llcrnrlon=8.45195, llcrnrlat=49.477816, urcrnrlon=8.491432, urcrnrlat=49.50117)    

drawmapboundary(fill_color='aqua')
m.fillcontinents(color='firebrick')
m.drawrivers(color='aqua')
m.drawcoastlines()

plt.show()

m.readshapefile('roads', 'roads')
然后我得到了谷歌地图API,但在我看来,我不能用它们发布地图。此外,加载pygmaps包装程序会崩溃:“ImportError:没有名为pygmaps的模块”,而且无论如何,我都不知道如何使用它

这里有人知道我可以采取哪些步骤来实现我的目标吗?我真的非常感谢你的建议。提前谢谢

使用,在shapefile中读取非常简单:

m.readshapefile('path_to/shapefile/shapefile', 'name')
请注意,第一个参数不需要在shapefile名称的末尾使用
.shp
。如果要访问shapefile的元素,请执行以下操作:

name_info = m.readshapefile('path_to/shapefile/shapefile', 'name')
for info, shape in zip(m.name_info, m.name):
    print(info)

这将打印出包含该shapefile中的属性和值的字典。从那里,你可以玩shapefile。

请分享你使用的代码,这将澄清你的问题。我在原始帖子中编辑了它。请看一个关于如何在
Basemap
中使用shapefile的非常基本的示例,可能还有一个更复杂的示例。此外,有关绘制街道网络的不同方法,请参见。如果这些都不能帮助您,请让我们知道在哪里可以找到您尝试使用的shapefile。