-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvononoir_delaunay_2d.py
69 lines (53 loc) · 1.67 KB
/
vononoir_delaunay_2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""Vononoir Delaunay 2D.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ce7bmXQUlSuUkAP4edoBXjjUhUSRMCPX
** Plotting a voronoi diagram **
"""
# Commented out IPython magic to ensure Python compatibility.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
# %matplotlib inline
points = np.array([[8.381184, 0.152739],
[0.196395, 7.467857],
[6.812772, 4.450964],
[3.794810, 9.318146],
[8.317960, 4.659943],
[5.028129, 4.186495],
[7.094714, 8.462214],
[4.288924, 5.251525],
[3.046174, 2.026474],
[1.896537, 6.721375]])
fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
from scipy.spatial import Voronoi, voronoi_plot_2d
vor = Voronoi(points)
fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
voronoi_plot_2d(vor, ax)
#https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.voronoi_plot_2d.html
"""**Plotting** **a** **Delaunay** **triangulation**"""
from scipy.spatial import Delaunay
tri = Delaunay(points)
fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
ax.triplot(points[:,0], points[:,1], tri.simplices.copy(), color='blue')
"""**Plotting both graphs**"""
fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
voronoi_plot_2d(vor, ax)
ax.triplot(points[:,0], points[:,1], tri.simplices.copy(), color='blue')