在Python 3.2中,为Matplotlib的basemap使用不同的颜色填充每个邮政编码

在Python 3.2中,为Matplotlib的basemap使用不同的颜色填充每个邮政编码,python,matplotlib,shapefile,python-3.2,matplotlib-basemap,Python,Matplotlib,Shapefile,Python 3.2,Matplotlib Basemap,我正在Python 3.2中为Matplotlib的底图中的邮政编码多边形着色 我需要用不同的颜色填写每个邮政编码 邮政编码信息来自shapefile 我无法在以下位置找到解决方案: 任何帮助都将不胜感激 谢谢有一种在shapefile中阅读的非常方便的方法 m = Basemap() m.readshapefile('file_without_extension', 'name') 然后,您可以使用m.name和m.name\u info访问shapefile上的信息 然后创建要用于颜色信

我正在Python 3.2中为Matplotlib的底图中的邮政编码多边形着色

我需要用不同的颜色填写每个邮政编码

邮政编码信息来自shapefile

我无法在以下位置找到解决方案:

任何帮助都将不胜感激

谢谢

有一种在shapefile中阅读的非常方便的方法

m = Basemap()
m.readshapefile('file_without_extension', 'name')
然后,您可以使用
m.name
m.name\u info
访问shapefile上的信息

然后创建要用于颜色信息的数据框

import pandas as pd
import numpy as np
from matplotlib.patches import Polygon

zipdf = pd.DataFrame({
    'shapes': [Polygon(np.array(shape), True) for shape in m.name],
    'zip': [area['zip'] for area in m.name_info]
})
如果要包含shapefile中未包含的着色信息,请将其他信息与刚才创建的数据框合并

zipdf = zipdf.merge(other_df, how='right', on='zip')
现在,为了给地图着色,我使用了一个colormap,它在zipcode中获取租金的值,所以我将展示它

from matplotlib.collections import PatchCollection
import matplotlib.cm as cm
import matplotlib.colors as colors

fig = plt.figure()
ax = fig.add_subplot(111)

cmap = plt.get_cmap('viridis')
pc = PatchCollection(zipdf['shapes'], zorder=2)
norm = colors.Normalize()

pc.set_facecolor(cmap(norm(zipdf['price'].values)))
ax.add_collection(pc)

cmapper = cm.ScalarMappable(norm=norm, cmap=cmap)
cmapper.set_array(zipdf['price'])
plt.colorbar(cmapper)

plt.show()
要了解更多信息,请查看


如果我遗漏了什么,请告诉我

请张贴用于绘制地图的代码。可能重复的