2017-01-18 13 views
1

我想將matplotlib座標軸放置在cartopy圖上的特定座標處,但不知道如何正確設置位置。該代碼應:放置副圖在lat/long座標在cartopy

  • 情節德國的正投影
  • 添加文本「柏林」在柏林的位置
  • 在柏林
  • 的位置

我的代碼添加直方圖如下:

import numpy as np 
import matplotlib.pyplot as plt 
import cartopy.crs as ccrs 
import cartopy.feature as cfeature 

plt.figure(figsize=(8, 8)) 

extent = [6, 15, 47, 55]   # Extent of Germany in Lat/Long 

lat, lon = 52.520007, 13.404954 # Location of Berlin 

# Plot our coastline and set the extent 
ax = plt.axes(projection=ccrs.Orthographic(central_longitude=10.5, \ 
     central_latitude=51.0, \ 
     globe=None)) 
ax.coastlines('10m') 
ax.set_extent(extent) 

# Add text at the location of Berlin 
plt.text(lon, lat, 'Berlin', \ 
     verticalalignment='center', \ 
     horizontalalignment='right', \ 
     transform=ccrs.PlateCarree()) 

# Add subplot 
sub_ax = plt.axes([(lon-extent[0])/(extent[1] - extent[0]), \ 
        (lat-extent[2])/(extent[3] - extent[2]), \ 
        .1, .1], \ 
        axisbg='none') 

plt.hist(np.random.randn(100), 10, normed=1) 

Code output

正如你所看到的,直方圖並不位於柏林,因爲(我認爲)它相對於圖的邊界框而不是軸。我試過像plt.text那樣添加transform=ax.transAxes,但是這會給出錯誤BboxTransformTo

我應該補充一點,我知道我的位置計算通常不起作用,因爲我沒有使用歐幾里德幾何,但是對於我的目的來說,它足夠接近。

回答

2

下面的代碼應該得到相對於圖的邊緣所需位置:

ax_lon = (lon-extent[0])/(extent[1] - extent[0]) 
ax_lat = (lat-extent[2])/(extent[3] - extent[2]) 
fig_lon = (ax.bbox.x0 + ax_lon * ax.bbox.width)/fig.bbox.width 
fig_lat = (ax.bbox.y0 + ax_lat * ax.bbox.height)/fig.bbox.height 

但如果這個數字被調整,這些值將成爲錯誤的。調整大小時,我可以考慮將其保持在正確位置的唯一方法是使用事件回調來更新位置。

+1

遲來的感謝!我仍然努力爭取這個工作(儘管獲得無花果的Bbox非常有用),並且我意識到我使用一個jupyter筆記本電腦會讓事情變得有點麻煩。筆記本正在調整這個數字(所以'figsize =(8,8)'實際上意味着''8'高,但只有你需要的寬度。「 這樣做的結果是,我從來沒有考慮調整大小所有;當我在IPython中嘗試時,按照您所描述的方式工作,但只能調整大小。 – badgerm

1

這裏是我的嘗試:

import numpy as np 
import matplotlib.pyplot as plt 
import cartopy.crs as ccrs 
#import cartopy.feature as cfeature 
from mpl_toolkits.axes_grid1.inset_locator import inset_axes 

height = 18. 
width = 8. 
plt.figure(figsize = (width, height)) 

extent = [6, 15, 47, 55] # Extent of Germany [Long1,2, Lat1,2] 

ortho = ccrs.Orthographic(central_longitude=10.5, \ 
     central_latitude=51.0, \ 
     globe=None) 

ax = plt.axes(projection=ortho) 
ax.coastlines('50m') 
ax.set_extent(extent) 

def plot_city_name(lon, lat, cityname): 
    """plot city name at specified location""" 
    plt.text(lon, lat, cityname, \ 
      va='center', \ 
      fontsize=18, \ 
      ha='right', \ 
      transform=ccrs.PlateCarree(), \ 
      zorder=0) 

lat, lon = 52.520007, 13.404954 # Location of Berlin 
plot_city_name(lon, lat, 'Berlin') 

latd, lond = 51.341734, 7.417392 # Location of Hagen 
plot_city_name(lond, latd, 'Hagen') 

def plot_hist(mapx, mapy, ax, width): 
    """plot simple histogram at specified location""" 
    ax_sub= inset_axes(ax, width=width, \ 
         height=width, loc=3, \ 
         bbox_to_anchor=(mapx, mapy), \ 
         bbox_transform=ax.transData, \ 
         borderpad=0) 
    # plot simple histogram 
    ax_sub.hist(np.random.randn(100), bins=10, normed=1) 

def xy_from_longlat(lon, lat): 
    """convert long-lat (degrees) to grid values""" 
    return ortho.transform_point(lon, lat, ccrs.PlateCarree()) 

# plot histograms at 2 locations 
inset_size = 1.0 # unit? 

mapx, mapy = xy_from_longlat(lon, lat) 
plot_hist(mapx, mapy, ax, inset_size) # Berlin 

mapx, mapy = xy_from_longlat(lond, latd) 
plot_hist(mapx, mapy, ax, inset_size) # Hagen 

plt.show() 

輸出:

enter image description here

編輯

在IPython中/ jupyter筆記本電腦,你可以平移和縮放圖形沒有打破直方圖的相對位置。