Skip to content

Commit fca0d85

Browse files
authored
Improve color handling and documentation of plot_neighborhood (#469)
- Fix bug, that the color could not be set as expected - Forward walkable area options correctly - Improve documentation, state clear what base and default pedestrians are - Add options to control alpha of default, base, and neighbor pedestrians - Add options to print without walkable area
1 parent 65afabb commit fca0d85

1 file changed

Lines changed: 50 additions & 23 deletions

File tree

pedpy/plotting/plotting.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pandas as pd
1414
import shapely
1515
from matplotlib.collections import LineCollection
16-
from matplotlib.colors import Normalize
16+
from matplotlib.colors import Normalize, to_rgb
1717
from matplotlib.patches import Polygon
1818
from mpl_toolkits.axes_grid1 import make_axes_locatable
1919
from numpy.typing import NDArray
@@ -679,7 +679,7 @@ def plot_neighborhood(
679679
neighbors: pd.DataFrame,
680680
frame: int,
681681
voronoi_data: pd.DataFrame,
682-
walkable_area: WalkableArea,
682+
walkable_area: Optional[WalkableArea] = None,
683683
axes: Optional[matplotlib.axes.Axes] = None,
684684
**kwargs: Any,
685685
) -> matplotlib.axes.Axes:
@@ -703,10 +703,20 @@ def plot_neighborhood(
703703
below for list of usable keywords
704704
705705
Keyword Args:
706-
hole_color (optional): color of the holes in the walkable area
707-
base_color (optional): color of the base pedestrians
708-
neighbor_color (optional): color of neighbor pedestrians
709-
default_color (optional): color of default pedestrians
706+
base_color (optional): color of the base pedestrian, whose neighborhood
707+
will be highlighted
708+
base_alpha (optional): alpha of the base pedestrian
709+
neighbor_color (optional): color of neighbor pedestrians of the base
710+
pedestrian
711+
neighbor_alpha (optional): alpha of the neighbor pedestrians
712+
default_color (optional): color of default pedestrians, which are not
713+
neighbors of the base pedestrian
714+
default_alpha (optional): alpha of the default pedestrians
715+
line_color (optional): color of the borders
716+
line_width (optional): line width of the borders
717+
hole_color (optional): background color of holes
718+
hole_alpha (optional): alpha of background color for holes
719+
710720
Returns:
711721
matplotlib.axes.Axes: instances where the neighborhood is plotted
712722
"""
@@ -744,7 +754,7 @@ def _plot_neighborhood(
744754
neighbor_ids: dict[int, List[int]],
745755
frame: int,
746756
voronoi_data: pd.DataFrame,
747-
walkable_area: WalkableArea,
757+
walkable_area: Optional[WalkableArea] = None,
748758
axes: Optional[matplotlib.axes.Axes] = None,
749759
**kwargs: Any,
750760
) -> matplotlib.axes.Axes:
@@ -763,18 +773,30 @@ def _plot_neighborhood(
763773
below for list of usable keywords
764774
765775
Keyword Args:
766-
hole_color (optional): color of the holes in the walkable area
767-
base_color (optional): color of the base pedestrians
768-
neighbor_color (optional): color of neighbor pedestrians
769-
default_color (optional): color of default pedestrians
776+
base_color (optional): color of the base pedestrian, whose neighborhood
777+
will be highlighted
778+
base_alpha (optional): alpha of the base pedestrian
779+
neighbor_color (optional): color of neighbor pedestrians of the base
780+
pedestrian
781+
neighbor_alpha (optional): alpha of the neighbor pedestrians
782+
default_color (optional): color of default pedestrians, which are not
783+
neighbors of the base pedestrian
784+
default_alpha (optional): alpha of the default pedestrians
785+
line_color (optional): color of the borders
786+
line_width (optional): line width of the borders
787+
hole_color (optional): background color of holes
788+
hole_alpha (optional): alpha of background color for holes
789+
770790
Returns:
771791
matplotlib.axes.Axes: instances where the neighborhood is plotted
772792
"""
773793
# Extract color settings from kwargs
774-
hole_color = kwargs.pop("hole_color", "w")
775-
base_color = kwargs.pop("base_color", PEDPY_RED)
776-
neighbor_color = kwargs.pop("neighbor_color", PEDPY_GREEN)
777-
default_color = kwargs.pop("default_color", PEDPY_GREY)
794+
base_color = to_rgb(kwargs.pop("base_color", PEDPY_RED))
795+
base_alpha = kwargs.pop("base_alpha", 0.5)
796+
neighbor_color = to_rgb(kwargs.pop("neighbor_color", PEDPY_GREEN))
797+
neighbor_alpha = kwargs.pop("neighbor_alpha", 0.5)
798+
default_color = to_rgb(kwargs.pop("default_color", PEDPY_GREY))
799+
default_alpha = kwargs.pop("default_alpha", 0.2)
778800

779801
# Filter voronoi_data for polygons in the same frame
780802
voronoi_neighbors = voronoi_data[voronoi_data[FRAME_COL] == frame]
@@ -783,28 +805,33 @@ def _plot_neighborhood(
783805
ped_ids = voronoi_neighbors[ID_COL].to_numpy()
784806
polygons = voronoi_neighbors[POLYGON_COL].to_numpy()
785807
colors = np.full((len(ped_ids), 3), default_color, dtype=float)
786-
alphas = np.full(len(ped_ids), 0.2)
808+
alphas = np.full(len(ped_ids), default_alpha)
787809

788810
# Set base pedestrian color and neighbors colors
789811
for idx, ped_id in enumerate(ped_ids):
790812
if ped_id == pedestrian_id:
791813
colors[idx] = base_color
792-
alphas[idx] = 0.5
814+
alphas[idx] = base_alpha
793815
elif ped_id in neighbor_ids.get(pedestrian_id, []):
794816
colors[idx] = neighbor_color
795-
alphas[idx] = 0.5
817+
alphas[idx] = neighbor_alpha
796818

797819
# Set up the plot
798820
if axes is None:
799821
axes = plt.gca()
800822
axes.set_title(f"Neighbors of pedestrian {pedestrian_id}")
801823

802824
# Plot the walkable area
803-
plot_walkable_area(
804-
axes=axes,
805-
walkable_area=walkable_area,
806-
hole_color=hole_color,
807-
)
825+
if walkable_area is not None:
826+
axes = plot_walkable_area(
827+
axes=axes, walkable_area=walkable_area, **kwargs
828+
)
829+
else:
830+
x_min, y_min, x_max, y_max = shapely.MultiPolygon(polygons).bounds
831+
margin_x = 0.05 * (x_max - x_min)
832+
margin_y = 0.05 * (y_max - y_min)
833+
axes.set_xlim(x_min - margin_x, x_max + margin_x)
834+
axes.set_ylim(y_min - margin_y, y_max + margin_y)
808835

809836
# Plot each polygon with precomputed colors and alphas
810837
for poly, color, alpha in zip(polygons, colors, alphas, strict=False):

0 commit comments

Comments
 (0)