将给定坐标处的饼图添加到 cartopy 投影

Adding pie chart at given coordinates to cartopy projection

我是数据可视化的初学者,对于 cartopy 更是如此,我知道对于大多数人来说我的问题是显而易见的。我正在尝试熟悉 cartopy,并且成功地绘制了文本和点。但是我无法实现饼图。

我只想在特定投影上绘制饼图。但我真的很困惑,尽管有 cartopy 的文档。我先试试这个:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines(resolution='110m')  # 110, 50, 10
ax.stock_img() 

lat, long = 30, 30 # the latitude longitude
ax.pie(long, lat, [0.25, 0.75], transform=ccrs.PlateCarree())

那行不通,所以我检查了一下,发现了这个 但我不明白引擎盖下发生了什么,而且它似乎仅限于 PlateCarre()。我尝试修改它,但我没能让它正常工作。

所以我的一个非常简单的问题是如何在给定经纬度的特定投影中添加多个饼图?如果你能提出你的答案,你将非常受欢迎。

您可以使用 inset_axes 将新坐标轴放入图中,这将允许托管饼图。 inset_axes 的位置由 bbox_to_anchor 参数决定。要让此参数使用 cartopy 轴 (ax) 的投影坐标,您需要设置 bbox_transform=ax.transData。 如果您的坐标在不同的坐标系中,则需要先使用投影的 .transform_point 方法将它们转换为正在使用的坐标。

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines(resolution='110m')
ax.stock_img() 


def plot_pie_inset(data,ilon,ilat,ax,width):
    ax_sub= inset_axes(ax, width=width, height=width, loc=10, 
                       bbox_to_anchor=(ilon, ilat),
                       bbox_transform=ax.transData, 
                       borderpad=0)
    wedges,texts= ax_sub.pie(data)

    ax_sub.set_aspect("equal")

lon,lat = 90,30
lonr,latr =  ccrs.Robinson().transform_point(lon,lat, ccrs.PlateCarree())
plot_pie_inset([0.25, 0.75],lonr,latr,ax,0.5)


plt.show()