Skip to content

Commit

Permalink
ENH: use representative_point instead of centroid in Graph plotting (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis authored Jul 2, 2024
1 parent 331dfdd commit a5fe4c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
10 changes: 6 additions & 4 deletions libpysal/graph/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def _plot(
Graph to be plotted
gdf : geopandas.GeoDataFrame
Geometries indexed using the same index as Graph. Geometry types other than
points are converted to centroids encoding start and end point of Graph
edges.
points are converted to representative points encoding start and end point of
Graph edges.
focal : hashable | array-like[hashable] | None, optional
ID or an array-like of IDs of focal geometries whose weights shall be
plotted. If None, all weights from all focal geometries are plotted.
Expand Down Expand Up @@ -93,7 +93,9 @@ def _plot(
# we need to work on int position to allow fast filtering of duplicated edges and
# cannot rely on gdf remaining in the same order between Graph creation and
# plotting
coords = shapely.get_coordinates(gdf.reindex(graph_obj.unique_ids).centroid)
coords = shapely.get_coordinates(
gdf.reindex(graph_obj.unique_ids).representative_point()
)

if focal is not None:
if not pd.api.types.is_list_like(focal):
Expand Down Expand Up @@ -188,7 +190,7 @@ def _explore_graph(
folium.Map
folium map
"""
geoms = gdf.centroid.reindex(g.unique_ids)
geoms = gdf.representative_point().reindex(g.unique_ids)

if node_kws is not None:
if "color" not in node_kws:
Expand Down
44 changes: 22 additions & 22 deletions libpysal/graph/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,38 @@ def setup_method(self):
self.expected_paths = [
np.array(
[
[941639.45038754, 150931.99114113],
[941639.45038754, 150931.99114113],
[943802.68511489, 147890.05410767],
[943802.68511489, 147890.05410767],
]
),
np.array(
[
[1034578.07840646, 197116.60422991],
[998769.11468895, 174169.76072687],
[1033983.96582281, 196127.39050293],
[998506.94007314, 177674.69769287],
]
),
np.array(
[
[1034578.07840646, 197116.60422991],
[993336.96493848, 222451.43672456],
[1033983.96582281, 196127.39050293],
[995258.50398262, 226631.05230713],
]
),
np.array(
[
[1034578.07840646, 197116.60422991],
[1021174.78976724, 249937.98006968],
[1033983.96582281, 196127.39050293],
[1021230.82521895, 251186.3362925],
]
),
np.array(
[
[998769.11468895, 174169.76072687],
[993336.96493848, 222451.43672456],
[998506.94007314, 177674.69769287],
[995258.50398262, 226631.05230713],
]
),
np.array(
[
[993336.96493848, 222451.43672456],
[1021174.78976724, 249937.98006968],
[995258.50398262, 226631.05230713],
[1021230.82521895, 251186.3362925],
]
),
]
Expand All @@ -73,7 +73,7 @@ def test_default(self):
pathcollection = ax.collections[1]
np.testing.assert_array_equal(
pathcollection.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid),
shapely.get_coordinates(self.nybb.representative_point()),
)

# edge color
Expand All @@ -98,7 +98,7 @@ def test_string_id(self):
pathcollection = ax.collections[1]
np.testing.assert_array_equal(
pathcollection.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid),
shapely.get_coordinates(self.nybb.representative_point()),
)

def test_misaligned(self):
Expand All @@ -113,7 +113,7 @@ def test_misaligned(self):
pathcollection = ax.collections[1]
np.testing.assert_array_equal(
pathcollection.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid),
shapely.get_coordinates(self.nybb.representative_point()),
)

def test_no_nodes(self):
Expand All @@ -139,13 +139,13 @@ def test_focal(self):
pathcollection = ax.collections[1]
np.testing.assert_array_equal(
pathcollection.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid)[2:],
shapely.get_coordinates(self.nybb.representative_point())[2:],
)

pathcollection_focal = ax.collections[2]
np.testing.assert_array_equal(
pathcollection_focal.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid)[[1]],
shapely.get_coordinates(self.nybb.representative_point())[[1]],
)

def test_focal_array(self):
Expand All @@ -163,13 +163,13 @@ def test_focal_array(self):
pathcollection = ax.collections[1]
np.testing.assert_array_equal(
pathcollection.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid)[1:],
shapely.get_coordinates(self.nybb.representative_point())[1:],
)

pathcollection_focal = ax.collections[2]
np.testing.assert_array_equal(
pathcollection_focal.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid)[[1, -1]],
shapely.get_coordinates(self.nybb.representative_point())[[1, -1]],
)

def test_color(self):
Expand Down Expand Up @@ -214,7 +214,7 @@ def test_ax(self):
pathcollection = ax.collections[2]
np.testing.assert_array_equal(
pathcollection.get_offsets().data,
shapely.get_coordinates(self.nybb.centroid),
shapely.get_coordinates(self.nybb.representative_point()),
)

def test_figsize(self):
Expand All @@ -228,8 +228,8 @@ def test_limit_extent(self):
self.G_str.plot(
self.nybb_str, focal="Bronx", limit_extent=True, ax=ax, color="red"
)
assert ax.get_ylim() == (194475.53543792566, 252579.0488616723)
assert ax.get_xlim() == (991274.9092650851, 1036640.134079854)
assert ax.get_ylim() == (193374.44321345096, 253939.28358198277)
assert ax.get_xlim() == (993322.2308906089, 1035920.2389148154)

def test_focal_kws(self):
ax = self.G_str.plot(
Expand Down

0 comments on commit a5fe4c0

Please sign in to comment.