2016-12-02 28 views
0

我正尝试使用底图和熊猫来创建等值线图,以绘制跨CCG(NHS临床试运行组)的处方率水平。我正在从提供CCG区域边界的http://geoportal.statistics.gov.uk/datasets/1bc1e6a77cdd4b3a9a0458b64af1ade4_1下载形状文件。然而,我遇到的最初问题是读形状文件。 以下错误引起的:使用底图和大熊猫创建等值线图

raise IOError('cannot locate %s.shp'%shapefile) 

这是到目前为止我的代码...

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.cm 

from mpl_toolkits.basemap import Basemap 
from matplotlib.patches import Polygon 
from matplotlib.collections import PatchCollection 
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_0=54.5, lon_0=-4.36, 
llcrnrlon=-6., llcrnrlat= 49.5, urcrnrlon=2., urcrnrlat=55.2) 

m.drawmapboundary(fill_color='#46bcec') 
m.fillcontinents(color='#f2f2f2',lake_color='#46bcec') 
m.drawcoastlines() 

m.readshapefile('/Volumes/Clinical_Commissioning_Groups_April_2016_Full_Extent_Boundaries_in_England', 'areas', drawbounds =True) 
m.areas 

df_poly = pd.DataFrame({'shapes': [Polygon(np.array(shape), True) for shape in m.areas],'area': [area['ccg16cd'] for area in m.areas_info]}) 

rates=pd.read_csv('Volumes/TOSHIBA EXT/Basemap rates.csv', delimiter=",", usecols=[0,6]) 
rates.columns = ['ccg16cd','MEAN YEARLY PRESCRIPTION RATE'] 
frame = df_poly.merge(rates, on='ccg16cd', how='left') 

cmap = plt.get_cmap('Oranges') 
pc = PatchCollection(df_poly.shapes, zorder=2) 
norm = Normalize() 

pc.set_facecolor(cmap(norm(df_poly['count'].fillna(0).values))) 
ax.add_collection(pc) 

mapper = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap) 

mapper.set_array(df_poly['count']) 
plt.colorbar(mapper, shrink=0.4) 

m 

希望任何指针,我怎么能达到这个地区分布图 - 从什么错误在阅读shapefile。

回答

0

尝试使用geopandas在shape文件读取:

进口geopandas如GP

shape_file = gp.read_file( 'FileName.shp')

另外,检查路径shape文件是正确的。

+0

我得到一个导入错误:没有名为geopandas的模块 –

+0

您需要使用以下命令安装软件包:pip install geopandas –

+0

更多信息请点击http://geopandas.org/install.html –