Skip to content

Commit baf5c87

Browse files
reimplementation of draw_voroinoi (#2608)
* reimplementation of draw_voroinoi * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 29d0f3b commit baf5c87

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

mesa/experimental/cell_space/voronoi.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ def __init__(
186186
random: Random | None = None,
187187
cell_klass: type[Cell] = Cell,
188188
capacity_function: callable = round_float,
189-
cell_coloring_property: str | None = None,
190189
) -> None:
191190
"""A Voronoi Tessellation Grid.
192191
@@ -200,7 +199,7 @@ def __init__(
200199
random (Random): random number generator
201200
cell_klass (type[Cell]): type of cell class
202201
capacity_function (Callable): function to compute (int) capacity according to (float) area
203-
cell_coloring_property (str): voronoi visualization polygon fill property
202+
204203
"""
205204
super().__init__(capacity=capacity, random=random, cell_klass=cell_klass)
206205
self.centroids_coordinates = centroids_coordinates
@@ -215,7 +214,6 @@ def __init__(
215214
self.triangulation = None
216215
self.voronoi_coordinates = None
217216
self.capacity_function = capacity_function
218-
self.cell_coloring_property = cell_coloring_property
219217

220218
self._connect_cells()
221219
self._build_cell_polygons()
@@ -266,4 +264,3 @@ def _build_cell_polygons(self):
266264
polygon_area = self._compute_polygon_area(polygon)
267265
self._cells[region].properties["area"] = polygon_area
268266
self._cells[region].capacity = self.capacity_function(polygon_area)
269-
self._cells[region].properties[self.cell_coloring_property] = 0

mesa/visualization/mpl_space_drawing.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from matplotlib.cm import ScalarMappable
2121
from matplotlib.collections import PatchCollection
2222
from matplotlib.colors import LinearSegmentedColormap, Normalize, to_rgba
23-
from matplotlib.patches import RegularPolygon
23+
from matplotlib.patches import Polygon, RegularPolygon
2424

2525
import mesa
2626
from mesa.experimental.cell_space import (
@@ -501,14 +501,19 @@ def draw_continuous_space(
501501

502502

503503
def draw_voronoi_grid(
504-
space: VoronoiGrid, agent_portrayal: Callable, ax: Axes | None = None, **kwargs
504+
space: VoronoiGrid,
505+
agent_portrayal: Callable,
506+
ax: Axes | None = None,
507+
draw_grid: bool = True,
508+
**kwargs,
505509
):
506510
"""Visualize a voronoi grid.
507511
508512
Args:
509513
space: the space to visualize
510514
agent_portrayal: a callable that is called with the agent and returns a dict
511515
ax: a Matplotlib Axes instance. If none is provided a new figure and ax will be created using plt.subplots
516+
draw_grid: whether to draw the grid or not
512517
kwargs: additional keyword arguments passed to ax.scatter
513518
514519
Returns:
@@ -541,16 +546,18 @@ def draw_voronoi_grid(
541546

542547
_scatter(ax, arguments, **kwargs)
543548

544-
for cell in space.all_cells:
545-
polygon = cell.properties["polygon"]
546-
ax.fill(
547-
*zip(*polygon),
548-
alpha=min(1, cell.properties[space.cell_coloring_property]),
549-
c="red",
550-
zorder=0,
551-
) # Plot filled polygon
552-
ax.plot(*zip(*polygon), color="black") # Plot polygon edges in black
549+
def setup_voroinoimesh(cells):
550+
patches = []
551+
for cell in cells:
552+
patch = Polygon(cell.properties["polygon"])
553+
patches.append(patch)
554+
mesh = PatchCollection(
555+
patches, edgecolor="k", facecolor=(1, 1, 1, 0), linestyle="dotted", lw=1
556+
)
557+
return mesh
553558

559+
if draw_grid:
560+
ax.add_collection(setup_voroinoimesh(space.all_cells.cells))
554561
return ax
555562

556563

0 commit comments

Comments
 (0)