gpt4 book ai didi

python - 通过使用 Axes.pie 的 inset_axes 隐藏的 Cartopy coaSTLines

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:49 27 4
gpt4 key购买 nike

我正在制作一张世界地图,在各个模型网格框中包含饼图。我使用 cartopy 制作 map 和海岸线。我使用 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,因此我将饼图中的色 block 设为半透明。这失败了,因为 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()

enter image description here

关于python - 通过使用 Axes.pie 的 inset_axes 隐藏的 Cartopy coaSTLines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44200931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com