请勿在画布外面绘制网格线/轮廓线/海岸线

发布于 2025-02-08 04:49:46 字数 3554 浏览 2 评论 0原文

我正在以EPS格式绘制带有 Cartopy 的地图面板。最终的情节看起来不错,但是当我将其插入乳胶文档时, 非常广泛。在使用Adobe Illustrator检查剧情时,似乎绘制了所有网格线/轮廓线/海岸线,甚至是帆布之外的那些,它们被隐藏了,但确实占据了图中的某些空间。

我尝试使用Condectained_layouttigh_layout,但它们与subplots_adjust不兼容,我用来添加共享colorbar。

我用来绘制的代码如下:

proj2 = ccrs.LambertConformal(central_longitude=0, central_latitude=50)
proj_lonlat = ccrs.PlateCarree()
fig = plt.figure(figsize=(12, 9), constrained_layout=True)

# define a function to plot
def plot_era5_500z_MSLp(f500, fsurf, time, label, ax):
    # read data
    for i in np.arange(len(f500.time.values)):
        if pd.to_datetime(f500.time.values[i]) == pd.to_datetime(time):
            print('processing time: ' + time)
            lons = f500.longitude.values         # 1-d array
            lats = f500.latitude.values          # 1-d array
            gph500 = f500.z.values[i,:,:]/98     # geopotential (m2 s-2) -> geopotential height (dagpm) [time = 72, lat = 241, lon = 561]
            pmsl = fsurf.msl.values[i,:,:]/100      # mean sea level pressure    Pa -> hPa
    
    # create base map
    ax.set_extent([-35, 30, 25, 70])   # x0, x1, y0, y1
    gl = ax.gridlines(crs=proj_lonlat, draw_labels=True, xlocs=[-60,-40,-20,0,20,40,60], ylocs=[20,30,40,50,60],
                      x_inline=False, y_inline=False, color='k', alpha=0.5, linestyle='dotted')
    gl.top_labels=False
    gl.right_labels=False
    gl.xlabel_style = {'size': 14, 'color': 'k'}
    gl.ylabel_style = {'size': 14, 'color': 'k'}
    gl.rotate_labels = False
    ax.add_feature(cfeature.COASTLINE.with_scale('50m'), lw=0.4, alpha=0.9)   # add coastline feature
    
    # plot 500hPa geopotential height  (zc: z contour)
    z_levels = np.arange(500, 580+10, 8)
    zc = ax.contour(lons, lats, gph500, transform=proj_lonlat, 
                    levels=z_levels, extent='both', colors='mediumblue', linewidths=0.5)
    ax.clabel(zc, inline=True, fontsize=10, fmt='%.0f')
    
    # plot MSL pressure (mslps: MSL p shading; mslpc: MSL p contour)
    levels = np.arange(960, 1057, 4)
    mslps = ax.contourf(lons, lats, pmsl, levels=levels, cmap='Spectral_r', transform=proj_lonlat)
    mslpc = ax.contour(lons, lats, pmsl, levels=levels, colors='k', linewidths=0.5, alpha=0.6, transform=proj_lonlat)
    
    ax.set_title(label + ' ' + time, loc= 'left', pad=0.5, fontsize=14)
    return mslps
    

# fig (a)
ax1 = plt.subplot(2, 2, 1, projection=proj2)
plot_era5_500z_MSLp(f500_2016nov, fsurf_2016nov, '2016-11-20 12:00', '(a)', ax1)

# fig (b)
ax2 = plt.subplot(2, 2, 2, projection=proj2)
plot_era5_500z_MSLp(f500_2016nov, fsurf_2016nov, '2016-11-24 00:00', '(b)', ax2)

# fig (c)
ax3 = plt.subplot(2, 2, 3, projection=proj2)
plot_era5_500z_MSLp(f500_2017feb, fsurf_2017feb, '2017-02-27 18:00', '(c)', ax3)

# fig (4)
ax4 = plt.subplot(2, 2, 4, projection=proj2)
mslps = plot_era5_500z_MSLp(f500_2017mar, fsurf_2017mar, '2017-03-04 06:00', '(d)', ax4)   # only return mslps here for plotting the sharred colorbar


fig.subplots_adjust(right=0.8, wspace=0.2, hspace=0.000001)
cbar_ax = fig.add_axes([0.82, 0.2, 0.02, 0.55])    # left border, bottom border, width, height
cbar = fig.colorbar(mslps, cax=cbar_ax)
cbar.set_label(label='Mean sea level pressure (hPa)', size=16)
cbar.ax.tick_params(labelsize=14)

生成的EPS绘图看起来不错,但是在Adobe Illustrator中,可以看到画布外的多余线:

