[英]Plotting Graph in Gnuplot or using matplotlib
I have a data set for clusters . 我有一个用于群集的数据集。 And the output seems something like this :- 输出看起来像这样:-
1 [1,2] 1 [1,2]
2 [1,6] 2 [1,6]
1 [2,4] 1 [2,4]
Where 1,2 ... is the cluster id and [1,2]..so on are the points . 其中1,2 ...是聚类ID,[1,2] .. so等是点。 So i want to plot the points x co-ordinate and y co-ordinate on both the axis and corresponding to that a point in graph depicting the cluster id as label and for different id the color of points should be different. 因此,我想在轴上绘制点x坐标和y坐标,并对应于图中将簇ID描述为标签的点,对于不同的ID,点的颜色应该不同。 How do i go about it? 我该怎么办?
Thanks 谢谢
If one axis is the cluster id I don't get how you fit both the x and y coordinates onto the other axis. 如果一个轴是集群ID我不明白你如何适应x和y坐标到另一个轴。 So I plotted the x,y on the x and y axis and used the cluster id as a label; 因此,我将x,y绘制在x和y轴上,并使用集群ID作为标签; you can swap which value goes into which axis, I guess: 您可以交换哪个值进入哪个轴,我猜:
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()
Nb: if you have a lot of data, don't call scatter
for each point separately; Nb:如果您有大量数据,请不要分别为每个点调用scatter
; append x, y , cluster to three separate lists and scatter
the lists. 将x,y,簇附加到三个单独的列表中并scatter
这些列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.