gpt4 book ai didi

python - 为什么 matplotlib basemap 没有绘制 map 中某些区域的颜色?

转载 作者:行者123 更新时间:2023-11-28 17:06:05 25 4
gpt4 key购买 nike

下面的代码应该为越南的所有州着色:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

fig, ax = plt.subplots(figsize=(10,20))

# create the map
map = Basemap(resolution='l', # c, l, i, h, f or None
projection='merc',
lat_0=15.95, lon_0=105.85,
llcrnrlon=102., llcrnrlat= 8.31, urcrnrlon=109.69, urcrnrlat=23.61)



# load the shapefile, use the name 'states'
map.readshapefile(r'path\to\gadm36_VNM_1', name='states', drawbounds=True)
# shapefile downloaded from http://www.gadm.org/



# collect the state names from the shapefile attributes so we can
# look up the shape obect for a state by it's name
state_names = []
for shape_dict in map.states_info:
state_names.append(shape_dict['VARNAME_1'])

ax = plt.gca() # get current axes instance



# NOR, CEN, SOU and MEK are some subdivisions I have created for the states of Vietnam

NOR = ['Lai Chau',
'Lao Cai',
'Ha Giang',
'Cao Bang',
'Dien Bien',
'Son La',
'Yen Bai',
'Tuyen Quang',
'Bac Kan',
'Lang Son',
'Thai Nguyen',
'Phu Tho',
'Vinh Phuc',
'Hoa Binh',
'Ha Noi',
'Bac Ninh',
'Hai Duong',
'Hung Yen',
'Ha Nam',
'Quang Ninh',
'Hai Phong',
'Thai Binh',
'Nam Dinh',
'Bac Giang',
'Ninh Binh']



CEN = ['Thanh Hoa',
'Nghe An',
'Ha Tinh',
'Quang Binh',
'Quang Tri',
'Thua Thien Hue',
'Da Nang']



SOU = ['Quang Nam',
'Kon Tum',
'Quang Ngai',
'Gia Lai',
'Binh Dinh',
'Dak Lak',
'Phu Yen',
'Khanh Hoa',
'Dak Nong',
'Lam Dong',
'Ninh Thuan']




MEK = ['Binh Phuoc',
'Dong Nai',
'Binh Thuan',
'Tay Ninh',
'Binh Duong',
'Dong Nai',
'Ba Ria - Vung Tau',
'Ho Chi Minh',
'Long An',
'An Giang',
'Dong Thap',
'Tien Giang',
'Kien Giang',
'Can Tho',
'Vinh Long',
'Ben Tre',
'Hau Giang',
'Tra Vinh',
'Soc Trang',
'Bac Lieu',
'Ca Mau']



# Define the colours to be used to colour the states

from matplotlib import cm
from numpy import linspace

start = 0.5
stop = 1.0
number_of_lines= 4
cm_subsection = linspace(start, stop, number_of_lines)

cm_subsection[0] = cm_subsection[0]*4
cm_subsection[1] = cm_subsection[1]*0.6
cm_subsection[2] = cm_subsection[2]*0.8
cm_subsection[3] = cm_subsection[3]*0.1

colors = [ cm.Blues(x) for x in cm_subsection ]


for state in NOR:
seg = map.states[state_names.index(state)]
poly = Polygon(seg, facecolor=colors[0], edgecolor=colors[0])
ax.add_patch(poly)

for state in CEN:
seg = map.states[state_names.index(state)]
poly = Polygon(seg, facecolor=colors[1], edgecolor=colors[1])
ax.add_patch(poly)

for state in SOU:
seg = map.states[state_names.index(state)]
poly = Polygon(seg, facecolor=colors[2], edgecolor=colors[2])
ax.add_patch(poly)

for state in MEK:
seg = map.states[state_names.index(state)]
poly = Polygon(seg, facecolor=colors[3], edgecolor=colors[3])
ax.add_patch(poly)





import matplotlib.patches as mpatches

NOR_patch = mpatches.Patch(color=colors[0], label='Rate: 34.85%')
CEN_patch = mpatches.Patch(color=colors[1], label='Rate: 25.61%')
SOU_patch = mpatches.Patch(color=colors[2], label='Rate: 32.66%')
MEK_patch = mpatches.Patch(color=colors[3], label='Rate: 20.02%')
plt.legend(handles=[NOR_patch, CEN_patch, SOU_patch, MEK_patch])
plt.show()

但这会产生下面的 map ,其中一些州没有着色,即使它们出现在州名和分区中:

enter image description here

事实上,如果我尝试为名称不在列表中的州着色,它会抛出一个错误:

MEK.append('ABCDE')

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-619-a89da62a0831> in <module>()
134
135 for state in MEK:
--> 136 seg = map.states[state_names.index(state)]
137 poly = Polygon(seg, facecolor=colors[3], edgecolor=colors[3])
138 ax.add_patch(poly)

ValueError: 'ABCDE' is not in list

很明显,列表中没有着色的状态,因为我没有收到任何错误。那么,这是怎么回事?

编辑:令我吃惊的是,几乎所有非有色州都至少与现实世界中的海洋共享部分边界。 6 个异常在下面以红色突出显示:

