はじめに
ボロノイ図とは、任意の位置に配置された複数個の点に対して、同一平面上の他の点がどの点に近いかによって分割した図のことである。ここでは、scipy.spatialのVoronoi, voronoi_plot_2dでボロノイ図を作成する方法を紹介する。
コード

解説
モジュールのインポート
バージョン
データの作成
np.random.rand(12,2)で0から1の値をもつ2次元配列を作成する。
ボロノイ図の作成
vor = Voronoi(points)としてから、voronoi_plot_2d(vor)でボロノイ図が作成できる。

図の調整
ボロノイ分割の頂点を非表示にする
show_vertices=Falseで頂点を非表示にできる。

データ点を非表示にする
show_points=Falseでデータ点を非表示にできる。

点のサイズを変更する
point_sizeにによりデータ点のサイズを変更することができる。

分割線のスタイルを変更する
line_colors, line_width, line_alphaにより線の外観を変更できる。

任意のaxにボロノイ図をいれる
subplotsで作成したaxに対して、voronoi_plot_2dでax=axとすればaxにボロノイ図を入れられる。

ボロノイ分割の頂点のサイズの変更
vor.verticesにより、頂点の座標が得られるので、それをボロノイ図上に重ねてプロットする。

参考

ボロノイ図 - Wikipedia
voronoi_plot_2d — SciPy v1.15.2 Manual
コメント