【问题标题】:Plotting points - Python matplotlib绘图点 - Python matplotlib
【发布时间】:2020-06-12 16:08:40
【问题描述】:

我在绘制点时遇到问题。我对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

【问题讨论】:

  • 期望的输出是什么?

标签: python matplotlib plot


【解决方案1】:

您的features 数组中没有0,因此当您执行labels[:]==0 时,它会给您一个false 数组,这不是很有用。

我认为您想在第 3 行执行此操作:

features[labels[:]==-1,0],features[labels[:]==-1,1]

这是我这样做的结果:

【讨论】:

    【解决方案2】:

    试试这个:

    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)
    

    给出:

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2016-08-21
      • 1970-01-01
      相关资源
      最近更新 更多