前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Colorize Voronoi Diagram Template

Colorize Voronoi Diagram Template

作者头像
杨丝儿
发布2022-03-01 13:12:07
2000
发布2022-03-01 13:12:07
举报
文章被收录于专栏:杨丝儿的小站杨丝儿的小站

Stackoverflow

Colorize Voronoi Diagram

https://stackoverflow.com/questions/20515554/colorize-voronoi-diagram/20678647#20678647

IPython Cookbook, Second Edition (2018)

14.5. Computing the Voronoi diagram of a set of points

https://ipython-books.github.io/145-computing-the-voronoi-diagram-of-a-set-of-points/

template

代码语言:javascript
复制
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d

def normalizer(dataArray):
    if dataArray.max() - dataArray.min() == 0:
        return dataArray
    return (dataArray-dataArray.min())/(dataArray.max() - dataArray.min())

def comparisionPlot(columnName0, columnName1, hueColumn, dataDf):
    # add 4 distant dummy points to help coloring the edge
    # dataPoints = np.append((dataDfNormalized)[['P-MEAN', 'RANK']], [[2,2], [-2,2], [2,-2], [-2,-2]], axis = 0)

    # normal version
    dataPoints = dataDf[[columnName0, columnName1]]

    # plot Voronoi diagrame
    vor = Voronoi(dataPoints)
    voronoi_plot_2d(vor, show_vertices = True, point_size = 2)

    # color list 
    colorList = []
    for regionIndex in range(len(vor.regions)):
        if not -1 in vor.regions[regionIndex]:
            polygon = [vor.vertices[i] for i in vor.regions[regionIndex]]
            if len(polygon) == 0:
                colorList += colorList[-1:]
                continue
            colorList += [np.array(polygon).transpose().min()]
        colorList += colorList[-1:]
    colorList = normalizer(np.array(colorList))

    # colorize
    for regionIndex in range(len(vor.regions)):
        if not -1 in vor.regions[regionIndex]:
            polygon = [vor.vertices[i] for i in vor.regions[regionIndex]]
            plt.fill(*zip(*polygon),color=np.repeat(colorList[regionIndex],3))

    # fix the range of axes
    plt.xlim([-0.2,1.2]), plt.ylim([-0.2,1.2])

    plt.show()

Question answering

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-12-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Stackoverflow
  • IPython Cookbook, Second Edition (2018)
  • template
  • Question answering
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档