簡體   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