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

reimplementation of draw_voroinoi #2608

Merged
merged 2 commits into from
Jan 10, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading