Python - 从CSV绘制群集

时间:2018-02-05 18:31:40

标签: python csv plot

我是Python的初学者。 我想从CSV数据中绘制一些二维簇。 CSV由x,y和标签组成。 CSV示例:

0.800717,0.84454,0
0.876446,0.626109,0
0.00504536,0.367249,1
0.0142114,0.794659,2
0.140504,0.240966,1

我尝试使用类似的东西,但它无法正常工作:

#!usr/bin/env

import numpy as np
import matplotlib.pyplot as plt
import csv

with open("kmeans.csv") as filein:
    reader = csv.reader(filein, skipinitialspace = True)
    xs, ys, label = zip(*reader)

plt.scatter(xs, ys)
plt.savefig("output.png")

当前输出示例:

current output 预期的结果将是这样的(忽略质心):

expected output

1 个答案:

答案 0 :(得分:0)

我建议您使用pandas,而不是csv reader:

justify-content: space-between

enter image description here