Skip to content

Commit

Permalink
reimplementation of draw_voroinoi (#2608)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
quaquel and pre-commit-ci[bot] authored Jan 10, 2025
1 parent 29d0f3b commit baf5c87
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
5 changes: 1 addition & 4 deletions mesa/experimental/cell_space/voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def __init__(
random: Random | None = None,
cell_klass: type[Cell] = Cell,
capacity_function: callable = round_float,
cell_coloring_property: str | None = None,
) -> None:
"""A Voronoi Tessellation Grid.
Expand All @@ -200,7 +199,7 @@ def __init__(
random (Random): random number generator
cell_klass (type[Cell]): type of cell class
capacity_function (Callable): function to compute (int) capacity according to (float) area
cell_coloring_property (str): voronoi visualization polygon fill property
"""
super().__init__(capacity=capacity, random=random, cell_klass=cell_klass)
self.centroids_coordinates = centroids_coordinates
Expand All @@ -215,7 +214,6 @@ def __init__(
self.triangulation = None
self.voronoi_coordinates = None
self.capacity_function = capacity_function
self.cell_coloring_property = cell_coloring_property

self._connect_cells()
self._build_cell_polygons()
Expand Down Expand Up @@ -266,4 +264,3 @@ def _build_cell_polygons(self):
polygon_area = self._compute_polygon_area(polygon)
self._cells[region].properties["area"] = polygon_area
self._cells[region].capacity = self.capacity_function(polygon_area)
self._cells[region].properties[self.cell_coloring_property] = 0
29 changes: 18 additions & 11 deletions mesa/visualization/mpl_space_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from matplotlib.cm import ScalarMappable
from matplotlib.collections import PatchCollection
from matplotlib.colors import LinearSegmentedColormap, Normalize, to_rgba
from matplotlib.patches import RegularPolygon
from matplotlib.patches import Polygon, RegularPolygon

import mesa
from mesa.experimental.cell_space import (
Expand Down Expand Up @@ -501,14 +501,19 @@ def draw_continuous_space(


def draw_voronoi_grid(
space: VoronoiGrid, agent_portrayal: Callable, ax: Axes | None = None, **kwargs
space: VoronoiGrid,
agent_portrayal: Callable,
ax: Axes | None = None,
draw_grid: bool = True,
**kwargs,
):
"""Visualize a voronoi grid.
Args:
space: the space to visualize
agent_portrayal: a callable that is called with the agent and returns a dict
ax: a Matplotlib Axes instance. If none is provided a new figure and ax will be created using plt.subplots
draw_grid: whether to draw the grid or not
kwargs: additional keyword arguments passed to ax.scatter
Returns:
Expand Down Expand Up @@ -541,16 +546,18 @@ def draw_voronoi_grid(

_scatter(ax, arguments, **kwargs)

for cell in space.all_cells:
polygon = cell.properties["polygon"]
ax.fill(
*zip(*polygon),
alpha=min(1, cell.properties[space.cell_coloring_property]),
c="red",
zorder=0,
) # Plot filled polygon
ax.plot(*zip(*polygon), color="black") # Plot polygon edges in black
def setup_voroinoimesh(cells):
patches = []
for cell in cells:
patch = Polygon(cell.properties["polygon"])
patches.append(patch)
mesh = PatchCollection(
patches, edgecolor="k", facecolor=(1, 1, 1, 0), linestyle="dotted", lw=1
)
return mesh

if draw_grid:
ax.add_collection(setup_voroinoimesh(space.all_cells.cells))
return ax


Expand Down

0 comments on commit baf5c87

Please sign in to comment.