作为python绘制地图的备忘录
python调用在线地图api教程
思路:
.stock_img()
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
fig = plt.figure()
proj = ccrs.PlateCarree(central_longitude=0)
extents = [65, 110, 25, 45]
res = '10m'
ax = fig.add_subplot(111, projection=proj)
ax.set_extent(extents, crs=proj)
# 添加地形图
ax.stock_img()
# 添加河湖
ax.add_feature(cfeature.LAKES.with_scale(res), edgecolor=None,color='cyan')
ax.add_feature(cfeature.RIVERS.with_scale(res),lw=1)
for ea in tp.iloc[[0,1,3,5,7,8,9]]['geometry']:
feat = cartopy.feature.ShapelyFeature([ea], proj, facecolor='chartreuse', edgecolor='black', lw=0.2,alpha=0.4)
ax.add_feature(feat)
path = '/group1/longjs/TP_shape/Union/'
tp = gpd.read_file(path+'TP_basins.shp') # 本地shape文件,为流域边界
# 添加经纬度
ax.set_xticks(np.arange(65, 110, 10), crs=proj)
ax.set_yticks(np.arange(25, 45, 2.5), crs=proj)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
plt.show();
思路:
import matplotlib.pyplot as plt
import geopandas as gpd
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.io.img_tiles as cimgt
# 定义画布
fig = plt.figure()
proj = ccrs.PlateCarree(central_longitude=0)
extents = [65, 110, 25, 45]
ax = fig.add_subplot(111, projection=proj)
ax.set_extent(extents, crs=proj)
# 调用地图
ax.add_image(cimgt.MAP_ter_w(),5) # 数字为地图分层
path = '/group1/longjs/TP_shape/Union/'
tp = gpd.read_file(path+'TP_basins.shp') # 本地shape文件,为流域边界
# 依次绘制不同流域
for ea in tp.iloc[[0,1,3,5,7,8,9]]['geometry']:
feat = cartopy.feature.ShapelyFeature([ea], proj, facecolor="lime", edgecolor='black', lw=0.2,alpha=0.4)
ax.add_feature(feat)
# 添加细节
lon_lat = ax.gridlines(draw_labels=True, linewidth=1, color='k', alpha=0.2, linestyle='--')
lon_lat.xlabels_top = False
lon_lat.ylabels_right = False
lon_lat.xformatter = LONGITUDE_FORMATTER
lon_lat.yformatter = LATITUDE_FORMATTER
plt.show();
思路:
import cartopy
import cartopy.crs as ccrs
from cartopy.feature import ShapelyFeature
from matplotlib import pyplot as plt
import geopandas as gpd
import cartopy.io.img_tiles as cimgt
# 定义画布
fig = plt.figure()
proj = ccrs.PlateCarree(central_longitude=0)
extents = [-30, 60, 30, 80]
ax = fig.add_subplot(111, projection=proj)
ax.set_extent(extents, crs=proj)
# 添加底图
ax.stock_img()
# 添加数据
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = world[(world['continent'] == 'Europe')]
# 依次画出欧洲不同国家边界,并给予不同颜色。需要注意,中国的边界不完整!
for ea in europe['geometry']:
color = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])]
feat = cartopy.feature.ShapelyFeature([ea], proj, facecolor=color,edgecolor='black', lw=0.2,alpha=0.5)
ax.add_feature(feat)
# ax.add_image(cimgt.MAP_cva_w(),6)
# 添加海岸线
# ax.add_feature(cartopy.feature.COASTLINE)
plt.show();