首页 > 解决方案 > 如何用Python在美国地图中用颜色标记州名和年份?

问题描述

我试着画关于美国州地图的图片。但是,我不知道如何编写正确的代码来做到这一点。这是我使用的示例之一。但我需要解决这个问题。

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon

# create the map
map = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
        projection='lcc',lat_1=33,lat_2=45,lon_0=-95)

# load the shapefile, use the name 'states'
map.readshapefile('st99_d00', name='states', drawbounds=True)

# 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['NAME'])

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

# get Texas and draw the filled polygon
seg = map.states[state_names.index('Texas')]
poly = Polygon(seg, facecolor='red',edgecolor='red')
ax.add_patch(poly)

plt.show()

这是我想要的基本示例。

在此处输入图像描述

加上这个,我想输入州名。此外,是否可以在这张美国地图上仅缩放密歇根州、印第安纳州、俄亥俄州和伊利诺伊州?

标签: pythonmatplotlibmatplotlib-basemap

解决方案


推荐阅读