【问题标题】:Adding pie chart at given coordinates to cartopy projection将给定坐标处的饼图添加到 cartopy 投影
【发布时间】:2017-07-23 15:45:15
【问题描述】:

我是数据可视化的初学者,甚至更喜欢 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())

这不起作用,所以我检查并发现了这个Cartopy coastlines hidden by inset_axes use of Axes.pie,但我不明白幕后发生了什么,而且它似乎仅限于 PlateCarre()。我已尝试对其进行修改,但无法使其正常工作。

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

【问题讨论】:

  • 饼图与地图坐标有什么关系吗?还是您想在与地图相同的区域中显示更多的饼图,例如在this question 中?我想作为这个问题的答案之一的作者,我可能可以在这里提供帮助,但我很难理解你到底想做什么。请注意,使其他答案复杂化的是饼图位于海岸线的原因。如果你想拥有它,它应该会容易得多。
  • 我想通过坐标(经纬度)绘制几个不同的饼图。对于海岸线我不介意。

标签: python-3.x matplotlib pie-chart cartopy


【解决方案1】:

您可以使用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()

【讨论】:

  • 您的代码在所有地方切换了纬度/经度位置。虽然结果是正确的。
  • @swatchai 是的,因为它无处不在,所以没关系。我现在改了,谢谢指出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
相关资源
最近更新 更多