Python:属性错误

Python:属性错误,python,matplotlib,Python,Matplotlib,各位!!我试图用python制作一个网格,但我总是遇到同样的错误: 这是我正在使用的代码: from __future__ import print_function from matplotlib.widgets import LassoSelector from matplotlib.path import Path import octant from ocean.seaplot import * import matplotlib class SelectFromCollection

各位!!我试图用python制作一个网格,但我总是遇到同样的错误:

这是我正在使用的代码:

from __future__ import print_function
from matplotlib.widgets import LassoSelector
from matplotlib.path import Path
import octant
from ocean.seaplot import *
import matplotlib


class SelectFromCollection(object):
    """Select indices from a matplotlib collection using `LassoSelector`.

    Selected indices are saved in the `ind` attribute. This tool highlights
    selected points by fading them out (i.e., reducing their alpha values).
    If your collection has alpha < 1, this tool will permanently alter them.

    Note that this tool selects collection objects based on their *origins*
    (i.e., `offsets`).

    Parameters
    ----------
    ax : :class:`~matplotlib.axes.Axes`
        Axes to interact with.

    collection : :class:`matplotlib.collections.Collection` subclass
        Collection you want to select from.

    alpha_other : 0 <= float <= 1
        To highlight a selection, this tool sets all selected points to an
        alpha value of 1 and non-selected points to `alpha_other`.
    """

    def __init__(self, ax, collection, alpha_other=0.3):
        self.canvas = ax.figure.canvas
        self.collection = collection
        self.alpha_other = alpha_other

        self.xys = collection.get_offsets()
        self.Npts = len(self.xys)

        # Ensure that we have separate colors for each object
        self.fc = collection.get_facecolors()
        if len(self.fc) == 0:
            raise ValueError('Collection must have a facecolor')
        elif len(self.fc) == 1:
            self.fc = np.tile(self.fc, self.Npts).reshape(self.Npts, -1)

        self.lasso = LassoSelector(ax, onselect=self.onselect)
        self.ind = []

    def onselect(self, verts):
        path = Path(verts)
        self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
        self.fc[:, -1] = self.alpha_other
        self.fc[self.ind, -1] = 1
        self.collection.set_facecolors(self.fc)
        self.canvas.draw_idle()

    def disconnect(self):
        self.lasso.disconnect_events()
        self.fc[:, -1] = 1
        self.collection.set_facecolors(self.fc)
        self.canvas.draw_idle()



def create_grid2():
    #lendo dados de costa
    path = '/home/lof/myroms/Projects/ventos_mare_pcce/'  
    cost = sio.loadmat(os.path.join(path,'ce_coast2.mat'))  

    #definindo delimitações do mapa
    ulon = -35
    dlon = -42  
    ulat = -1
    dlat = -6

    plt.ion()
    #criando o mapa
    fig,m = make_map(llcrnrlon=dlon-3, 
        urcrnrlon=ulon+3,
        llcrnrlat=dlat-3,
        urcrnrlat=ulat+3,
        figsize=(14,10),inloc=4)

    ax = m.ax


    cpts = m.scatter(cost['lon'],cost['lat'],
        latlon=True,s=40)

    selector = SelectFromCollection(ax, cpts)

    plt.draw()
    raw_input('Press any key to accept selected points')
    print("Selected points:")
    print(selector.xys[selector.ind])
    costcoord = selector.xys[selector.ind]
    selector.disconnect()
    plt.close()






    costcoord = np.flipud(costcoord)

    # Block end of script so you can check that the lasso is disconnected.
    raw_input('Press any key to continue')

    betacost = np.zeros(costcoord.shape[0])
    betacost[[0,-1]] = 1


    #criando o mapa
    fig,m = make_map(llcrnrlon=dlon-3, 
            urcrnrlon=ulon+3,
            llcrnrlat=dlat-3,
            urcrnrlat=ulat+3,
            figsize=(14,10),inloc=4)


    m.plot(costcoord[:,0],costcoord[:,1],marker='o')
    raw_input('Adjust the zoom and press any key to continue')
    points = input('how many points do you want?  ')
    print('Do not forget to start from the north (Brazil) ')
    pts = plt.ginput(points)

    pts = np.array(pts)


    betapts = np.zeros(points)
    betapts[[0,-1]] = 1

    beta = np.hstack((betacost,betapts))
    xy = np.vstack((costcoord,pts))

    mask =  beta>0

    #criando o mapa
    fig,m = make_map(llcrnrlon=dlon-3, 
            urcrnrlon=ulon+3,
            llcrnrlat=dlat-3,
            urcrnrlat=ulat+3,
            figsize=(14,10),inloc=4)


    m.plot(xy[:,0],xy[:,1],marker='o',color='g')
    m.plot(xy[:,0][mask],xy[:,1][mask],marker='o',color='k')
    #m.plot(xy[0,0],xy[0,1],marker='o',color='r',alpha=0.5)
    m.plot(pts[:,0],pts[:,1],marker='o',color='r')


    lon,lat = m(xy[:,0],xy[:,1],inverse=True)

    M = input('how many points in X you want?  ')
    N = input('how many points in Y you want?  ')
    shape = (N,M)

    grd = octant.grid.Gridgen(lon,lat,beta,shape)

    m.plot(grd.x,grd.y,'-k',latlon=True,alpha=0.4)
    m.plot(grd.x.T,grd.y.T,'-k',latlon=True,alpha=0.4)

    grid = dict(X=grd.x,Y=grd.y)
    return grid

grid = create_grid2()
我多次尝试重新安装gridgen和octant软件包,但总是出现相同的错误。。。有人能帮我吗?谢谢

from __future__ import print_function
from matplotlib.widgets import LassoSelector
from matplotlib.path import Path
import octant
from ocean.seaplot import *
import matplotlib


class SelectFromCollection(object):
    """Select indices from a matplotlib collection using `LassoSelector`.

    Selected indices are saved in the `ind` attribute. This tool highlights
    selected points by fading them out (i.e., reducing their alpha values).
    If your collection has alpha < 1, this tool will permanently alter them.

    Note that this tool selects collection objects based on their *origins*
    (i.e., `offsets`).

    Parameters
    ----------
    ax : :class:`~matplotlib.axes.Axes`
        Axes to interact with.

    collection : :class:`matplotlib.collections.Collection` subclass
        Collection you want to select from.

    alpha_other : 0 <= float <= 1
        To highlight a selection, this tool sets all selected points to an
        alpha value of 1 and non-selected points to `alpha_other`.
    """

    def __init__(self, ax, collection, alpha_other=0.3):
        self.canvas = ax.figure.canvas
        self.collection = collection
        self.alpha_other = alpha_other

        self.xys = collection.get_offsets()
        self.Npts = len(self.xys)

        # Ensure that we have separate colors for each object
        self.fc = collection.get_facecolors()
        if len(self.fc) == 0:
            raise ValueError('Collection must have a facecolor')
        elif len(self.fc) == 1:
            self.fc = np.tile(self.fc, self.Npts).reshape(self.Npts, -1)

        self.lasso = LassoSelector(ax, onselect=self.onselect)
        self.ind = []

    def onselect(self, verts):
        path = Path(verts)
        self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
        self.fc[:, -1] = self.alpha_other
        self.fc[self.ind, -1] = 1
        self.collection.set_facecolors(self.fc)
        self.canvas.draw_idle()

    def disconnect(self):
        self.lasso.disconnect_events()
        self.fc[:, -1] = 1
        self.collection.set_facecolors(self.fc)
        self.canvas.draw_idle()



def create_grid2():
    #lendo dados de costa
    path = '/home/lof/myroms/Projects/ventos_mare_pcce/'  
    cost = sio.loadmat(os.path.join(path,'ce_coast2.mat'))  

    #definindo delimitações do mapa
    ulon = -35
    dlon = -42  
    ulat = -1
    dlat = -6

    plt.ion()
    #criando o mapa
    fig,m = make_map(llcrnrlon=dlon-3, 
        urcrnrlon=ulon+3,
        llcrnrlat=dlat-3,
        urcrnrlat=ulat+3,
        figsize=(14,10),inloc=4)

    ax = m.ax


    cpts = m.scatter(cost['lon'],cost['lat'],
        latlon=True,s=40)

    selector = SelectFromCollection(ax, cpts)

    plt.draw()
    raw_input('Press any key to accept selected points')
    print("Selected points:")
    print(selector.xys[selector.ind])
    costcoord = selector.xys[selector.ind]
    selector.disconnect()
    plt.close()






    costcoord = np.flipud(costcoord)

    # Block end of script so you can check that the lasso is disconnected.
    raw_input('Press any key to continue')

    betacost = np.zeros(costcoord.shape[0])
    betacost[[0,-1]] = 1


    #criando o mapa
    fig,m = make_map(llcrnrlon=dlon-3, 
            urcrnrlon=ulon+3,
            llcrnrlat=dlat-3,
            urcrnrlat=ulat+3,
            figsize=(14,10),inloc=4)


    m.plot(costcoord[:,0],costcoord[:,1],marker='o')
    raw_input('Adjust the zoom and press any key to continue')
    points = input('how many points do you want?  ')
    print('Do not forget to start from the north (Brazil) ')
    pts = plt.ginput(points)

    pts = np.array(pts)


    betapts = np.zeros(points)
    betapts[[0,-1]] = 1

    beta = np.hstack((betacost,betapts))
    xy = np.vstack((costcoord,pts))

    mask =  beta>0

    #criando o mapa
    fig,m = make_map(llcrnrlon=dlon-3, 
            urcrnrlon=ulon+3,
            llcrnrlat=dlat-3,
            urcrnrlat=ulat+3,
            figsize=(14,10),inloc=4)


    m.plot(xy[:,0],xy[:,1],marker='o',color='g')
    m.plot(xy[:,0][mask],xy[:,1][mask],marker='o',color='k')
    #m.plot(xy[0,0],xy[0,1],marker='o',color='r',alpha=0.5)
    m.plot(pts[:,0],pts[:,1],marker='o',color='r')


    lon,lat = m(xy[:,0],xy[:,1],inverse=True)

    M = input('how many points in X you want?  ')
    N = input('how many points in Y you want?  ')
    shape = (N,M)

    grd = octant.grid.Gridgen(lon,lat,beta,shape)

    m.plot(grd.x,grd.y,'-k',latlon=True,alpha=0.4)
    m.plot(grd.x.T,grd.y.T,'-k',latlon=True,alpha=0.4)

    grid = dict(X=grd.x,Y=grd.y)
    return grid

grid = create_grid2()