enter image description here

这很有趣!它与这个问题有什么关系吗?如果是,什么?为什么?为什么存在这 6 个异常(exception)?

编辑 2: 我在绘制菲律宾 map 时得到类似的结果:

enter image description here

最佳答案

shapefiles 中,一个国家/省份/无论什么,都可以分解成几个线段。为什么会这样,我不知道,但为了正确绘制形状,您需要使用所有必要的线段。实际上,在 Basemap documentation for shapefiles “填充多边形”下有一个示例,说明如何正确执行此操作。我将他们的示例改编为您的用例。它可能不是最佳解决方案,但它似乎有效。

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib import patches as mpatches
from matplotlib import cm
from numpy import linspace
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection

fig, ax = plt.subplots(figsize=(4,8))

# create the map
map = Basemap(resolution='l', # c, l, i, h, f or None
projection='merc',
lat_0=15.95, lon_0=105.85,
llcrnrlon=102., llcrnrlat= 8.31, urcrnrlon=109.69, urcrnrlat=23.61)

# load the shapefile, use the name 'states'
map.readshapefile(r'shapefiles/gadm36_VNM_1', name='states', drawbounds=True)
# shapefile downloaded from http://www.gadm.org/

# collect the state names from the shapefile attributes so we can
# look up the shape obect for a state by it's name
state_names = []
for shape_dict in map.states_info:
state_names.append(shape_dict['VARNAME_1'])

ax = plt.gca() # get current axes instance

# NOR, CEN, SOU and MEK are some subdivisions I have created for the states of Vietnam
NOR = ['Lai Chau',
'Lao Cai',
'Ha Giang',
'Cao Bang',
'Dien Bien',
'Son La',
'Yen Bai',
'Tuyen Quang',
'Bac Kan',
'Lang Son',
'Thai Nguyen',
'Phu Tho',
'Vinh Phuc',
'Hoa Binh',
'Ha Noi',
'Bac Ninh',
'Hai Duong',
'Hung Yen',
'Ha Nam',
'Quang Ninh',
'Hai Phong',
'Thai Binh',
'Nam Dinh',
'Bac Giang',
'Ninh Binh']

CEN = ['Thanh Hoa',
'Nghe An',
'Ha Tinh',
'Quang Binh',
'Quang Tri',
'Thua Thien Hue',
'Da Nang']

SOU = ['Quang Nam',
'Kon Tum',
'Quang Ngai',
'Gia Lai',
'Binh Dinh',
'Dak Lak',
'Phu Yen',
'Khanh Hoa',
'Dak Nong',
'Lam Dong',
'Ninh Thuan']

MEK = ['Binh Phuoc',
'Dong Nai',
'Binh Thuan',
'Tay Ninh',
'Binh Duong',
'Dong Nai',
'Ba Ria - Vung Tau',
'Ho Chi Minh',
'Long An',
'An Giang',
'Dong Thap',
'Tien Giang',
'Kien Giang',
'Can Tho',
'Vinh Long',
'Ben Tre',
'Hau Giang',
'Tra Vinh',
'Soc Trang',
'Bac Lieu',
'Ca Mau']

# Define the colours to be used to colour the states
start = 0.5
stop = 1.0
number_of_lines= 4
cm_subsection = linspace(start, stop, number_of_lines)

cm_subsection[0] = cm_subsection[0]*4
cm_subsection[1] = cm_subsection[1]*0.6
cm_subsection[2] = cm_subsection[2]*0.8
cm_subsection[3] = cm_subsection[3]*0.1

colors = [ cm.Blues(x) for x in cm_subsection ]

##collecting the line segments for the provinces:
patches = {state: [] for state in NOR+CEN+SOU+MEK}
for info, shape in zip(map.states_info, map.states):
for state in NOR+CEN+SOU+MEK:
if info['VARNAME_1'] == state:
patches[state].append(mpatches.Polygon(
shape, True,
))

##coloring the the provinces by group:
for state in NOR:
ax.add_collection(PatchCollection(
patches[state], facecolor = colors[0], edgecolor=colors[0]
))

for state in CEN:
ax.add_collection(PatchCollection(
patches[state], facecolor = colors[1], edgecolor=colors[1]
))

for state in SOU:
ax.add_collection(PatchCollection(
patches[state], facecolor = colors[2], edgecolor=colors[2]
))

for state in MEK:
ax.add_collection(PatchCollection(
patches[state], facecolor = colors[3], edgecolor=colors[3]
))

NOR_patch = mpatches.Patch(color=colors[0], label='Rate: 34.85%')
CEN_patch = mpatches.Patch(color=colors[1], label='Rate: 25.61%')
SOU_patch = mpatches.Patch(color=colors[2], label='Rate: 32.66%')
MEK_patch = mpatches.Patch(color=colors[3], label='Rate: 20.02%')
plt.legend(handles=[NOR_patch, CEN_patch, SOU_patch, MEK_patch])
plt.show()

结果符合预期:

result of the above code

请注意,我只能在 Python 3.6 下测试代码,因此可能需要进行一些调整。希望这会有所帮助。

关于python - 为什么 matplotlib basemap 没有绘制 map 中某些区域的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50928942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com