matplotlib Cartopy花式盒

9jyewag0  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(133)

你好,我一直在尝试绘制正射投影中的数据。数据被绘制,但我希望框遵循数据限制。像在这个例子中,我共享的形式M_map[在这里输入图像描述

你有什么建议吗?

kwvwclae

kwvwclae1#

在问题下的注解中,有多个指向与问题匹配的现有答案的链接。因此,此问题可能是重复问题。但是,没有一个答案提供如示例图中所示的斑马图案边框。
我借此机会提供一个独特的答案,也绘制了Map边界与斑马图案线类似的样本地块。

import cartopy.crs as ccrs
import cartopy
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as patches

# The lat-long projection
noProj = ccrs.PlateCarree(central_longitude=0)
# The projection of the map:
myProj = ccrs.Orthographic(central_longitude=-25, central_latitude=58)
myProj._threshold = myProj._threshold/40.  #for higher precision plot

fig = plt.figure(figsize=(8,12))
ax = fig.add_subplot(1, 1, 1, projection=myProj)

# Zebra-border-line segments ...
#  four edges on separate lines
# 1: lower edge: Left - Right
# 2: Right edge: Bottom - Top
# 3: Upper edge: Right - Left
# 4: Left edge: Top - Bottom
[ax_hdl] = ax.plot(
    [
        -45, -40, -35, -30, -25, -20, -15, -10, -5,
        -5,-5,-5,-5,-5,
        -10,-15,-20,-25,-30,-35,-40,-45,
        -45, -45, -45, -45, -45
    ],
    [
        45, 45, 45, 45, 45, 45, 45, 45, 45,
        50, 55, 60, 65, 70,
        70,70,70,70,70,70,70,70,
        65, 60, 55, 50, 45 
    ],
    color='black', linewidth=0.5, marker='none',
    transform=noProj)
tx_path = ax_hdl._get_transformed_path()
path_in_data_coords, _ = tx_path.get_transformed_path_and_affine()
polygon1s = mpath.Path( path_in_data_coords.vertices)
vcode = [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1]  #Path-code
polygon1v = mpath.Path( path_in_data_coords.vertices, vcode)

ax.set_boundary(polygon1s) #masks-out unwanted part of the plot

# Zebra-pattern creation
# The pattern line is created from 2 layers
#  lower layer: thicker, black solid line
#  top layer: thinner, dashed white line
patch1s = patches.PathPatch(polygon1s, facecolor='none', ec="black", lw=7, zorder=100)
patch1v = patches.PathPatch(polygon1v, facecolor='none', ec="white", lw=6, zorder=101)
ax.add_patch(patch1s)
ax.add_patch(patch1v)

ax.gridlines(draw_labels=True, x_inline=False, y_inline=False)
ax.add_feature(cartopy.feature.OCEAN, linewidth=.3, color='lightblue')
ax.add_feature(cartopy.feature.LAND, zorder=1, edgecolor='black')
ax.title.set_text("Map with zebra border line")
plt.show()

相关问题