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

move to splot implementation of gdf plotting #231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
45 changes: 7 additions & 38 deletions libpysal/weights/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,44 +1247,13 @@ def plot(self, gdf, indexed_on=None, ax=None, color='k',
raise ImportError("W.plot depends on matplotlib.pyplot, and this was"
"not able to be imported. \nInstall matplotlib to"
"plot spatial weights.")
if ax is None:
f = plt.figure()
ax = plt.gca()
else:
f = plt.gcf()
if node_kws is not None:
if 'color' not in node_kws:
node_kws['color'] = color
else:
node_kws=dict(color=color)
if edge_kws is not None:
if 'color' not in edge_kws:
edge_kws['color'] = color
else:
edge_kws=dict(color=color)

for idx, neighbors in self:
if idx in self.islands:
continue
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)
focal = np.hstack(gdf.loc[idx].geometry.centroid.xy)
seen = set()
for nidx, neighbor in zip(neighbors, centroids):
if (idx,nidx) in seen:
continue
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)
return f,ax

from pysal.viz.splot.libpysal import plot_spatial_weights
node_kws = dict() if node_kws is None else node_kws
edge_kws = dict() if edge_kws is None else edge_kws
node_kws.setdefault('color', color)
edge_kws.setdefault('color', color)
return plot_spatial_weights(self, gdf, indexed_on=indexed_on, ax=ax,
node_kws=node_kws, edge_kws=edge_kws)

class WSP(object):

Expand Down