Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: spatial: faster 2D Voronoi and Convex Hull plotting #5061

Merged
merged 3 commits into from
Oct 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 25 additions & 7 deletions scipy/spatial/_plotutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,25 @@ def convex_hull_plot_2d(hull, ax=None):
Requires Matplotlib.

"""
from matplotlib.collections import LineCollection

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

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

line_segments.append([(x, y) for x, y in hull.points[simplex]])
ax.add_collection(LineCollection(line_segments,
colors='k',
linestyle='solid'))
_adjust_bounds(ax, hull.points)

return ax.figure


@_held_figure
def voronoi_plot_2d(vor, ax=None):
def voronoi_plot_2d(vor, ax=None, **kw):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just add show_vertices=True here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it as you suggest initially, but the _held_figure decorator was raising the following error:

TypeError: _held_figure() takes from 2 to 3 positional arguments but 4 were given

"""
Plot the given Voronoi diagram in 2-D

Expand All @@ -117,6 +122,8 @@ def voronoi_plot_2d(vor, ax=None):
Diagram to plot
ax : matplotlib.axes.Axes instance, optional
Axes to plot on
show_vertices : bool, optional
Add the Voronoi vertices to the plot.

Returns
-------
Expand All @@ -132,19 +139,27 @@ def voronoi_plot_2d(vor, ax=None):
Requires Matplotlib.

"""
from matplotlib.collections import LineCollection

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

ax.plot(vor.points[:,0], vor.points[:,1], '.')
ax.plot(vor.vertices[:,0], vor.vertices[:,1], 'o')
if kw.get('show_vertices', True):
ax.plot(vor.vertices[:,0], vor.vertices[:,1], 'o')

line_segments = []
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-')
line_segments.append([(x, y) for x, y in vor.vertices[simplex]])

ax.add_collection(LineCollection(line_segments,
colors='k',
linestyle='solid'))
ptp_bound = vor.points.ptp(axis=0)

line_segments = []
center = vor.points.mean(axis=0)
for pointidx, simplex in zip(vor.ridge_points, vor.ridge_vertices):
simplex = np.asarray(simplex)
Expand All @@ -159,9 +174,12 @@ def voronoi_plot_2d(vor, ax=None):
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--')
line_segments.append([(vor.vertices[i, 0], vor.vertices[i, 1]),
(far_point[0], far_point[1])])

ax.add_collection(LineCollection(line_segments,
colors='k',
linestyle='dashed'))
_adjust_bounds(ax, vor.points)

return ax.figure
2 changes: 2 additions & 0 deletions scipy/spatial/tests/test__plotutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import matplotlib
matplotlib.rcParams['backend'] = 'Agg'
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
has_matplotlib = True
except:
has_matplotlib = False
Expand Down Expand Up @@ -37,6 +38,7 @@ def test_voronoi(self):
r = voronoi_plot_2d(obj, ax=fig.gca())
assert_(r is fig)
voronoi_plot_2d(obj)
voronoi_plot_2d(obj, show_vertices=False)

@dec.skipif(not has_matplotlib, "Matplotlib not available")
def test_convex_hull(self):
Expand Down