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

(bug) support graph in moran's viz #184

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
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
36 changes: 29 additions & 7 deletions splot/_viz_esda_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import seaborn as sbn
from esda.moran import Moran, Moran_BV, Moran_Local, Moran_Local_BV
from libpysal.weights.spatial_lag import lag_spatial
from libpysal.weights import W
from matplotlib import colors, patches
from spreg import OLS

Expand Down Expand Up @@ -275,7 +276,10 @@ def _moran_global_scatterplot(

# plot and set standards
if zstandard is True:
lag = lag_spatial(moran.w, moran.z)
if isinstance(moran.w, W):
lag = lag_spatial(moran.w, moran.z)
else:
lag = moran.w.lag(moran.z)
fit = OLS(moran.z[:, None], lag[:, None])
# plot
ax.scatter(moran.z, lag, **scatter_kwds)
Expand All @@ -284,7 +288,10 @@ def _moran_global_scatterplot(
ax.axvline(0, alpha=0.5, color="k", linestyle="--")
ax.axhline(0, alpha=0.5, color="k", linestyle="--")
else:
lag = lag_spatial(moran.w, moran.y)
if isinstance(moran.w, W):
lag = lag_spatial(moran.w, moran.y)
else:
lag = moran.w.lag(moran.y)
b, a = numpy.polyfit(moran.y, lag, 1)
# plot
ax.scatter(moran.y, lag, **scatter_kwds)
Expand Down Expand Up @@ -566,7 +573,10 @@ def _moran_bv_scatterplot(
ax.set_title("Bivariate Moran Scatterplot" + " (" + str(round(moran_bv.I, 2)) + ")")

# plot and set standards
lag = lag_spatial(moran_bv.w, moran_bv.zy)
if isinstance(moran_bv.w, W):
lag = lag_spatial(moran_bv.w, moran_bv.zy)
else:
lag = moran_bv.w.lag(moran_bv.zy)
fit = OLS(moran_bv.zy[:, None], lag[:, None])
# plot
ax.scatter(moran_bv.zx, lag, **scatter_kwds)
Expand Down Expand Up @@ -862,7 +872,10 @@ def _moran_loc_scatterplot(

# plot and set standards
if zstandard is True:
lag = lag_spatial(moran_loc.w, moran_loc.z)
if isinstance(moran_loc.w, W):
lag = lag_spatial(moran_loc.w, moran_loc.z)
else:
lag = moran_loc.w.lag(moran_loc.z)
fit = OLS(moran_loc.z[:, None], lag[:, None])
# v- and hlines
ax.axvline(0, alpha=0.5, color="k", linestyle="--")
Expand All @@ -879,7 +892,10 @@ def _moran_loc_scatterplot(
ax.plot(lag, fit.predy, **fitline_kwds)
ax.scatter(moran_loc.z, fit.predy, **scatter_kwds)
else:
lag = lag_spatial(moran_loc.w, moran_loc.y)
if isinstance(moran_loc.w, W):
lag = lag_spatial(moran_loc.w, moran_loc.y)
else:
lag = moran_loc.w.lag(moran_loc.y)
b, a = numpy.polyfit(moran_loc.y, lag, 1)
# dashed vert at mean of the attribute
ax.vlines(moran_loc.y.mean(), lag.min(), lag.max(), alpha=0.5, linestyle="--")
Expand Down Expand Up @@ -1217,7 +1233,10 @@ def plot_local_autocorrelation(

df_mask = gdf[ix]
x_mask = moran_loc.z[ix]
y_mask = lag_spatial(moran_loc.w, moran_loc.z)[ix]
if isinstance(moran_loc.w, W):
y_mask = lag_spatial(moran_loc.w, moran_loc.z)[ix]
else:
y_mask = moran_loc.w.lag(moran_loc.z)[ix]
axs[0].plot(
x_mask,
y_mask,
Expand Down Expand Up @@ -1348,7 +1367,10 @@ def _moran_loc_bv_scatterplot(
ax.set_title("Moran BV Local Scatterplot")

# plot and set standards
lag = lag_spatial(moran_loc_bv.w, moran_loc_bv.zy)
if isinstance(moran_loc_bv.w, W):
lag = lag_spatial(moran_loc_bv.w, moran_loc_bv.zy)
else:
lag = moran_loc_bv.w.lag(moran_loc_bv.zy)
fit = OLS(moran_loc_bv.zy[:, None], lag[:, None])
# v- and hlines
ax.axvline(0, alpha=0.5, color="k", linestyle="--")
Expand Down
Loading