From 6eb5ea72c0c3c746cf7aa9ab72e6e88c478b75d8 Mon Sep 17 00:00:00 2001 From: Sahil Chhoker Date: Thu, 23 Jan 2025 16:55:53 +0530 Subject: [PATCH 1/2] added conditional checks for empty grid placement --- mesa/visualization/mpl_space_drawing.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/mesa/visualization/mpl_space_drawing.py b/mesa/visualization/mpl_space_drawing.py index 11100e6d104..46cd691c60d 100644 --- a/mesa/visualization/mpl_space_drawing.py +++ b/mesa/visualization/mpl_space_drawing.py @@ -276,8 +276,9 @@ def draw_orthogonal_grid( s_default = (180 / max(space.width, space.height)) ** 2 arguments = collect_agent_data(space, agent_portrayal, size=s_default) - # plot the agents - _scatter(ax, arguments, **kwargs) + if arguments["loc"].size > 0: + # plot the agents + _scatter(ax, arguments, **kwargs) # further styling ax.set_xlim(-0.5, space.width - 0.5) @@ -433,10 +434,12 @@ def draw_network( # this assumes that nodes are identified by an integer # which is true for default nx graphs but might user changeable pos = np.asarray(list(pos.values())) - arguments["loc"] = pos[arguments["loc"]] - # plot the agents - _scatter(ax, arguments, **kwargs) + if arguments["loc"].size > 0: + arguments["loc"] = pos[arguments["loc"]] + + # plot the agents + _scatter(ax, arguments, **kwargs) # further styling ax.set_axis_off() @@ -484,8 +487,9 @@ def draw_continuous_space( s_default = (180 / max(width, height)) ** 2 arguments = collect_agent_data(space, agent_portrayal, size=s_default) - # plot the agents - _scatter(ax, arguments, **kwargs) + if arguments["loc"].size > 0: + # plot the agents + _scatter(ax, arguments, **kwargs) # further visual styling border_style = "solid" if not space.torus else (0, (5, 10)) @@ -544,7 +548,8 @@ def draw_voronoi_grid( ax.set_xlim(x_min - x_padding, x_max + x_padding) ax.set_ylim(y_min - y_padding, y_max + y_padding) - _scatter(ax, arguments, **kwargs) + if arguments["loc"].size > 0: + _scatter(ax, arguments, **kwargs) def setup_voroinoimesh(cells): patches = [] @@ -602,4 +607,4 @@ def _scatter(ax: Axes, arguments, **kwargs): zorder=z_order, **{k: v[logical] for k, v in arguments.items()}, **kwargs, - ) + ) \ No newline at end of file From 4ed34a04249f08e036a634c3faa7fe918024f8de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 23 Jan 2025 11:32:52 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/visualization/mpl_space_drawing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/visualization/mpl_space_drawing.py b/mesa/visualization/mpl_space_drawing.py index 46cd691c60d..14c8e4bc4f1 100644 --- a/mesa/visualization/mpl_space_drawing.py +++ b/mesa/visualization/mpl_space_drawing.py @@ -607,4 +607,4 @@ def _scatter(ax: Axes, arguments, **kwargs): zorder=z_order, **{k: v[logical] for k, v in arguments.items()}, **kwargs, - ) \ No newline at end of file + )