IndexError with Basemap.contour() when using certain projections

后端 未结 3 1534
挽巷
挽巷 2020-12-12 03:06

I have run into problems when using Basemap.contour with certain projections. Based on the example given in the Basemap documentation, I created the following w

相关标签:
3条回答
  • 2020-12-12 03:13

    I found a really simple workaround to this problem. Instead of calling Basemap.contour, one can call contour directly on the Axes instance of the Basemap:

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig,ax = plt.subplots()
    m2 = Basemap(projection='kav7',lon_0=0, ax=ax)
    
    x = np.linspace(0, m2.urcrnrx, 100)
    y = np.linspace(0, m2.urcrnry, 100)
    
    xx, yy = np.meshgrid(x, y)
    lon,lat = m2(xx,yy, inverse = True)
    
    data = np.sin(np.pi*lon/180)*np.cos(np.pi*lat/90)
    m2.drawcoastlines(linewidth=0.5)
    
    levels = np.linspace(-1,1,8)
    ##m2.contour(xx, yy, data, levels)
    ax.contour(xx,yy,data,levels)
    plt.show()
    

    This produces the following picture both under Python 2.7 and 3.6:

    0 讨论(0)
  • 2020-12-12 03:20

    This bug has been fixed 2 years ago and does not occur in any basemap version >=1.1.0, independent of the use of python 2 or 3.

    0 讨论(0)
  • 2020-12-12 03:23

    This behavior is according to python3 integer division. Look for examples:

    1) python3:

    n=100
    print (n/2, (n+1)/2)
    

    Output: 50.0 50.5

    2) For python 2.7 this code returns 50 50

    Solutions:

    1) manually update contour and contourf function of basemap with division for python3.

    You have to write for integer n: n//2 which is apply division from python2.

    2) or run your program with python2.

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