首页 > 解决方案 > 绘图点 - Python matplotlib

问题描述

我在绘制点时遇到了麻烦。我对python不太了解,而且我还在学习。

这是我的代码:

def graph(features, labels,classes):
  plt.plot(features[labels[:]==1,0], features[labels[:]==1,1],'g^',
       features[labels[:]==0,0],features[labels[:]==0,1],'rs')
  plt.axis([-4, 4,-4, 4])
  plt.xlabel('x: feature 1')
  plt.ylabel('y: feature 2')
  plt.legend(['Class'+str(classes[1]), 'Class'+str(classes[0])])
  plt.show()


features=np.array([[1,1], [1,0], [0,1], [-1,-1], [0.5,3], [0.7,2], [-1,0], [-1,1], [2,0], [-2,-1]])
labels=np.array([1,1,-1,-1,1,1,-1,-1,1,-1])
classes=[0,1]

当我运行代码时,这是输出: Plotting points

标签: pythonmatplotlibplot

解决方案


尝试这个:

def graph(features, labels,classes):
    plt.plot(features[:,0][labels==1], features[:,1][labels==1],'g^')
    plt.plot(features[:,0][labels==-1],features[:,1][labels==-1],'rs')

    plt.axis([-4, 4,-4, 4])
    plt.xlabel('x: feature 1')
    plt.ylabel('y: feature 2')
    plt.legend(['Class'+str(classes[1]), 'Class'+str(classes[0])])
    plt.show()


features=np.array([[1,1], [1,0], [0,1], [-1,-1], [0.5,3], [0.7,2], [-1,0], [-1,1], [2,0], [-2,-1]])
labels=np.array([1,1,-1,-1,1,1,-1,-1,1,-1])
classes=[0,1]

graph(features,labels,classes)

这使:

在此处输入图像描述


推荐阅读