python - 绘图点 - 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
解决方案
尝试这个:
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)
这使:
推荐阅读
- python - 通过将 for 循环替换为 pandas 操作来提高代码效率
- javascript - 在nodejs中检查日期数组是否由n个连续天组成的最佳方法?
- spring-boot-actuator - 是否有 Hibernate Metrics 千分尺可用于捕获 Hibernate/JPA SQL 语句、最小、最大时间
- json - 托管 API 并将 Json 数据推送到 S3,同时将其转换为 Avro
- python - 'TypeError:元组索引必须是整数或切片,而不是元组'是什么意思?
- bash - 显示 gcloud 的所有行
作业列表输出 - node.js - 如何在 Node JS 中上传文件?
- jhipster - 无法启动使用 JHipster 生成的后端
- csv - Golang 读取 csv 在内存中消耗的空间是磁盘空间的 2 倍以上
- vb.net - 使用 VB.NET 的 SQLite 数据库的性能问题