Vit*_*ata 8 python numpy dataframe pandas cartopy
使用 python3 和 cartopy,有这个代码:
import matplotlib.pyplot as plt
import cartopy
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
ax.add_feature(cartopy.feature.LAKES, alpha=0.95)
ax.add_feature(cartopy.feature.RIVERS)
ax.set_extent([-150, 60, -25, 60])
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
reader = shpreader.Reader(shpfilename)
countries = reader.records()
for country in countries:
if country.attributes['SOVEREIGNT'] == "Bulgaria":
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0, 1, 0), label = "A")
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(1, 1, 1), label = country.attributes['SOVEREIGNT'])
plt.rcParams["figure.figsize"] = (50,50)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我明白了:
问题:
我应该写什么,以便在保加利亚(或我在 中提到的任何其他国家/地区)上空得到一个红色的“ A ” country.attributes['SOVEREIGNT']?目前根本没有显示标签,我不确定如何更改标签的字体。因此,以下内容似乎只更改了颜色,而没有添加标签:
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0, 1, 0), label = "A")
Run Code Online (Sandbox Code Playgroud)
您可以检索几何的质心并在该位置绘制文本:
import matplotlib.patheffects as PathEffects
for country in countries:
if country.attributes['SOVEREIGNT'] == "Bulgaria":
g = ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0, 1, 0), label="A")
x = country.geometry.centroid.x
y = country.geometry.centroid.y
ax.text(x, y, 'A', color='red', size=15, ha='center', va='center', transform=ccrs.PlateCarree(),
path_effects=[PathEffects.withStroke(linewidth=5, foreground="k", alpha=.8)])
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(1, 1, 1), label = country.attributes['SOVEREIGNT'])
Run Code Online (Sandbox Code Playgroud)
随着范围集中在“保加利亚”,它看起来像:
编辑:
要分离“依赖项”,请考虑使用admin_0_map_units代替admin_0_map_countries,请参阅Natural Earth 文档
。
要突出显示小国家/地区,您可以使用以下内容向几何图形添加缓冲区:
highlight = ['Singapore', 'Liechtenstein']
for country in countries:
if country.attributes['NAME'] in highlight:
if country.geometry.area < 2:
geom = [country.geometry.buffer(2)]
else:
geom = [country.geometry]
g = ax.add_geometries(geom, ccrs.PlateCarree(), facecolor=(0, 0.5, 0, 0.6), label="A", zorder=99)
x = country.geometry.centroid.x
y = country.geometry.centroid.y
ax.text(x, y+5, country.attributes['NAME'], color='red', size=14, ha='center', va='center', transform=ccrs.PlateCarree(),
path_effects=[PathEffects.withStroke(linewidth=3, foreground="k", alpha=.8)])
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(1, 1, 1), label=country.attributes['NAME'])
Run Code Online (Sandbox Code Playgroud)
你可以用这样的东西分割一个特定的国家,它使用 Shapely 在几何体的中间执行一个交集。最终,将绘图和空间分析(拆分等)分成更不同的步骤可能会“更清晰”。像这样混合可能会使代码更难在其他情况下重用。
from shapely.geometry import LineString, MultiLineString
for country in countries:
if country.attributes['NAME'] in 'China':
# line at the centroid y-coord of the country
l = LineString([(-180, country.geometry.centroid.y),
(180, country.geometry.centroid.y)])
north_poly = MultiLineString([l, north_line]).convex_hull
south_poly = MultiLineString([l, south_line]).convex_hull
g = ax.add_geometries([country.geometry.intersection(north_poly)], ccrs.PlateCarree(), facecolor=(0.8, 0.0, 0.0, 0.4), zorder=99)
g = ax.add_geometries([country.geometry.intersection(south_poly)], ccrs.PlateCarree(), facecolor=(0.0, 0.0, 0.8, 0.4), zorder=99)
x = country.geometry.centroid.x
y = country.geometry.centroid.y
ax.text(x, y, country.attributes['NAME'], color='k', size=16, ha='center', va='center', transform=ccrs.PlateCarree(),
path_effects=[PathEffects.withStroke(linewidth=5, foreground="w", alpha=1)], zorder=100)
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(1, 1, 1), label=country.attributes['NAME'])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1868 次 |
| 最近记录: |