Пример #1
0
def voronoi_plot_no_points(vor, ax=None):
    """
    Plot the given Voronoi diagram in 2-D
    Parameters
    ----------
    vor : scipy.spatial.Voronoi instance
        Diagram to plot
    ax : matplotlib.axes.Axes instance, optional
        Axes to plot on
    Returns
    -------
    fig : matplotlib.figure.Figure instance
        Figure for the plot
    See Also
    --------
    Voronoi
    Notes
    -----
    Requires Matplotlib.
    """
    if vor.points.shape[1] != 2:
        raise ValueError("Voronoi diagram is not 2-D")

    # ax.plot(vor.points[:, 0], vor.points[:, 1], 'o')
    # ax.plot(vor.vertices[:,0], vor.vertices[:,1], 'o')

    for simplex in vor.ridge_vertices:
        simplex = np.asarray(simplex)
        if np.all(simplex >= 0):
            ax.plot(vor.vertices[simplex, 0], vor.vertices[simplex, 1], 'k-')

    ptp_bound = vor.points.ptp(axis=0)

    center = vor.points.mean(axis=0)
    for pointidx, simplex in zip(vor.ridge_points, vor.ridge_vertices):
        simplex = np.asarray(simplex)
        if np.any(simplex < 0):
            i = simplex[simplex >= 0][0]  # finite end Voronoi vertex

            t = vor.points[pointidx[1]] - vor.points[pointidx[0]]  # tangent
            t /= np.linalg.norm(t)
            n = np.array([-t[1], t[0]])  # normal

            midpoint = vor.points[pointidx].mean(axis=0)
            direction = np.sign(np.dot(midpoint - center, n)) * n
            far_point = vor.vertices[i] + direction * ptp_bound.max()

            ax.plot([vor.vertices[i, 0], far_point[0]],
                    [vor.vertices[i, 1], far_point[1]], 'k--')

    _adjust_bounds(ax, vor.points)

    return ax.figure
Пример #2
0
def hull_without_points(hull, ax=None):
    """
    modified from scipy.spatial.convex_hull_plot_2d
    """
    if ax is None:
        ax = pyplot.gcf().gca()

    if hull.points.shape[1] != 2:
        raise ValueError("Convex hull is not 2-D")

    for simplex in hull.simplices:
        ax.plot(hull.points[simplex, 0], hull.points[simplex, 1], 'k-')

    _adjust_bounds(ax, hull.points)

    return ax.figure
Пример #3
0
def hull_without_points(hull, ax=None):
    """
    modified from scipy.spatial.convex_hull_plot_2d
    """
    if ax is None:
        ax = pyplot.gcf().gca()
        
    if hull.points.shape[1] != 2:
        raise ValueError("Convex hull is not 2-D")

    for simplex in hull.simplices:
        ax.plot(hull.points[simplex,0], hull.points[simplex,1], 'k-')

    _adjust_bounds(ax, hull.points)

    return ax.figure
Пример #4
0
def cone_rays(angles, ai, hull, which=None, ax=None, fill_args={}, plot=True):
    """
   draw the cone rays
   """
    angles = np.sort(angles)
    points = np.array([np.cos(angles), np.sin(angles)]).T

    vor = Voronoi(points)

    if vor.points.shape[1] != 2:
        raise ValueError("Voronoi diagram is not 2-D")

    if ax is None:
        ax = pyplot.gca()

    rays = np.array([(np.cos(_angle), np.sin(_angle)) for _angle in angles])
    for i in range(rays.shape[0]):
        rays[i] /= np.linalg.norm(rays[i])

    if plot:
        for ray in rays:
            ax.plot([0, 100 * ray[0]], [0, 100 * ray[1]], 'k--')

    if which is not None:
        if which < rays.shape[0] - 1:
            active_rays = [100 * rays[which], 100 * rays[which + 1]]
        else:
            active_rays = [100 * rays[0], 100 * rays[-1]]
        poly = np.vstack([
            active_rays[0],
            np.zeros(2), active_rays[1],
            100 * (active_rays[0] + active_rays[1])
        ])
        dual_rays = np.linalg.pinv(np.array(active_rays))
        angle1 = angle(dual_rays[:, 0])
        angle2 = angle(dual_rays[:, 1])

    else:
        poly = None
        active_rays = None
        dual_rays = None

    _adjust_bounds(ax, vor.points)

    ax.set_xticks([])
    ax.set_yticks([])
    return ax, poly, dual_rays, np.array(active_rays)
Пример #5
0
def voronoi_plot_2d(vor, ax=None, **kw):
    """
    Plot the given Voronoi diagram in 2-D
    Parameters
    ----------
    vor : scipy.spatial.Voronoi instance
        Diagram to plot
    ax : matplotlib.axes.Axes instance, optional
        Axes to plot on
    show_points: bool, optional
        Add the Voronoi points to the plot.
    show_vertices : bool, optional
        Add the Voronoi vertices to the plot.
    line_colors : string, optional
        Specifies the line color for polygon boundaries
    line_width : float, optional
        Specifies the line width for polygon boundaries
    line_alpha: float, optional
        Specifies the line alpha for polygon boundaries
    point_size: float, optional
        Specifies the size of points
    Returns
    -------
    fig : matplotlib.figure.Figure instance
        Figure for the plot
    See Also
    --------
    Voronoi
    Notes
    -----
    Requires Matplotlib.
    Examples
    --------
    Set of point:
    >>> import matplotlib.pyplot as plt
    >>> points = np.random.rand(10,2) #random
    Voronoi diagram of the points:
    >>> from scipy.spatial import Voronoi, voronoi_plot_2d
    >>> vor = Voronoi(points)
    using `voronoi_plot_2d` for visualisation:
    >>> fig = voronoi_plot_2d(vor)
    using `voronoi_plot_2d` for visualisation with enhancements:
    >>> fig = voronoi_plot_2d(vor, show_vertices=False, line_colors='orange',
    ...                 line_width=2, line_alpha=0.6, point_size=2)
    >>> plt.show()
    """
    from matplotlib.collections import LineCollection

    if vor.points.shape[1] != 2:
        raise ValueError("Voronoi diagram is not 2-D")

    if kw.get('show_points', True):
        point_size = kw.get('point_size', None)
        ax.plot(vor.points[:, 0],
                vor.points[:, 1],
                '.',
                markersize=point_size,
                frameon=False)
    if kw.get('show_vertices', True):
        ax.plot(vor.vertices[:, 0], vor.vertices[:, 1], 'o', frameon=False)

    line_colors = kw.get('line_colors', 'k')
    line_width = kw.get('line_width', 1.0)
    line_alpha = kw.get('line_alpha', 1.0)

    center = vor.points.mean(axis=0)
    ptp_bound = vor.points.ptp(axis=0)

    finite_segments = []
    infinite_segments = []
    for pointidx, simplex in zip(vor.ridge_points, vor.ridge_vertices):
        simplex = np.asarray(simplex)
        if np.all(simplex >= 0):
            finite_segments.append(vor.vertices[simplex])
        else:
            i = simplex[simplex >= 0][0]  # finite end Voronoi vertex

            t = vor.points[pointidx[1]] - vor.points[pointidx[0]]  # tangent
            t /= np.linalg.norm(t)
            n = np.array([-t[1], t[0]])  # normal

            midpoint = vor.points[pointidx].mean(axis=0)
            direction = np.sign(np.dot(midpoint - center, n)) * n
            # if (vor.furthest_site):  Had to comment this out to bypass error
            if hasattr(vor, 'furthest_site'):
                direction = -direction
            far_point = vor.vertices[i] + direction * ptp_bound.max()

            infinite_segments.append([vor.vertices[i], far_point])

    ax.add_collection(
        LineCollection(finite_segments,
                       colors=line_colors,
                       lw=line_width,
                       alpha=line_alpha,
                       linestyle='solid'))
    ax.add_collection(
        LineCollection(infinite_segments,
                       colors=line_colors,
                       lw=line_width,
                       alpha=line_alpha,
                       linestyle='solid'))

    _adjust_bounds(ax, vor.points)

    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    return ax.figure
