我正在尝试使用底图和熊猫创建一个等值区域图,以绘制CCG(NHS临床调试组)的处方率水平。我正在从http://geoportal.statistics.gov.uk/datasets/1bc1e6a77cdd4b3a9a0458b64af1ade4_1下载shapefile,它提供了CCG区域边界。但是我遇到的最初问题是读取shapefile。 出现以下错误:
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 :(得分:0)
尝试使用geopandas读取shapefile:
将geopandas导入为gp
shape_file = gp.read_file(' FileName.shp')
另外,检查shapefile的路径是否正确。