I believe a similar question to this was asked before, but it didn't really clarify things for me.
Basically, I have a list of tuples, each of which functions as a point (so (x, y, z) for example).
I want to plot them in either 2D or 3D after I run a clustering algorithm (that color codes the points).
clusters = []
def plotPoints(self):
fig = plt.figure()
if self.clusters[0].dimensions == 2:
ax = fig.add_subplot(111)
for i in range(0, len(self.clusters)):
ax.scatter(*zip(*self.clusters[i].points), c=self.clusters[i].color, marker="o", label=("Cluster " + str(i + 1)))
plt.legend(loc='upper left')
plt.show()
elif self.clusters[0].dimensions == 3:
ax = fig.add_subplot(111, projection='3d')
for i in range(0, len(self.clusters)):
ax.scatter(*zip(*self.clusters[i].points), c=self.clusters[i].color, marker="o", label=("Cluster " + str(i + 1)))
plt.legend(loc='upper left')
plt.show()
else:
print "Cannot plot in dimensions lower than 2 or higher than 3"
Cluster class:
class Cluster(object):
centroid = ()
dimensions = 0
color = 'k'
def __init__(self, init_pt, color):
self.points = []
self.points.append(init_pt)
self.dimensions = len(init_pt)
self.centroid = init_pt
self.color = color
def addPoint(self, pt):
try:
if len(pt) != self.dimensions:
raise ArithmeticError("Wrong number of dimensions on new point, ignoring")
else:
centroid_dim_list = []
for dim in range(0, self.dimensions):
centroid_dim_list.append((self.centroid[dim] * len(self.points) + pt[dim]) / float(len(self.points) + 1))
self.centroid = tuple(centroid_dim_list)
self.points.append(pt)
except ArithmeticError as ae:
print ae.message
pass
2D plots work just fine (and look really nice), but 3D plots give me a warning:
UserWarning: No labeled objects found. Use label='...' kwarg on individual plots.
warnings.warn("No labeled objects found. "
And no legend appears. But I am labeling the points, and the code I am using is almost identical, so I am confused as to what the problem is. I heard something about a proxy object, but I have no clue how to use that for this case.