In [63]:
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.display import display, HTML
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from operator import attrgetter
from matplotlib.patches import Ellipse
from math import atan2,degrees

In [5]:
points = pd.read_csv('Clustering.csv',sep=',')

In [21]:
plt.plot(points.X, points.Y, 'ro')
plt.axis([min(points.X)-1, max(points.X)+1, min(points.Y)-1, max(points.Y)+1])
for i, txt in enumerate(points.index):
    plt.annotate(txt+1, (points.X[i],points.Y[i]))
print type(plt)
midpoint = (((points.X[17]+points.X[8])/2,(points.Y[17]+points.Y[8])/2))
e = Ellipse(midpoint,5,5,30)
plt.add_artist(e)
plt.show()


<type 'module'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-2207afc0ef99> in <module>()
      6 midpoint = (((points.X[17]+points.X[8])/2,(points.Y[17]+points.Y[8])/2))
      7 e = Ellipse(midpoint,5,5,30)
----> 8 plt.add_artist(e)
      9 plt.show()

AttributeError: 'module' object has no attribute 'add_artist'

In [7]:
class Dist:
    def __init__(self,p1indx,p2indx,dist):
        self.p1 = p1indx
        self.p2 = p2indx
        self.dist=dist
distances={}
distancelst = []
for i in range(len(points.index)):
    for j in range(len(points.index)):
        if i==j:
            continue
        if (i,j) not in distances.keys():
            dist = np.linalg.norm(np.array([points.X[i],points.Y[i]])-np.array([points.X[j],points.Y[j]]))
            distances[(i,j)]=dist
            distances[(j,i)]=dist
            distancelst.append(Dist(i,j,dist))

In [9]:
distancelst.sort(key=lambda x: x.dist, reverse=False)

In [11]:
for i in distancelst:
    print i.dist,i.p1+1,i.p2+1


0.424264068712 9 18
0.583095189485 6 11
0.707106781187 3 12
0.707106781187 3 13
0.707106781187 12 19
0.761577310586 12 14
0.824621125124 2 14
0.824621125124 14 19
0.860232526704 10 11
1.0 3 19
1.0 6 10
1.0 12 13
1.11803398875 7 15
1.11803398875 18 20
1.1401754251 9 11
1.21655250606 9 10
1.3152946438 9 20
1.39283882772 1 11
1.41421356237 1 6
1.41421356237 2 19
1.41421356237 3 4
1.41421356237 4 8
1.41421356237 6 19
1.44222051019 3 14
1.56204993518 11 18
1.58113883008 2 12
1.58113883008 4 13
1.58113883008 7 17
1.58113883008 8 17
1.58113883008 10 18
1.58113883008 13 19
1.69705627485 6 9
1.72626765016 13 14
1.80277563773 15 16
1.80277563773 15 17
1.96977156036 6 14
1.98494332413 11 19
2.0 1 19
2.0 2 6
2.11896201004 11 20
2.12132034356 4 12
2.12132034356 6 12
2.12132034356 6 18
2.20907220344 1 9
2.2360679775 1 3
2.2360679775 1 4
2.2360679775 1 7
2.2360679775 1 10
2.2360679775 2 3
2.2360679775 2 10
2.2360679775 3 6
2.2360679775 4 19
2.2360679775 10 19
2.5 1 20
2.5 5 20
2.5 10 20
2.5179356624 2 11
2.54558441227 10 14
2.5495097568 1 12
2.5495097568 1 18
2.5495097568 2 13
2.5495097568 4 17
2.5495097568 16 17
2.5495097568 11 14
2.69072480941 11 12
2.69258240357 6 20
2.74590604355 3 11
2.80713376952 1 14
2.82842712475 3 8
2.82842712475 7 8
2.82842712475 7 16
2.84253408071 4 14
2.91547594742 1 13
2.91547594742 1 17
2.91547594742 6 13
2.91547594742 8 13
2.91547594742 10 12
3.0 1 8
3.0 4 6
3.04138126515 7 20
3.11126983722 9 19
3.16227766017 1 2
3.16227766017 3 10
3.16227766017 4 7
3.33766385366 4 11
3.35410196625 1 15
3.35410196625 8 15
3.39705755029 7 11
3.41760149813 2 9
3.44093010682 11 13
3.53553390593 5 18
3.53553390593 8 12
3.53553390593 18 19
3.60555127546 2 4
3.60555127546 5 7
3.60555127546 6 7
3.60555127546 8 19
3.60555127546 9 14
3.64005494464 5 15
3.67151195014 7 9
3.80525951809 5 9
3.80788655293 2 18
3.80788655293 3 17
3.80788655293 7 18
3.80788655293 10 13
3.80788655293 15 20
3.81837661841 9 12
3.88329756779 3 9
4.0 3 7
4.0 4 10
4.0 8 16
4.02243707222 14 18
4.03112887415 4 15
4.03112887415 19 20
4.12310562562 6 8
4.12310562562 7 19
4.12310562562 13 17
4.24264068712 7 10
4.24264068712 12 18
4.25205832509 8 14
4.29418211072 11 17
4.30116263352 3 18
4.30116263352 6 17
4.30116263352 17 19
4.32897216438 8 11
4.36806593357 4 9
4.472135955 1 5
4.472135955 12 17
4.4821869662 11 15
4.5 17 20
4.52769256907 7 12
4.52769256907 7 13
4.55411901469 5 11
4.58039299624 9 13
4.60977222865 2 20
4.60977222865 3 20
4.64004310325 9 15
4.66154480832 14 20
4.71699056603 4 20
4.71699056603 6 15
4.71699056603 12 20
4.71699056603 15 18
4.74341649025 4 18
4.87647413609 9 17
4.94772675074 7 14
5.0 1 16
5.0 2 8
5.0 5 10
5.0 5 16
5.0 13 18
5.02493781056 3 15
5.07740090991 14 17
5.09901951359 4 16
5.09901951359 5 6
5.09901951359 8 10
5.09901951359 17 18
5.14781507049 5 17
5.14781507049 10 17
5.2038447325 8 9
5.22015325446 8 20
5.22015325446 15 19
5.31507290637 10 15
5.31507290637 13 20
5.38516480713 2 7
5.5 13 15
5.52268050859 8 18
5.59016994375 12 15
5.59016994375 16 20
5.7008771255 2 17
6.0440052945 14 15
6.22414652784 11 16
6.32455532034 3 16
6.32455532034 5 19
6.40312423743 4 5
6.40312423743 5 8
6.40312423743 6 16
6.44049687524 9 16
6.5 2 15
6.5192024052 16 18
6.67083203206 13 16
6.7082039325 3 5
6.7082039325 16 19
6.96419413859 5 12
6.96419413859 12 16
7.03420215803 5 14
7.07106781187 2 5
7.07106781187 10 16
7.38241153012 5 13
7.51531769122 14 16
8.0622577483 2 16

0.424264068712 9 18 0.583095189485 6 11 0.707106781187 3 12 0.707106781187 3 13 0.707106781187 12 19 0.761577310586 12 14 0.824621125124 2 14 0.824621125124 14 19 0.860232526704 10 11 1.0 3 19 1.0 6 10 1.0 12 13 1.11803398875 7 15 1.11803398875 18 20 1.1401754251 9 11 1.21655250606 9 10 1.3152946438 9 20 1.39283882772 1 11 1.41421356237 1 6 1.41421356237 2 19 1.41421356237 3 4 1.41421356237 4 8 1.41421356237 6 19 1.44222051019 3 14 1.56204993518 11 18 1.58113883008 2 12 1.58113883008 4 13 1.58113883008 7 17 1.58113883008 8 17 1.58113883008 10 18 1.58113883008 13 19 1.69705627485 6 9 1.72626765016 13 14 1.80277563773 15 16 1.80277563773 15 17 1.96977156036 6 14 1.98494332413 11 19 2.0 1 19 2.0 2 6 2.11896201004 11 20 2.12132034356 4 12 2.12132034356 6 12 2.12132034356 6 18 2.20907220344 1 9 2.2360679775 1 3 2.2360679775 1 4 2.2360679775 1 7 2.2360679775 1 10 2.2360679775 2 3 2.2360679775 2 10 2.2360679775 3 6 2.2360679775 4 19 2.2360679775 10 19 2.5 1 20 2.5 5 20 2.5 10 20 2.5179356624 2 11 2.54558441227 10 14 2.5495097568 1 12 2.5495097568 1 18 2.5495097568 2 13 2.5495097568 4 17 2.5495097568 16 17 2.5495097568 11 14 2.69072480941 11 12 2.69258240357 6 20 2.74590604355 3 11 2.80713376952 1 14 2.82842712475 3 8 2.82842712475 7 8 2.82842712475 7 16 2.84253408071 4 14 2.91547594742 1 13 2.91547594742 1 17 2.91547594742 6 13 2.91547594742 8 13 2.91547594742 10 12 3.0 1 8 3.0 4 6 3.04138126515 7 20 3.11126983722 9 19 3.16227766017 1 2 3.16227766017 3 10 3.16227766017 4 7 3.33766385366 4 11 3.35410196625 1 15 3.35410196625 8 15 3.39705755029 7 11 3.41760149813 2 9 3.44093010682 11 13 3.53553390593 5 18 3.53553390593 8 12 3.53553390593 18 19 3.60555127546 2 4 3.60555127546 5 7 3.60555127546 6 7 3.60555127546 8 19 3.60555127546 9 14 3.64005494464 5 15 3.67151195014 7 9 3.80525951809 5 9 3.80788655293 2 18 3.80788655293 3 17 3.80788655293 7 18 3.80788655293 10 13 3.80788655293 15 20 3.81837661841 9 12 3.88329756779 3 9 4.0 3 7 4.0 4 10 4.0 8 16 4.02243707222 14 18 4.03112887415 4 15 4.03112887415 19 20 4.12310562562 6 8 4.12310562562 7 19 4.12310562562 13 17 4.24264068712 7 10 4.24264068712 12 18 4.25205832509 8 14 4.29418211072 11 17 4.30116263352 3 18 4.30116263352 6 17 4.30116263352 17 19 4.32897216438 8 11 4.36806593357 4 9 4.472135955 1 5 4.472135955 12 17 4.4821869662 11 15 4.5 17 20 4.52769256907 7 12 4.52769256907 7 13 4.55411901469 5 11 4.58039299624 9 13 4.60977222865 2 20 4.60977222865 3 20 4.64004310325 9 15 4.66154480832 14 20 4.71699056603 4 20 4.71699056603 6 15 4.71699056603 12 20 4.71699056603 15 18 4.74341649025 4 18 4.87647413609 9 17 4.94772675074 7 14 5.0 1 16 5.0 2 8 5.0 5 10 5.0 5 16 5.0 13 18 5.02493781056 3 15 5.07740090991 14 17 5.09901951359 4 16 5.09901951359 5 6 5.09901951359 8 10 5.09901951359 17 18 5.14781507049 5 17 5.14781507049 10 17 5.2038447325 8 9 5.22015325446 8 20 5.22015325446 15 19 5.31507290637 10 15 5.31507290637 13 20 5.38516480713 2 7 5.5 13 15 5.52268050859 8 18 5.59016994375 12 15 5.59016994375 16 20 5.7008771255 2 17 6.0440052945 14 15 6.22414652784 11 16 6.32455532034 3 16 6.32455532034 5 19 6.40312423743 4 5 6.40312423743 5 8 6.40312423743 6 16 6.44049687524 9 16 6.5 2 15 6.5192024052 16 18 6.67083203206 13 16 6.7082039325 3 5 6.7082039325 16 19 6.96419413859 5 12 6.96419413859 12 16 7.03420215803 5 14 7.07106781187 2 5 7.07106781187 10 16 7.38241153012 5 13 7.51531769122 14 16 8.0622577483 2 16


