简体   繁体   English

使用 cartopy 拼凑一个投影

[英]Piece together a projection using cartopy

I was trying to get a map projection using cartopy in python and it wasn't made so I'm trying to piece it together using to subplots with the following code:我试图在 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)

and I get the right projection I was looking for, however I am trying to remove the line between the two subplots and I keep getting issues, any suggestions?并且我得到了我正在寻找的正确投影,但是我试图删除两个子图之间的界限,并且我不断遇到问题,有什么建议吗?

输出图

Your question is a challenge as it is uncommon to plot a map with longitude extent greater than 360 degrees.您的问题是一个挑战,因为经度范围大于 360 度的 plot 和 map 并不常见。 What you have done is already a good achievement.你所做的已经是一个很好的成就。 What I will do just to finish your work.我会做什么只是为了完成你的工作。

Here is the code that produces the plot you need.这是生成您需要的 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