簡體   English   中英

使用 cartopy 拼湊一個投影

[英]Piece together a projection using cartopy

我試圖在 python 中使用 cartopy 獲得 map 投影,但它不是制作的,所以我試圖使用以下代碼將其拼湊到子圖上:

    fig = plt.figure(figsize =(25,13),facecolor='white')
    gs  = fig.add_gridspec(1,2,width_ratios=[4,2],height_ratios = [1], hspace=0.2,wspace=.0)
    
    ax1=fig.add_subplot(gs[0,0],projection=ccrs.PlateCarree())
    ax2=fig.add_subplot(gs[0,1],projection=ccrs.PlateCarree()) 

    ax2.set_extent([-180,0,-90,90])
    ax1.set_extent([-180,180,-90,90])

    ax1.add_feature(cfeature.LAND, color = 'lightgray')
    ax2.add_feature(cfeature.LAND, color = 'lightgray')

    ax1.add_feature(cfeature.COASTLINE)
    ax2.add_feature(cfeature.COASTLINE)

並且我得到了我正在尋找的正確投影,但是我試圖刪除兩個子圖之間的界限,並且我不斷遇到問題,有什么建議嗎?

輸出圖

您的問題是一個挑戰,因為經度范圍大於 360 度的 plot 和 map 並不常見。 你所做的已經是一個很好的成就。 我會做什么只是為了完成你的工作。

這是生成您需要的 plot 的代碼。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
#from shapely.geometry import Point, Polygon
import cartopy.feature as cfeature
import matplotlib.transforms as transforms
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

fig = plt.figure(figsize =(25,9.5), facecolor='white')
gs  = fig.add_gridspec(1, 2, width_ratios=[4,2], height_ratios = [1], hspace=0.2, wspace=.0)

proj = ccrs.PlateCarree(central_longitude=0)

ax1=fig.add_subplot( gs[0,0], projection=proj )
ax2=fig.add_subplot( gs[0,1], projection=proj ) 

ax1.set_extent([-179.9, 180, -90, 90])  #Tricky, -180 not works!
ax2.set_extent([-179.9, 0, -90, 90])

ax1.add_feature(cfeature.LAND, color = 'lightgray')
ax2.add_feature(cfeature.LAND, color = 'lightgray')

ax1.add_feature(cfeature.COASTLINE)
ax2.add_feature(cfeature.COASTLINE)

# Set color of ax2's boundaries
# If set 'white' the gridline at that position will be gone! 
ax2.outline_patch.set_edgecolor('lightgray')  # set color to match other gridlines 

# Draw 3 sides boundaries of ax2
# ------------------------------
# Define a `transformation`
# Signature: blended_transform_factory(x_transform, y_transform)
# the y coords of this transformation are data (as is = ax.transData)
# but the x coord are axes coordinate (0 to 1, ax.transAxes)
transAD = transforms.blended_transform_factory(ax2.transAxes, ax2.transData)

# Plot 3 lines around extents of ax2 
# Color is intentionally set as 'red'
# You need to change it to 'black' for your work
ax2.plot([0.996, 0.996], [-90, 90], color='red', lw=2, transform=transAD)
ax2.plot([0.0, 0.996], [-90, -90], color='red', lw=2, transform=transAD)
ax2.plot([0.0, 0.996], [89.6, 89.6], color='red', lw=2, transform=transAD)

gl1 = ax1.gridlines(ccrs.PlateCarree(),
                  xlocs=range(-180,181,20),
                  ylocs=range(-90,90,10),
                  linestyle='-',
                  y_inline=False, x_inline=False,
                  color='b', alpha=0.6, linewidth=0.25, draw_labels=True)
gl1.xformatter = LONGITUDE_FORMATTER
gl1.yformatter = LATITUDE_FORMATTER
gl1.right_labels = False

gl2 = ax2.gridlines(ccrs.PlateCarree(),
                  xlocs=range(-180,180,20),
                  ylocs=range(-90,90,10),
                  linestyle='-',
                  y_inline=False, x_inline=False,
                  color='b', alpha=0.6, linewidth=0.25, draw_labels=True)
gl2.xformatter = LONGITUDE_FORMATTER
gl2.yformatter = LATITUDE_FORMATTER
gl2.left_labels = False

長圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM