简体   繁体   English

在Cartopy中绘制直线,Robinson投影

[英]Plotting a straight line in Cartopy, Robinson projection

I'm playing around with cartopy trying to understand how it works. 我在玩Cartopy,试图了解它的工作原理。 The first thing I tried was very similar to the example in the docs under the 'Adding Data to the Map' section. 我尝试的第一件事与“将数据添加到地图”部分下的文档中的示例非常相似。

I'm trying to draw a straight line from Adelaide, Australia to Liverpool, UK on my Robinson projection. 我正试图用我的鲁滨逊投影画一条从澳大利亚阿德莱德到英国利物浦的直线。 This is my code: 这是我的代码:

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

ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines()
ax.stock_img()

ad_lat, ad_lon = -34.93, 138.60
liv_lat, liv_lon = 53.41, -2.99

plt.plot([ad_lon, liv_lon], [ad_lat, liv_lat],
          color='blue', linewidth=1, marker='o', markersize=3,
          )

plt.show()

However this produces a single marker in the middle of the map. 但是,这会在地图中间产生一个标记。 The docs say that by default, if you don't specify a transform property to plt.plot, it uses the one for the axes (Robinson in this case). 文档说,默认情况下,如果不为plt.plot指定一个transform属性,它将使用一个作为轴(在这种情况下为Robinson)。 When I explicitly add 'transform=ccrs.Robinson()' the same thing happens. 当我显式添加'transform = ccrs.Robinson()'时,会发生同样的事情。 However, it does let me use 'ccrs.Geodetic()' for a curved line, or ccrs.PlateCarree() for a slightly wonky straight line. 但是,它的确使我使用“ ccrs.Geodetic()”作为一条曲线,或者使用ccrs.PlateCarree()作为一条稍微弯曲的直线。

I can't find anything in the docs about the transform property being limited to some projections but not others, so I don't understand why this is happening. 我在文档中找不到有关transform属性的任何信息,但仅限于某些投影,而没有其他投影,因此我不明白为什么会发生这种情况。 Can anyone shed some light on this? 谁能对此有所启发?

The transform argument is tied to the data you are plotting. transform参数与您要绘制的数据相关。 You are specifying the start and end points of the line in lat/lon, and therefore the only sensible transforms are Geodetic and PlateCarree. 您正在以经度/纬度指定线的起点和终点,因此,唯一明智的转换是Geodetic和PlateCarree。

When you set transform=ccrs.Robinson() Cartopy is assuming the coordinates you provided are already in the Robinson projection, which they are not. 当您设置transform=ccrs.Robinson() Cartopy假设您提供的坐标已经在Robinson投影中,而实际上不在。 The Robinson projection extents in native coordinates are very large comapared to geographic coordinates (order ~1e7 perhaps) so both the points you selected are very near the centre of the projection, which is why you just see a dot. 与地理坐标相比,本机坐标中的Robinson投影范围非常大(也许约为1e7),因此您选择的两个点都非常靠近投影的中心,这就是为什么只看到一个点的原因。

If you want the line to be straight when drawn on the Robinson projection you will have to plot it in projection coordinates. 如果希望在Robinson投影上绘制直线时,该直线必须在投影坐标中绘制。 This is straightforward using Cartopy's transforms system as in the modified example below. 使用Cartopy的转换系统,这很简单,如下面的修改示例所示。 For more guidance on how Cartopy's transform/projection keywords work see https://scitools.org.uk/cartopy/docs/v0.16/tutorials/understanding_transform.html . 有关Cartopy的transform / projection关键字如何工作的更多指南,请参见https://scitools.org.uk/cartopy/docs/v0.16/tutorials/understanding_transform.html

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

ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines()
ax.stock_img()

ad_lat, ad_lon = -34.93, 138.60
liv_lat, liv_lon = 53.41, -2.99

# New bit: transform the given lat/lon points into the Robinson projection
geodetic = ccrs.Geodetic()
robinson = ccrs.Robinson()
ad_lon_t, ad_lat_t = robinson.transform_point(ad_lon, ad_lat, geodetic)
liv_lon_t, liv_lat_t = robinson.transform_point(liv_lon, liv_lat, geodetic)

plt.plot([ad_lon_t, liv_lon_t], [ad_lat_t, liv_lat_t],
         color='blue', linewidth=1, marker='o', markersize=3,
         # Be explicit about which transform you want:
         transform=robinson)

plt.show()

罗宾逊投影中的直线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM