A few weeks back, a colleague was searching for matlab for a task to get the Voronoi diagram from some points he had. I suggested why not use #python? He was not convinced and he got hold of matlab license and did his job.
But I had this itch for trying Voronoi in python, so here it is. Finally got some spare time. It was so easy in python, that I built the example around a tkinter gui.
But before we go to code, for everyone’s benefit What is Voronoi Diagram?
In mathematics, a Voronoi diagram is a way of dividing space into a number of regions. A set of points (called seeds, sites, or generators) is specified beforehand and for each seed there will be a corresponding region consisting of all points closer to that seed than to any other. The regions are called Voronoi cells. It is dual to the Delaunay triangulation.
via wikipedia
And here’s the code.
import Tkinter as Tk import tkFileDialog from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg import numpy as np from scipy.spatial import voronoi_plot_2d,Voronoi class SidePanel(): def __init__(self, root,ax0,fig): self.frame2 = Tk.Frame( root ) self.frame2.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1) self.ax=ax0 self.fig=fig self.ploted = False plotBut = Tk.Button(self.frame2, text="Plot") plotBut.pack(side="top",fill=Tk.BOTH) plotBut.bind("<Button>", self.my_plot) voroButton = Tk.Button(self.frame2, text="Load 2D points") voroButton.pack(side="top",fill=Tk.BOTH) voroButton.bind("<Button>", self.plotVo) clearButton = Tk.Button(self.frame2, text="Clear") clearButton.pack(side="top",fill=Tk.BOTH) clearButton.bind("<Button>", self.clear) def plotVo(self,event): self.ax.clear() root = Tk.Tk() root.withdraw() fname = tkFileDialog.askopenfilename() print fname # fname=r"/Users/Sukhbinder/Desktop/Desktop/sys2ndnov/SK14/SK14_v3_trials/SK14_trials/data/sk14/testcases/testcase5/cooords.item" data=np.genfromtxt(fname,skiprows=2) if data.shape[1] >2: data =data[:,:-1] self.var = Voronoi(data) voronoi_plot_2d(self.var,ax=self.ax) self.ploted = True self.fig.canvas.draw() def clear(self,event): self.ax.clear() self.fig.canvas.draw() def my_plot(self,event): self.ax.clear() self.ax.plot(np.random.rand(100)) self.ploted = True self.fig.canvas.draw() class Window(): def __init__(self, master): self.frame = Tk.Frame(master) self.f = Figure( figsize=(10, 9), dpi=80 ) self.ax0 = self.f.add_axes( (0.05, .05, .90, .90), axisbg=(.75,.75,.75), frameon=False) # self.ax0.plot(np.max(np.random.rand(100,10)*10,axis=1),"r-") self.frame = Tk.Frame( root ) self.frame.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1) self.sidepanel=SidePanel(root,self.ax0,self.f) self.canvas = FigureCanvasTkAgg(self.f, master=self.frame) self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) self.canvas.show() self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.frame ) self.toolbar.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) self.toolbar.update() if __name__ == '__main__': root = Tk.Tk() app = Window(root) root.title( "Voronoi with Python" ) root.update() root.deiconify() root.mainloop()
Please see, if you are still hungry for more information on Voronoi’s algorithm, see the excellent post titled A visual implementation of Fortune’s Voronoi algorithm
Pingback: A Shorter Version…. | SukhbinderSingh.com