虽然这个问题在目前的形式中似乎无法回答,但我至少会注意到,在使用底图时您似乎出了问题 - 您不想导入 Shapefile,而只是使用readshapefile
的方法Basemap
像这样的对象:
m = Basemap(projection='tmerc')
m.readshapefile("/path/to/your/shapefile", "mexican_states")
然后,您将能够通过以下方式访问每个州边界的坐标m.mexican_states
(作为数组列表)和相应的信息(例如名称,可能是识别代码)m.mexican_states_info
。然后,您将需要某种包含状态名称/代码的字典或数据帧(对应于中的内容)m.mexican_states_info
) 和您想要绘制的值。一个简单的例子会像这样工作,假设你有一个名为mexican_states_sick_people
看起来像{"Mexico City":123, "Chiapas":35, ...}
:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from mpl_toolkits.basemap import Basemap
from shapely.geometry import Polygon
from descartes import PolygonPatch
fig, ax = plt.subplots()
# Set up basemap and read in state shapefile (this will draw all state boundaries)
m = Basemap(projection='tmerc')
m.readshapefile("/path/to/your/shapefile", "mexican_states")
# Get maximum number of sick people to calculate shades for states based on relative number
max_sick = np.max(mexican_states_sick_people.values())
# Loop through the states contained in shapefile, attaching a PolygonPatch for each of them with shade corresponding to relative number of sick people
state_patches = []
for coordinates, state in zip(m.mexican_states, m.mexican_states_info):
if state["State_name"] in mexican_states_sick_people.keys():
shade = mexican_states_sick_people[state["State_name"]]/max_sick
state_patches.append(PolygonPatch(Polygon(coordinates), fc = "darkred", ec='#555555', lw=.2, alpha=shade, zorder=4)
# Put PatchCollection of states on the map
ax.add_collection(PatchCollection(state_patches, match_original=True))
如果您有一个州的工作形状文件,并确保您拥有的病人数据集具有每个州的某种标识符(名称或代码),该标识符允许您将数字与shapefile 中状态的标识符(这就是shade = ...
循环中的行依赖于 - 在示例中,我使用 shapefile 中的名称作为键来访问字典中的 vals)。
希望这有帮助,祝你好运!