Пример #6
0
def cone_rays(angles, which=None, ax=None, fill_args={}):
    """

    Plot the given Voronoi diagram in 2-D based on a set of directions

    Parameters
    ----------
    vor : scipy.spatial.Voronoi instance
        Diagram to plot
    ax : matplotlib.axes.Axes instance, optional
        Axes to plot on

    Returns
    -------
    fig : matplotlib.figure.Figure instance
        Figure for the plot

    See Also
    --------
    Voronoi

    Notes
    -----
    Requires Matplotlib.

    """
    angles = np.sort(angles)
    points = np.array([np.cos(angles), np.sin(angles)]).T

    vor = Voronoi(points)

    if vor.points.shape[1] != 2:
        raise ValueError("Voronoi diagram is not 2-D")

    if ax is None:
        ax = pyplot.gca()

    rays = np.array([(np.cos(angle), np.sin(angle)) for angle in angles])
    for i in range(rays.shape[0]):
        rays[i] /= np.linalg.norm(rays[i])
    rays *= 100

    for ray in rays:
        ax.plot([0, ray[0]], [0, ray[1]], 'k--')

    if which is not None:
        if which < rays.shape[0] - 1:
            active_rays = [rays[which], rays[which + 1]]
        else:
            active_rays = [rays[0], rays[-1]]
        poly = np.vstack([
            active_rays[0],
            np.zeros(2), active_rays[1],
            100 * (active_rays[0] + active_rays[1])
        ])
        dual_rays = np.linalg.pinv(np.array(active_rays))

    else:
        poly = None
        active_rays = None
        dual_rays = None

    _adjust_bounds(ax, vor.points)

    ax.set_xticks([])
    ax.set_yticks([])
    return ax, poly, dual_rays, np.array(active_rays)
Пример #7
0
def cone_rays(angles, ai, hull, which=None, ax=None, fill_args={},
              plot=True):
   """

   Plot the given Voronoi diagram in 2-D based on a set of directions

   Parameters
   ----------
   vor : scipy.spatial.Voronoi instance
       Diagram to plot
   ax : matplotlib.axes.Axes instance, optional
       Axes to plot on

   Returns
   -------
   fig : matplotlib.figure.Figure instance
       Figure for the plot

   See Also
   --------
   Voronoi

   Notes
   -----
   Requires Matplotlib.

   """
   angles = np.sort(angles)
   points = np.array([np.cos(angles), np.sin(angles)]).T
   
   vor = Voronoi(points)
   
   if vor.points.shape[1] != 2:
       raise ValueError("Voronoi diagram is not 2-D")

   if ax is None:
       ax = pyplot.gca()

   rays = np.array([(np.cos(_angle), np.sin(_angle)) for _angle in angles])
   for i in range(rays.shape[0]):
       rays[i] /= np.linalg.norm(rays[i])
   rays *= 100
   
   if plot:
       for ray in rays:
           ax.plot([0,ray[0]],[0,ray[1]], 'k--')
   
   if which is not None:
       if which < rays.shape[0]-1:
           active_rays = [rays[which], rays[which+1]]    
       else:
           active_rays = [rays[0], rays[-1]]
       poly = np.vstack([active_rays[0], np.zeros(2), active_rays[1], 100*(active_rays[0]+active_rays[1])])
       dual_rays = np.linalg.pinv(np.array(active_rays))
       angle1 = angle(dual_rays[:,0])
       angle2 = angle(dual_rays[:,1])

   else:
       poly = None
       active_rays = None
       dual_rays = None

   _adjust_bounds(ax, vor.points)
   
   ax.set_xticks([])
   ax.set_yticks([])
   return ax, poly, dual_rays, np.array(active_rays)