matplotlib - Displaying data in a hexagonal grid using Python -



matplotlib - Displaying data in a hexagonal grid using Python -

what looking method or class allows me display list of hexagons in grid. ideally able utilize form of set-method alter color/hatching/border of individual hexagons.

the hexagons stored using axial coordinate scheme outlined in guide @amitp. can output centers xy-coordinates.

i sense there might solution hidden somewhere in hexbin or regularpolycollection. former histogram method , latter seems overly complex dpi scaling.

so know of library provides hexagonal grid? doesn't have in matplotlib. quite happy utilize ascii fine art or switch r.

here comes implementation allow set hexagonal cell color want , allow create custom border colors.

this hand made, took 1hour using reference site. may have adapt needs, seems working.

from tkinter import * class hexacanvas(canvas): """ canvas provides create-hexagone method """ def __init__(self, master, *args, **kwargs): canvas.__init__(self, master, *args, **kwargs) self.hexasize = 20 def sethexasize(self, number): self.hexasize = number def create_hexagone(self, x, y, color = "black", fill="blue", color1=none, color2=none, color3=none, color4=none, color5=none, color6=none): """ compute coordinates of 6 points relative center position. point numbered next schema : points in euclidiean grid: 6 5 1 . 4 2 3 each color applied side link vertex same number following. ex : color 1 applied on side (vertex1, vertex2) take care tkinter ordinate axes inverted standard euclidian ones. point on screen horizontally mirrored. displayed points: 3 color3/ \color2 4 2 color4| |color1 5 1 color6\ /color6 6 """ size = self.hexasize Δx = (size**2 - (size/2)**2)**0.5 point1 = (x+Δx, y+size/2) point2 = (x+Δx, y-size/2) point3 = (x , y-size ) point4 = (x-Δx, y-size/2) point5 = (x-Δx, y+size/2) point6 = (x , y+size ) #this setting allow specify different color each side. if color1 == none: color1 = color if color2 == none: color2 = color if color3 == none: color3 = color if color4 == none: color4 = color if color5 == none: color5 = color if color6 == none: color6 = color self.create_line(point1, point2, fill=color1, width=2) self.create_line(point2, point3, fill=color2, width=2) self.create_line(point3, point4, fill=color3, width=2) self.create_line(point4, point5, fill=color4, width=2) self.create_line(point5, point6, fill=color5, width=2) self.create_line(point6, point1, fill=color6, width=2) if fill != none: self.create_polygon(point1, point2, point3, point4, point5, point6, fill=fill) class hexagonalgrid(hexacanvas): """ grid each cell hexagonal """ def __init__(self, master, scale, grid_width, grid_height, *args, **kwargs): Δx = (scale**2 - (scale/2.0)**2)**0.5 width = 2 * Δx * grid_width + Δx height = 1.5 * scale * grid_height + 0.5 * scale hexacanvas.__init__(self, master, background='white', width=width, height=height, *args, **kwargs) self.sethexasize(scale) def setcell(self, xcell, ycell, *args, **kwargs ): """ create content in cell of coordinates x , y. specify options throught keywords : color, fill, color1, color2, color3, color4; color5, color6""" #compute pixel coordinate of center of cell: size = self.hexasize Δx = (size**2 - (size/2)**2)**0.5 pix_x = Δx + 2*Δx*xcell if ycell%2 ==1 : pix_x += Δx pix_y = size + ycell*1.5*size self.create_hexagone(pix_x, pix_y, *args, **kwargs) if __name__ == "__main__": tk = tk() grid = hexagonalgrid(tk, scale = 50, grid_width=4, grid_height=4) grid.grid(row=0, column=0, padx=5, pady=5) def correct_quit(tk): tk.destroy() tk.quit() quit = button(tk, text = "quit", command = lambda :correct_quit(tk)) quit.grid(row=1, column=0) grid.setcell(0,0, fill='blue') grid.setcell(1,0, fill='red') grid.setcell(0,1, fill='green') grid.setcell(1,1, fill='yellow') grid.setcell(2,0, fill='cyan') grid.setcell(0,2, fill='teal') grid.setcell(2,1, fill='silver') grid.setcell(1,2, fill='white') grid.setcell(2,2, fill='gray') tk.mainloop()

i tried comment code properly. if seems unclear you, please not hesitate inquire explanation.

good luck arthur vaisse.

nb : script running on python 3. drawing little jagged. improve on tk canvas can add together anti alias suggested in https://mail.python.org/pipermail/tkinter-discuss/2009-april/001904.html

python matplotlib tkinter hexagonal-tiles

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -