hexagons.py 文件源码

python
阅读 15 收藏 0 点赞 0 评论 0

项目:SimpSOM 作者: fcomitani 项目源码 文件源码
def plot_hex(fig, centers, weights):

    """Plot an hexagonal grid based on the nodes positions and color the tiles
        according to their weights.

        Args:
            fig (matplotlib figure object): the figure on which the hexagonal grid will be plotted.
            centers (list, float): array containing couples of coordinates for each cell 
                 to be plotted in the Hexagonal tiling space.
            weights (list, float): array contaning informations on the weigths of each cell, 
                to be plotted as colors.

        Returns:
            ax (matplotlib axis object): the axis on which the hexagonal grid has been plotted.

        """

    ax = fig.add_subplot(111, aspect='equal')

    xpoints = [x[0]  for x in centers]
    ypoints = [x[1]  for x in centers]
    patches = []

    if any(isinstance(el, list) for el in weights) and len(weights[0])==3:

        for x,y,w in zip(xpoints,ypoints,weights):
            hexagon = RegularPolygon((x,y), numVertices=6, radius=.95/np.sqrt(3) , 
                                 orientation=np.radians(0), 
                                 facecolor=w)
            ax.add_patch(hexagon)

    else:

        cmap = plt.get_cmap('viridis')
        for x,y,w in zip(xpoints,ypoints,weights):
            hexagon = RegularPolygon((x,y), numVertices=6, radius=.95/np.sqrt(3) , 
                                 orientation=np.radians(0), 
                                 facecolor=cmap(w))
            patches.append(hexagon) 

        p = PatchCollection(patches)
        p.set_array(np.array(weights))
        ax.add_collection(p)

    ax.axis('off')
    ax.autoscale_view()

    return ax
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号