”,对于线条延伸超出轴边界的线图

我有什么办法可以限制数据的绘图范围,还是禁用画布外的线?

I'm plotting a map panel with cartopy in eps format. The resulting plot looks fine but has very broad margins when I insert it into my latex document. When checking the plot with adobe illustrator, it seems like the cartopy plots all the gridlines/contourlines/coastlines, even those outside of the canvas, which are hidden but do take up some spaces in the plot.

I tried to use constrained_layout and tight_layout, but they are incompatible with subplots_adjust which I use for adding the shared colorbar.

The code I use to plot is as follows:

proj2 = ccrs.LambertConformal(central_longitude=0, central_latitude=50)
proj_lonlat = ccrs.PlateCarree()
fig = plt.figure(figsize=(12, 9), constrained_layout=True)

# define a function to plot
def plot_era5_500z_MSLp(f500, fsurf, time, label, ax):
    # read data
    for i in np.arange(len(f500.time.values)):
        if pd.to_datetime(f500.time.values[i]) == pd.to_datetime(time):
            print('processing time: ' + time)
            lons = f500.longitude.values         # 1-d array
            lats = f500.latitude.values          # 1-d array
            gph500 = f500.z.values[i,:,:]/98     # geopotential (m2 s-2) -> geopotential height (dagpm) [time = 72, lat = 241, lon = 561]
            pmsl = fsurf.msl.values[i,:,:]/100      # mean sea level pressure    Pa -> hPa
    
    # create base map
    ax.set_extent([-35, 30, 25, 70])   # x0, x1, y0, y1
    gl = ax.gridlines(crs=proj_lonlat, draw_labels=True, xlocs=[-60,-40,-20,0,20,40,60], ylocs=[20,30,40,50,60],
                      x_inline=False, y_inline=False, color='k', alpha=0.5, linestyle='dotted')
    gl.top_labels=False
    gl.right_labels=False
    gl.xlabel_style = {'size': 14, 'color': 'k'}
    gl.ylabel_style = {'size': 14, 'color': 'k'}
    gl.rotate_labels = False
    ax.add_feature(cfeature.COASTLINE.with_scale('50m'), lw=0.4, alpha=0.9)   # add coastline feature
    
    # plot 500hPa geopotential height  (zc: z contour)
    z_levels = np.arange(500, 580+10, 8)
    zc = ax.contour(lons, lats, gph500, transform=proj_lonlat, 
                    levels=z_levels, extent='both', colors='mediumblue', linewidths=0.5)
    ax.clabel(zc, inline=True, fontsize=10, fmt='%.0f')
    
    # plot MSL pressure (mslps: MSL p shading; mslpc: MSL p contour)
    levels = np.arange(960, 1057, 4)
    mslps = ax.contourf(lons, lats, pmsl, levels=levels, cmap='Spectral_r', transform=proj_lonlat)
    mslpc = ax.contour(lons, lats, pmsl, levels=levels, colors='k', linewidths=0.5, alpha=0.6, transform=proj_lonlat)
    
    ax.set_title(label + ' ' + time, loc= 'left', pad=0.5, fontsize=14)
    return mslps
    

# fig (a)
ax1 = plt.subplot(2, 2, 1, projection=proj2)
plot_era5_500z_MSLp(f500_2016nov, fsurf_2016nov, '2016-11-20 12:00', '(a)', ax1)

# fig (b)
ax2 = plt.subplot(2, 2, 2, projection=proj2)
plot_era5_500z_MSLp(f500_2016nov, fsurf_2016nov, '2016-11-24 00:00', '(b)', ax2)

# fig (c)
ax3 = plt.subplot(2, 2, 3, projection=proj2)
plot_era5_500z_MSLp(f500_2017feb, fsurf_2017feb, '2017-02-27 18:00', '(c)', ax3)

# fig (4)
ax4 = plt.subplot(2, 2, 4, projection=proj2)
mslps = plot_era5_500z_MSLp(f500_2017mar, fsurf_2017mar, '2017-03-04 06:00', '(d)', ax4)   # only return mslps here for plotting the sharred colorbar


fig.subplots_adjust(right=0.8, wspace=0.2, hspace=0.000001)
cbar_ax = fig.add_axes([0.82, 0.2, 0.02, 0.55])    # left border, bottom border, width, height
cbar = fig.colorbar(mslps, cax=cbar_ax)
cbar.set_label(label='Mean sea level pressure (hPa)', size=16)
cbar.ax.tick_params(labelsize=14)

The resulting eps plot looks good, but in adobe illustrator, one can see the excess lines outside of the canvas:

for subplots with lines extending beyond axis boundaries

Is there any way I can limit the plotting range of the data, or disable the lines outside of the canvas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文