Python:在scipy情节之上绘制情节? (沃罗诺伊)

时间:2017-09-01 09:22:43

标签: python numpy matplotlib scipy voronoi

如何绘制voronoi图(这是一个scipy图)?请注意,我的问题与here略有不同,他们解释了如何颜色一个voronoi情节

例如,假设我有更多的分数

points = np.array([[1,2], [3,4], [5,6], [7,8]])

在第一次voronoi情节之后。我想在现有的情节中添加它们。我该怎么做?

voronoi情节I'指的是scipy.spatial.voronoi_plot_2d()

2 个答案:

答案 0 :(得分:3)

我认为你可以简单地重复使用这样的情节:

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import voronoi_plot_2d, Voronoi

points = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]])
v = Voronoi(points)
voronoi_plot_2d(v)

p2 = [[0.25, 1], [1, 0.75], [1.75, 0.25], [1.75, 1.75]]
x, y = zip(*p2)

plt.scatter(x, y, color='r')
plt.show()

Voronoi + points

答案 1 :(得分:0)

我遇到了同样的问题。这是一个可以显式执行的功能。注意:它将点放在无穷远处。 那里有很多改进的空间;随时进行编辑。

def plot_vor_edges(vor, ax=None):

    if ax is None:
        ax = plt.axes()

    ver = vor.vertices

    for reg in vor.regions:
        # Remove vertices at infinity
        if len(reg)>=1:
            if -1 in reg:
                reg = np.roll(reg, -reg.index(-1))
                reg = [x for x in reg if x != -1]

                regionX = ver[reg, 1]
                regionY = ver[reg, 0]
            else:
                regionX = ver[reg + [reg[0]], 1]
                regionY = ver[reg + [reg[0]], 0]

            ax.plot(regionX, regionY, '-k', linewidth=0.5)
        else:
            continue