我正试图使用Cartopy创建Map概览,但由于某种原因无法弄清楚如何绘制Map上显示的国家名称。
我的函数看起来像这样:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
def plot_southwest_indian_ocean():
# Define the latitude and longitude ranges for the Southwest Indian Ocean basin
lat_range = [-40, 0]
lon_range = [-340, -260]
# Create a figure and axis object with a desired projection
fig, ax = plt.subplots(figsize=(8, 6), subplot_kw={'projection': ccrs.PlateCarree()})
# Set the map boundaries
ax.set_extent(lon_range + lat_range, crs=ccrs.PlateCarree())
# Add land and ocean features
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.OCEAN)
# Add country borders
ax.add_feature(cfeature.BORDERS, linestyle='-', alpha=0.5)
# Add country labels
countries = cfeature.NaturalEarthFeature(
category='cultural',
name='admin_0_countries',
scale='50m',
facecolor='none',
edgecolor='gray'
)
ax.add_feature(countries, linewidth=0.5)
country_name = 'Mozambique'
country_boundaries = [country for country in countries.geometries() if country.properties['ADMIN'] == country_name]
for boundary in country_boundaries:
centroid = boundary.centroid
ax.annotate(
country_name,
xy=(centroid.x, centroid.y),
xytext=(5, 5),
textcoords='offset points',
ha='left',
va='bottom',
fontsize=8,
color='black',
bbox=dict(boxstyle='round,pad=0.2', fc='white', ec='gray', lw=0.5)
)
# Add gridlines
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='gray', alpha=0.5, linestyle='--')
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 12, 'color': 'gray'}
gl.ylabel_style = {'size': 12, 'color': 'gray'}
# Add a title to the plot
ax.set_title("Southwest Indian Ocean Basin")
# Show the plot
plt.show()
我试图在莫桑比克测试它,但继续遇到错误,如果我试图绘制一个国家或所有国家的标签。理想情况下,将绘制该Map范围内的所有内容。这是Map范围,没有任何国家/地区标签:
def plot_southwest_indian_ocean():
# Define the latitude and longitude ranges for the Southwest Indian Ocean basin
lat_range = [-40, 0]
lon_range = [-340, -260]
# Create a figure and axis object with a desired projection
fig, ax = plt.subplots(figsize=(8, 6), subplot_kw={'projection': ccrs.PlateCarree()})
# Set the map boundaries
ax.set_extent(lon_range + lat_range, crs=ccrs.PlateCarree())
# Add land and ocean features
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.OCEAN)
# Add country borders
ax.add_feature(cfeature.BORDERS, linestyle='-', alpha=0.5)
# Add gridlines
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='gray', alpha=0.5, linestyle='--')
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 12, 'color': 'gray'}
gl.ylabel_style = {'size': 12, 'color': 'gray'}
# Add a title to the plot
ax.set_title("Southwest Indian Ocean Basin")
# Show the plot
plt.show()
1条答案
按热度按时间u59ebvdq1#
带有
if country.properties['ADMIN'] == country_name
的行返回错误,因为country
没有properties
属性。从How to put a label on a country with Python cartopy?开始,我设法生成了这段代码,它应该可以做你想要的事情: