Force aspect ratio for a map

后端 未结 1 645
深忆病人
深忆病人 2020-12-11 06:47

If I define a set of (geo)axes with a given height and width how can I make sure that the plot will fill these axes?

import matplotlib.pyplot as plt
import c         


        
相关标签:
1条回答
  • 2020-12-11 07:10

    Good question. I've had this on my radar for quite some months now (https://github.com/SciTools/cartopy/issues/9).

    I recently learnt about matplotlib's ax.set_adjustable method (here in fact). Using this allows you to tell an axes which has a fixed (data) aspect ratio to fill the space that it can by changing the data limits.

    For example:

    import matplotlib.pyplot as plt
    
    
    ax = plt.axes([0.25, 0.05, 0.5, 0.9])
    ax.set_aspect(1)
    
    ax.plot(range(10))
    ax.set_adjustable('datalim')
    
    plt.show()
    

    Produces a non-square plot (with equal length scales in the x and y dimensions).

    It seems to me that this can be applied to cartopy maps too:

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    
    
    ax = plt.axes([0.25, 0.05, 0.5, 0.9], projection=ccrs.PlateCarree())
    ax.coastlines()
    ax.set_adjustable('datalim')
    
    ax.set_ylim([-90, 90])
    
    plt.show()
    

    I wonder if this suits your needs here?

    0 讨论(0)
提交回复
热议问题