且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Axes.pie的inset_axes隐藏的Cartopy海岸线

更新时间:2023-02-14 12:56:03

问题是每个轴位于另一个轴的上方或下方.因此,在轴内更改艺术家的zorder并没有帮助.原则上,可以设置轴本身的zorder,将插入轴放在主轴后面.

The problem is that each axes either lies on top or below another axes. So changing the zorder of artists within axes, does not help here. In principle, one could set the zorder of the axes themselves, putting the inset axes behind the main axes.

ax_sub.set_zorder(axis_main.get_zorder()-1)

Cartopy的GeoAxes使用其自己的背景补丁.然后需要将其设置为不可见.

Cartopy's GeoAxes uses its own background patch. This would then need to be set to invisble.

ax_main.background_patch.set_visible(False)

完整示例:

import cartopy.crs as ccrs
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

def plot_pie_inset(dataframe_pie,ilat_pie,ilon_pie,axis_main,width_local,alpha_local):
    ax_sub= inset_axes(axis_main, width=width_local, height=width_local, loc=3, 
                       bbox_to_anchor=(ilat_pie, ilon_pie),
                       bbox_transform=axis_main.transAxes, 
                       borderpad=0.0)
    wedges,texts= ax_sub.pie(dataframe_pie,colors=colors_dual)
    for w in wedges:
        w.set_linewidth(0.02)
        w.set_alpha(alpha_local)
        w.set_zorder(1)
    plt.axis('equal')
    # Put insets behind main axes
    ax_sub.set_zorder(axis_main.get_zorder()-1)

colors_dual=['RosyBrown','LightBlue']
lat_list= np.arange(0.2,0.7,0.05)

fig= plt.figure()
ax_main= plt.subplot(1,1,1,projection=ccrs.PlateCarree())
ax_main.coastlines()

# set background patch invisible, such that axes becomes transparent
# since the GeoAxes from cartopy uses a different patch as background
# the following does not work
# ax_main.patch.set_visible(False)
# so we need to set the GeoAxes' background_patch invisible
ax_main.background_patch.set_visible(False)

for ilat in np.arange(len(lat_list)):
    plot_pie_inset([75,25],lat_list[ilat],0.72,ax_main,0.2,0.9)

plt.show()