我绘制了两组重叠的轴,一组是另一组的缩放版本。我想在缩放轴的角和它在较大轴上表示的矩形的角之间画线。但是,我画的线条稍微偏离了位置。我试图把它浓缩成一个简单的例子:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
# Create a large figure:
fig = plt.figure(figsize=(10, 10))
# Add an axes set and draw coastlines:
ax1 = plt.axes([0.01, 0.49, 0.8, 0.5], projection=ccrs.PlateCarree())
ax1.set_global()
ax1.coastlines()
# Add a second axes set (overlaps first) and draw coastlines:
ax2 = plt.axes([0.45, 0.35, 0.4, 0.3], projection=ccrs.PlateCarree())
ax2.set_extent([-44, 45, -15, 45], crs=ccrs.PlateCarree())
ax2.coastlines()
# Draw the rectangular extent of the second plot on the first:
x = [-44, 45, 45, -44, -44]
y = [-15, -15, 45, 45, -15]
ax1.fill(x, y, transform=ccrs.PlateCarree(), color='#0323E4', alpha=0.5)
ax1.plot(x, y, transform=ccrs.PlateCarree(), marker='o')
# Now try and draw a line from the bottom left corner of the second axes set
# to the bottom left corner of the extent rectangle in the first plot:
transFigure = fig.transFigure.inverted()
coord1 = transFigure.transform(ax2.transAxes.transform([0, 0]))
coord2 = transFigure.transform(ax1.transData.transform([-45, -15]))
line = plt.Line2D((coord1[0], coord2[0]), (coord1[1], coord2[1]), transform=fig.transFigure)
fig.lines.append(line)
plt.show()
使用以下输出:
我认为这是因为我在调用时明确定义了轴的形状/方面plt.axes()
,并且此形状与 cartopy 轴的形状不匹配,因为它们是使用旨在使地图看起来正确的纵横比绘制的。我可以在调用中调整轴的形状,以plt.axes()
使纵横比与地图的纵横比相匹配,并且在我期望的位置绘制线条,但这并不容易!有没有办法在坐标变换中解决这个问题?