【发布时间】:2017-10-27 07:25:56
【问题描述】:
我正在制作一张世界地图,其中包含单个模型网格框中的饼图。我使用 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 没有任何好处。
欢迎提出任何建议。
【问题讨论】:
标签: python matplotlib z-order cartopy