与地图共享子图之间的x和y轴

时间:2018-11-29 01:03:50

标签: python matplotlib matplotlib-basemap cartopy

我想使用sharexsharey在两个子图中同步zoomingclicking

这是一个简单的代码:

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

proj = ccrs.LambertConformal(central_latitude=39,central_longitude=-95,
                            standard_parallels = (39, 39))

fig = plt.figure(figsize=(12,12))

ax1 = plt.subplot(2,2,1,projection = proj)
ax2 = plt.subplot(2,2,2,projection = proj,sharex=ax1, sharey=ax1)

ax1.coastlines()
ax2.coastlines()
ax1.add_feature(cfeature.STATES)
ax2.add_feature(cfeature.STATES)

ax1.set_extent([-100, -80, 20, 35], crs=ccrs.PlateCarree())
ax2.set_extent([-100, -80, 20, 35], crs=ccrs.PlateCarree())

lon, lat = -85, 25

ax1.scatter(lon,lat,transform=ccrs.PlateCarree())
ax2.scatter(lon,lat,transform=ccrs.PlateCarree())

plt.show()

但是,地图范围不同: original 通过moving工具稍作移动后,就一样了!  move

这是另一个例子:

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


proj = ccrs.PlateCarree()

ax1 = plt.subplot(1,4,1,projection = proj)
ax2 = plt.subplot(1,4,2,projection = proj,sharex=ax1, sharey=ax1)
ax3 = plt.subplot(1,4,3,projection = proj,sharex=ax1, sharey=ax1)
ax4 = plt.subplot(1,4,4,projection = proj,sharex=ax1, sharey=ax1)


for ax in [ax1,ax2,ax3,ax4]:
    ax.coastlines()
    gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, color='gray', alpha=0.5, linestyle='--')
    ax.set_extent([-105, -75, 25, 50], crs=ccrs.PlateCarree())

plt.show()

而且,结果看起来很奇怪:

example_2

1 个答案:

答案 0 :(得分:0)

它与matplotlib的版本有关。 更新为3.0.2后,现在可以正常使用了。