diff --git a/libpysal/weights/tests/test_weights.py b/libpysal/weights/tests/test_weights.py index a0bd65d3b..8bd9456d6 100644 --- a/libpysal/weights/tests/test_weights.py +++ b/libpysal/weights/tests/test_weights.py @@ -1,5 +1,6 @@ import os import tempfile +import warnings import unittest import pytest @@ -15,6 +16,20 @@ NPTA3E = np.testing.assert_array_almost_equal +try: + import geopandas + + GEOPANDAS_EXTINCT = False +except ImportError: + GEOPANDAS_EXTINCT = True + +try: + import matplotlib + + MPL_EXTINCT = False +except ImportError: + MPL_EXTINCT = True + class TestW(unittest.TestCase): def setUp(self): @@ -408,6 +423,13 @@ def test_roundtrip_write(self): new = W.from_file(path) np.testing.assert_array_equal(self.w.sparse.toarray(), new.sparse.toarray()) + @unittest.skipIf(GEOPANDAS_EXTINCT or MPL_EXTINCT, "Missing dependencies") + def test_plot(self): + df = geopandas.read_file(examples.get_path("10740.shp")) + with warnings.catch_warnings(record=True) as record: + self.w.plot(df) + assert len(record) == 0 + def test_to_sparse(self): sparse = self.w_islands.to_sparse() np.testing.assert_array_equal(sparse.data, [1, 1, 1, 1, 0]) diff --git a/libpysal/weights/weights.py b/libpysal/weights/weights.py index 2e1434233..275adb51d 100644 --- a/libpysal/weights/weights.py +++ b/libpysal/weights/weights.py @@ -1490,8 +1490,10 @@ def plot( if indexed_on is not None: neighbors = gdf[gdf[indexed_on].isin(neighbors)].index.tolist() idx = gdf[gdf[indexed_on] == idx].index.tolist()[0] - centroids = gdf.loc[neighbors].centroid.apply(lambda p: (p.x, p.y)) - centroids = np.vstack(centroids.values) + else: + neighbors = list(neighbors) + centroids = gdf.loc[neighbors].centroid + centroids = np.stack([centroids.x, centroids.y], axis=1) focal = np.hstack(gdf.loc[idx].geometry.centroid.xy) seen = set() for nidx, neighbor in zip(neighbors, centroids): @@ -1500,11 +1502,8 @@ def plot( ax.plot(*list(zip(focal, neighbor)), marker=None, **edge_kws) seen.update((idx, nidx)) seen.update((nidx, idx)) - ax.scatter( - gdf.centroid.apply(lambda p: p.x), - gdf.centroid.apply(lambda p: p.y), - **node_kws, - ) + centroids = gdf.centroid + ax.scatter(centroids.x, centroids.y, **node_kws) return f, ax