我想將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)
正如你所看到的,直方圖並不位於柏林,因爲(我認爲)它相對於圖的邊界框而不是軸。我試過像plt.text
那樣添加transform=ax.transAxes
,但是這會給出錯誤BboxTransformTo
。
我應該補充一點,我知道我的位置計算通常不起作用,因爲我沒有使用歐幾里德幾何,但是對於我的目的來說,它足夠接近。
遲來的感謝!我仍然努力爭取這個工作(儘管獲得無花果的Bbox非常有用),並且我意識到我使用一個jupyter筆記本電腦會讓事情變得有點麻煩。筆記本正在調整這個數字(所以'figsize =(8,8)'實際上意味着''8'高,但只有你需要的寬度。「 這樣做的結果是,我從來沒有考慮調整大小所有;當我在IPython中嘗試時,按照您所描述的方式工作,但只能調整大小。 – badgerm