In [20]:
import numpy.random as rnd
NUM = 250

ells = [Ellipse(xy=rnd.rand(2)*10, width=rnd.rand(), height=rnd.rand(), angle=rnd.rand()*360)
        for i in range(NUM)]

fig = plt.figure(0)
ax = fig.add_subplot(111, aspect='equal')
for e in ells:
    ax.add_artist(e)
    e.set_clip_box(ax.bbox)
    e.set_alpha(rnd.rand())
    e.set_facecolor(rnd.rand(3))

ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
print type(ax)
plt.show()


<class 'matplotlib.axes._subplots.AxesSubplot'>

In [150]:
def EclispeInfo(p1indx,p2indx,increment=0.2,anglecoef=1):
    delta_x = points.X[p1indx]-points.X[p2indx]
    delta_y = points.Y[p1indx]-points.Y[p2indx]
    theta_radians = atan2(delta_y, delta_x)
    e = Ellipse(((points.X[p1indx]+points.X[p2indx])/2,(points.Y[p1indx]+points.Y[p2indx])/2),
            abs(delta_y)+increment,abs(delta_x)+increment,-2*anglecoef*degrees(theta_radians))
    e.set_clip_box(ax.bbox)
    e.set_alpha(0.4)
    e.set_facecolor(rnd.rand(3))
    return e

In [162]:
fig = plt.figure(0)
ax = fig.add_subplot(111, aspect='equal')

ax.plot(points.X, points.Y, 'ro')
ax.axis([min(points.X)-1, max(points.X)+1, min(points.Y)-1, max(points.Y)+1])
for i, txt in enumerate(points.index):
    ax.annotate(txt+1, (points.X[i],points.Y[i]))
ax.add_artist(EclispeInfo(17,8))
ax.add_artist(EclispeInfo(5,10))
ax.add_artist(EclispeInfo(2,11))
ax.add_artist(EclispeInfo(6,14))
ax.add_artist(EclispeInfo(17,19,1))
ax.add_artist(EclispeInfo(5,9,1))
ax.add_artist(EclispeInfo(2,12,1))
ax.add_artist(EclispeInfo(6,16,1))
ax.add_artist(EclispeInfo(8,9,3.5,0.5))
ax.add_artist(EclispeInfo(8,9,3.9))
ax.add_artist(EclispeInfo(12,18,0.8,2.6))
#And So On I stopped due unclearity of the plot after a while.
plt.show()



In [ ]: