如何让使用python绘制中国地图并给特定地区上色?

在使用python Basemap绘制地图并给特定地区染色的过程中,有些地区无法上色。请问各位大神有无解决方法?
图片描述


我是准备给14个地区染色,但是能成功上色的只有6个省和直辖市。

请问这是Basemap画地图的bug吗?

在绘制的过程中参考的是
链接描述

大神的教程。

我是准备给14个地区染色,但是能成功上色的只有6个省和直辖市。

请问这是Basemap画地图的bug吗?



代码如下:

plt.figure(figsize=(16,10))

map = Basemap(llcrnrlon=77, llcrnrlat=14, urcrnrlon=140, urcrnrlat=51,projection='lcc',lat_1=33, lat_2=45, lon_0=100)

map.drawcoastlines()

map.readshapefile('CHN_adm1', 'states', drawbounds=True)

#map.drawcountries()





statenames=[]

for shapedict in map.states_info:

    statename = shapedict['NL_NAME_1']

    p = statename.split('|')

    if len(p) > 1:

        s = p[1]

    else:

        s = p[0]

    statenames.append(s)



ax = plt.gca()



seg = map.states[statenames.index('四川')]

seg1 = map.states[statenames.index('湖南')]

seg2 = map.states[statenames.index('江苏')]

seg3 = map.states[statenames.index('上海')]

seg4 = map.states[statenames.index('安徽')]

seg5 = map.states[statenames.index('浙江')]

seg6 = map.states[statenames.index('北京')]

seg7 = map.states[statenames.index('山东')]

seg8 = map.states[statenames.index('江西')]

seg9 = map.states[statenames.index('广西')]

seg10 = map.states[statenames.index('湖北')]





poly = Polygon(seg,facecolor = 'crimson',edgecolor = 'r')

poly1 = Polygon(seg1,facecolor = 'crimson',edgecolor = 'r')

poly2 = Polygon(seg2,facecolor = 'crimson',edgecolor = 'r')

poly3 = Polygon(seg3,facecolor = 'violet',edgecolor = 'r')

poly4 = Polygon(seg4,facecolor = 'violet',edgecolor = 'r')

poly5 = Polygon(seg5,facecolor = 'violet',edgecolor = 'r')

poly6 = Polygon(seg6,facecolor = 'gold',edgecolor = 'r')

poly7 = Polygon(seg7,facecolor = 'gold',edgecolor = 'r')

poly8 = Polygon(seg8,facecolor = 'coral',edgecolor = 'r')

poly9 = Polygon(seg9,facecolor = 'coral',edgecolor = 'r')

poly10 = Polygon(seg10,facecolor = 'coral',edgecolor = 'r')





ax.add_patch(poly)

ax.add_patch(poly1)

ax.add_patch(poly2)

ax.add_patch(poly3)

ax.add_patch(poly4)

ax.add_patch(poly5)

ax.add_patch(poly6)

ax.add_patch(poly7)

ax.add_patch(poly8)

ax.add_patch(poly9)

ax.add_patch(poly10)



plt.show()
阅读 14k
2 个回答
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap

plt.figure(figsize=(16,10))
map = Basemap(llcrnrlon=77, llcrnrlat=14, urcrnrlon=140, urcrnrlat=51,projection='lcc',lat_1=33, lat_2=45, lon_0=100)
map.drawcoastlines()
map.readshapefile('map/CHN_adm1', 'states', drawbounds=True)
ax = plt.gca()

for i, shapedict in enumerate(map.states_info):
    p = shapedict['NL_NAME_1'].split('|')
    s = p[1] if len(p) > 1 else p[0]
    if s == '山东':
        ax.add_patch(Polygon(map.states[i], facecolor = 'gold', edgecolor = 'r'))
    elif s == '浙江':
        ax.add_patch(Polygon(map.states[i], facecolor = 'violet', edgecolor = 'r'))

只画山东和浙江的例子

图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