2017-05-26 59 views
1

我正在使用各個模型網格框中的餅圖生成世界地圖。我用地圖製作地圖和海岸線。我使用inset_axes生成的餅圖。不幸的是,餅圖隱藏了海岸線,我想清楚地看到它們。由inset_axes隱藏的Cartopy海岸線使用Axes.pie

最低工作例如:

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

我可以通過使餅圖通過減小α值部分透明看到海岸線。但是,這使得顏色有些淡化。我的目標是將海岸線作爲最頂層。

我曾嘗試使用'zorder'強制海岸線到頂層。但是,'zorder'不能傳遞給inset_axes,也不能傳遞給ax.pie,所以我已經使餅圖中的色塊變得透明。這會失敗,因爲ax_main.coastlines沒有自己的「zorder」。海岸線zorder似乎與ax_main的綁定。增加ax_main的zorder沒有任何好處。

非常歡迎任何建議。

回答

2

問題是每個軸或者位於頂部或者位於另一個軸的下面。所以改變軸上的藝術家的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() 

enter image description here

+0

非常感謝您的快速和有用的回覆。 – LeightonRegayre

+0

我不清楚如何將背景補丁設置爲不可見,以允許海岸線可見。所使用的set_visible(False)是否適用於GeoAxes的背景和海岸線,或只是背景? 我想在可見和部分透明之間有一些細微的差別,這意味着看不見海岸線的線條。那是對的嗎? 再次感謝! – LeightonRegayre

+1

背景是所有軸背後的白色矩形(因此也位於海岸線之後)。如果我們將插入物放在主軸的後面,這個白色矩形位於插入物的前面並隱藏它們。但是使背景不可見(即殺死白色矩形),可以看到主軸後面的所有內容都可以看到。 – ImportanceOfBeingErnest

1

替代的解決方案建議通過同事忽略使用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()