繁体   English   中英

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

[英]Cartopy coastlines hidden by inset_axes use of Axes.pie

我正在用各个模型网格框中的饼图制作世界地图。 我使用Cartopy绘制地图和海岸线。 我使用inset_axes生成的饼图。 不幸的是,饼图隐藏了海岸线,我想清楚地看到它们。

最低工作示例:

import cartopy.crs as ccrs
import numpy as np
import cartopy.feature as feature
import matplotlib.pyplot as plt

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.figure.transFigure, 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')

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(zorder=3)
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()

我可以通过减小alpha值使饼图部分透明来查看海岸线。 但是,这会使颜色有些柔和。 我的目标是使海岸线成为最顶层。

我尝试使用“ zorder”将海岸线强制到顶层。 但是,“ zorder”不能传递给inset_axes,也不能传递给ax.pie,因此我使饼图中的色块变得半透明。 之所以失败是因为ax_main.coastlines没有自己的“ zorder”。 海岸线zorder似乎与ax_main相连。 增加ax_main的zorder没有任何好处。

任何建议都非常欢迎。

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

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

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

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()

在此处输入图片说明

一位同事建议的一种替代解决方案是忽略使用inset_axes,但获得了相似的结果。 主要区别在于此解决方案中的坐标系是原始的纬度/经度坐标,而不是图形坐标。

def plot_pie_direct(dataframe_pie,ilat_pie,ilon_pie,axis_main,width_local,alpha_local):
    wedges,texts= ax_main.pie(dataframe_pie,colors=colors_aer_atm,radius=width_local)
    for w in wedges:
        w.set_linewidth(0.02)  ## Reduce linewidth to near-zero
        w.set_center((ilat_pie,ilon_pie))
        w.set_zorder(0)

fig= plt.figure()
ax_main= plt.axes(projection=ccrs.PlateCarree())
ax_main.coastlines(zorder=3)
ax_main.set_global()
lim_x= ax_main.get_xlim()
lim_y= ax_main.get_ylim()
for ilat in np.arange(len(lat_list_trim)):
    plot_pie_direct(frac_aer_atm_reshape_trim[:,ilat,ilon],x_val_pies[ilon],y_val_pies[ilat],ax_main,lat_list_diff_trim,0.9)

ax_main.coastlines(zorder=3)
ax_main.set_xlim(lim_x)
ax_main.set_ylim(lim_y)
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM