AttributeError:'Figure'对象没有属性'set_axes'

时间:2018-10-27 13:34:34

标签: python-3.x matplotlib deprecated

我正在使用python-3.7.0 matplotlib-3.0.0。我正在尝试使用以下代码绘制分类器(在线获取):

def plotLinearClassifier(h, X, Y):
    """
    Draw the current decision boundary, margin and data
    """
    plt.plot(X[Y>=0.5,0], X[Y>=0.5,1], 'b+',
         X[Y< 0.5,0], X[Y< 0.5,1], 'ro')
    axes = plt.figure(1).get_axes()[0]
    xlim = axes.get_xlim()
    ylim = axes.get_ylim()

    xmin = xlim[0] + (xlim[1] - xlim[0]) / 100
    xmax = xlim[1] - (xlim[1] - xlim[0]) / 100
    ymin = ylim[0] + (ylim[1] - ylim[0]) / 100
    ymax = ylim[1] - (ylim[1] - ylim[0]) / 100

    if type(h.weights) == np.ndarray:
        #b = h.bias
        b = 0
        w = h.weights

        #print b
        #print w

        # find the zeros along each axis
        # w0*l + w1*? + b = 0  ==>  ? = -(b + w0*l) / w1
        xmin_zero = - (b + w[0] * xmin) / w[1]
        xmax_zero = - (b + w[0] * xmax) / w[1]
        ymin_zero = - (b + w[1] * ymin) / w[0]
        ymax_zero = - (b + w[1] * ymax) / w[0]

        #print (ylim, xlim, (xmin_zero, xmax_zero), (ymin_zero, ymax_zero))

        # now, two of these should actually be in bounds, figure out which
        inBounds = []
        if ylim[0] <= xmin_zero and xmin_zero <= ylim[1]:
            inBounds.append( (xmin, xmin_zero) )
        if ylim[0] <= xmax_zero and xmax_zero <= ylim[1]:
            inBounds.append( (xmax, xmax_zero) )
        if xlim[0] <= ymin_zero and ymin_zero <= xlim[1]:
            inBounds.append( (ymin_zero, ymin) )
        if xlim[0] <= ymax_zero and ymax_zero <= xlim[1]:
            inBounds.append( (ymax_zero, ymax) )

        #print inBounds

        if len(inBounds) >= 2:
            plt.plot(X[Y>=0.5,0], X[Y>=0.5,1], 'b+',
                 X[Y< 0.5,0], X[Y< 0.5,1], 'ro',
                 [inBounds[0][0], inBounds[1][0]], [inBounds[0][1], inBounds[1][1]], 'k-')
            plt.figure(1).set_axes([axes])
            plt.legend(('positive', 'negative', 'hyperplane'))
        else:
            plt.plot(X[Y>=0.5,0], X[Y>=0.5,1], 'b+',
                 X[Y< 0.5,0], X[Y< 0.5,1], 'ro')
            plt.figure(1).set_axes([axes])
            plt.legend(('positive', 'negative'))

但是我收到此错误:

plotClassifier.plotLinearClassifier(h, datasets.TwoDAxisAligned.X, datasets.TwoDAxisAligned.Y)
  File "...\commons\plotClassifier.py", line 88, in plotLinearClassifier
    plt.figure(1).set_axes([axes])
AttributeError: 'Figure' object has no attribute 'set_axes'

我不确定应该用什么替换代码中的“ set_axes”函数,因为该函数在该库的matplotlib-3.0.0版本中似乎已弃用。

任何提示

0 个答案:

没有答案