import matplotlib.pyplot as plt
from ast import literal_eval
data = """1 [1,2]
2 [1,6]
1 [2,4]"""
def cluster_color(clusternum):
'''Some kind of map of cluster ID to a color'''
if clusternum == 1:
return [(1, 0,0)]
if clusternum == 2:
return [(0, 0, 1)]
else:
return [(0, 1,0)]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def datalines(pt):
'''Pick the coordinates and label out of this text format'''
label, xy = pt.split(' ')
xy = literal_eval(xy)
assert(len(xy) == 2)
return xy[0], xy[1], label
for pt in data.splitlines():
if len(pt) > 0:
x, y, cluster = datalines(pt)
ax.scatter(x, y, c= cluster_color(float(cluster)))
ax.text(x + .01, y + .01, cluster)
fig.show()
如果其中一个轴是簇id,我不知道如何将x和y坐标拟合到另一个轴上。因此,我在x和y轴上绘制x,y,并使用集群id作为标签;您可以交换哪个值进入哪个轴,我猜:
注意:如果您有很多数据,不要分别为每个点调用
scatter
;将x、y、cluster附加到三个单独的列表中,scatter
这些列表。在相关问题 更多 >
编程相关推荐