2016-12-02 31 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 –