I have a class POLYGON, each object of this class has as its parameters the coordinates of the vertices of the polygon (the hexagon in this case) and the parameter "value", which displays, for example, the value of a function in the hexagon. I create the required number of hexagons at given points without any problems. The question is how to draw them, so that each hexagon is colored in accordance with the value of the parameter "value" and also draw a colorbar. All this using the matplotlib library. An example of how it should turn out is in the picture.

Shading example

POLYGON class:

class POLYGON: def __init__(self,xc,yc,znum,value,st): self.xc=xc #ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Ρ†Π΅Π½Ρ‚Ρ€Π° Π½Π° оси абсцисс self.yc=yc #ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Ρ†Π΅Π½Ρ‚Ρ€Π° Π½Π° оси ΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ self.znum=znum #для Π½ΡƒΠΌΠ΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈ поиска self.value=value #Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π² Π΄Π°Π½Π½ΠΎΠΌ ΡˆΠ΅ΡΡ‚ΠΈΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ΅ self.st=st #Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΏΠΎΠ΄ ΠΊΠ»ΡŽΡ‡ self.r=st/2 #радиус вписанной окруТности self.R=st/pow(3,.5) #радиус описанной окруТности self.a=self.R #сторона ΡˆΠ΅ΡΡ‚ΠΈΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° self.points=[(self.xc+self.a/2,self.yc+self.r), #массив ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ Π²Π΅Ρ€ΡˆΠΈΠ½ (self.xc+self.R,self.yc), (self.xc+self.a/2,self.yc-self.r), (self.xc-self.a/2,self.yc-self.r), (self.xc-self.R,self.yc), (self.xc-self.a/2,self.yc+self.r)] 

I can draw them using pylab:

 def drawPolygons (axes,points,color): """ РисованиС ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° """ polygon_1 = pylab.Polygon (points, facecolor=color, edgecolor='black') axes.add_patch (polygon_1) #Допустим, Π΅ΡΡ‚ΡŒ массив ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ Ρ†Π΅Π½Ρ‚Ρ€ΠΎΠ² ΡˆΠ΅ΡΡ‚ΠΈΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠΎΠ² xc=[0, 10.392304845413264, 20.784609690826528] yc=[0, -6.0, -12.0] poly=[] #Массив, содСрТащий ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Ρ‹ ΡˆΠ΅ΡΡ‚ΠΈΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠΎΠ² for i in range(len(xc)): #Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΡˆΠ΅ΡΡ‚ΠΈΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° с Π΄Π°Π½Π½Ρ‹ΠΌΠΈ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌΠΈ Ρ†Π΅Π½Ρ‚Ρ€Π° #Π‘ΠΎ случайным ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠΌ "value" pol=POLYGON(xc[i],yc[i],'znum',random.randint(0,20),12) poly.append(pol) pylab.xlim (-50, 50) pylab.ylim (-50, 50) axes = pylab.gca() axes.set_aspect("equal") for i in range(len(poly)): a=poly[i].points drawPolygons (axes,a,(1.0,0.0,0.0,0.7)) pylab.show() 

However, in this case, you have to explicitly set the color. How to make automatic generation of colors for any colormaps from matplotlib, knowing the value of "value" for all hexagons, and how to make the colorbar do not understand.

  • @MaxU Added a class and a task of objects - romanenkovli
  • @MaxU sorry, corrected, now should work - romanenkovli

1 answer 1

 import random import matplotlib.pyplot as plt import matplotlib.cm as cm class POLYGON: def __init__(self,xc,yc,znum,value,st): self.xc=xc #ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Ρ†Π΅Π½Ρ‚Ρ€Π° Π½Π° оси абсцисс self.yc=yc #ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Ρ†Π΅Π½Ρ‚Ρ€Π° Π½Π° оси ΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ self.znum=znum #для Π½ΡƒΠΌΠ΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈ поиска self.value=value #Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π² Π΄Π°Π½Π½ΠΎΠΌ ΡˆΠ΅ΡΡ‚ΠΈΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ΅ self.st=st #Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΏΠΎΠ΄ ΠΊΠ»ΡŽΡ‡ self.r=st/2 #радиус вписанной окруТности self.R=st/pow(3,.5) #радиус описанной окруТности self.a=self.R #сторона ΡˆΠ΅ΡΡ‚ΠΈΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° self.points=[(self.xc+self.a/2,self.yc+self.r), #массив ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ Π²Π΅Ρ€ΡˆΠΈΠ½ (self.xc+self.R,self.yc), (self.xc+self.a/2,self.yc-self.r), (self.xc-self.a/2,self.yc-self.r), (self.xc-self.R,self.yc), (self.xc-self.a/2,self.yc+self.r)] def draw(self, ax=None, cmap=None, norm=None, edgecolor='black'): ax = ax if ax is not None else plt.gca() if cmap is None: cmap = cm.get_cmap('jet') if norm is None: norm = matplotlib.colors.Normalize(0, 20) polygon_1 = plt.Polygon(self.points, facecolor=cmap(norm(self.value)), edgecolor=edgecolor) ax.add_patch(polygon_1) xc=[0, 10.392304845413264, 20.784609690826528] yc=[0, -6.0, -12.0] polys = [POLYGON(x, y, 'znum', random.randint(0,20), 12) for x,y in zip(xc, yc)] cmap = cm.get_cmap('jet') # or any other one norm = matplotlib.colors.Normalize(0, 20) ax = plt.gca() _ = [pol.draw(ax=ax, cmap=cmap, norm=norm) for pol in polys] plt.xlim (-50, 50) plt.ylim (-50, 50) ax.set_aspect("equal") plt.show() 

enter image description here

  • Thank you very much, I built it into my program, everything works, I understood everything) I think I can handle the colorbar myself) - romanenkovli