Skip to content

Commit

Permalink
adapt moran.py and test_moran.py to new splot functionality
Browse files Browse the repository at this point in the history
add examples and documentation
  • Loading branch information
slumnitz committed Jul 9, 2018
1 parent 329a195 commit 8fc12e0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 21 deletions.
88 changes: 73 additions & 15 deletions esda/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,17 +1009,28 @@ def plot(self, gdf, attribute, p=0.05,
cmap=cmap,
figsize=figsize)
return fig, axs

def scatterplot(self, p=0.05, **kwargs):
"""Plot Local Moran Scatterplot


def scatterplot(self, zstandard=True, p=0.05,
ax=None, scatter_kwds=None, fitline_kwds=None):
"""
Moran Scatterplot with option of coloring of Local Moran Statistics
Parameters
----------
p : float, optional
The p-value threshold for significance. Points will
be colored by LISA and significance. Default=0.05
**kwargs : keyword arguments, optional
Keywords used for creating and designing the plot.
If given, the p-value threshold for significance. Points will
be colored by significance. By default it will not be colored.
Default =None.
ax : Matplotlib Axes instance, optional
If given, the Moran plot will be created inside this axis.
Default =None.
scatter_kwds : keyword arguments, optional
Keywords used for creating and designing the scatter points.
Default =None.
fitline_kwds : keyword arguments, optional
Keywords used for creating and designing the moran fitline.
Default =None.
Returns
-------
Expand All @@ -1030,6 +1041,24 @@ def scatterplot(self, p=0.05, **kwargs):
Examples
--------
>>> import matplotlib.pyplot as plt
>>> import geopandas as gpd
>>> import libpysal.api as lp
>>> from libpysal import examples
>>> from esda.moran import Moran_Local
Load data and calculate Moran Local statistics
>>> link = examples.get_path('columbus.shp')
>>> gdf = gpd.read_file(link)
>>> y = gdf['HOVAL'].values
>>> w = lp.Queen.from_dataframe(gdf)
>>> w.transform = 'r'
>>> moran_loc = Moran_Local(y, w)
plot
>>> moran_loc.scatterplot()
>>> plt.show()
customize plot
>>> moran_loc.scatterplot(fitline_kwds=dict(color='#4393c3'))
>>> plt.show()
"""
try:
import splot.esda
Expand All @@ -1038,13 +1067,16 @@ def scatterplot(self, p=0.05, **kwargs):
UserWarning)
raise e

fig, ax = splot.esda.moran_loc_scatterplot(self, p=p, **kwargs)
fig, ax = splot.esda.moran_loc_scatterplot(self, zstandard=zstandard, p=p,
ax=ax, scatter_kwds=scatter_kwds,
fitline_kwds=fitline_kwds)
return fig, ax


def LISA_map(self, gdf, p=0.05,
legend=True, **kwargs):
"""Plot LISA cluster map
def lisa_map(self, gdf, p=0.05, ax=None,
legend=True, legend_kwds=None, **kwargs):
"""
Plot LISA cluster map
Parameters
----------
Expand All @@ -1054,12 +1086,19 @@ def LISA_map(self, gdf, p=0.05,
provided `gdf`. (either using gdf.assign() or gdf.copy())
p : float, optional
The p-value threshold for significance. Points will
be colored by significance. Default =0.05
be colored by significance.
ax : matplotlib Axes instance, optional
Axes in which to plot the figure in multiple Axes layout.
Default = None
legend : boolean, optional
If True, legend for maps will be depicted. Default = True
legend_kwds : dict, optional
Dictionary to control legend formatting options. Example:
``legend_kwds={'loc': 'upper left', 'bbox_to_anchor': (0.92, 1.05)}``
Default = None
**kwargs : keyword arguments, optional
Keywords used for creating and designing the plot.
Keywords designing and passed to geopandas.GeoDataFrame.plot().
Returns
-------
fig : matplotlip Figure instance
Expand All @@ -1069,6 +1108,24 @@ def LISA_map(self, gdf, p=0.05,
Examples
--------
>>> import matplotlib.pyplot as plt
>>> import geopandas as gpd
>>> import libpysal.api as lp
>>> from libpysal import examples
>>> from esda.moran import Moran_Local
Load data and calculate Moran Local statistics
>>> link = examples.get_path('columbus.shp')
>>> gdf = gpd.read_file(link)
>>> y = gdf['HOVAL'].values
>>> w = lp.Queen.from_dataframe(gdf)
>>> w.transform = 'r'
>>> moran_loc = Moran_Local(y, w)
plot
>>> moran_loc.lisa_map(gdf)
>>> plt.show()
customize plot
>>> moran_loc.lisa_map(gdf, legend=False)
>>> plt.show()
"""
try:
import splot.esda
Expand All @@ -1077,7 +1134,8 @@ def LISA_map(self, gdf, p=0.05,
UserWarning)
raise e

fig, ax = splot.esda.lisa_cluster(self, gdf, p=p, **kwargs)
fig, ax = splot.esda.lisa_cluster(self, gdf, p=p, ax=ax,
legend=legend, legend_kwds=legend_kwds, **kwargs)
return fig, ax


Expand Down
13 changes: 7 additions & 6 deletions esda/tests/test_moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ def test_plot(self):
y = gdf['HOVAL'].values
w = lp.Queen.from_dataframe(gdf)
w.transform = 'r'
mloc = moran.Moran_Local(y, w)
fig, _ = mloc.plot(gdf, 'HOVAL')
moran_loc = moran.Moran_Local(y, w)
fig, _ = moran_loc.plot(gdf, 'HOVAL')
plt.close(fig)
# also test with quadrant and mask
fig, _ = mloc.plot(gdf, 'HOVAL', p=0.05,
fig, _ = moran_loc.plot(gdf, 'HOVAL', p=0.05,
region_column='POLYID',
mask=['1', '2', '3'], quadrant=1)
plt.close(fig)
Expand All @@ -140,7 +140,7 @@ def test_scatterplot(self):
fig, _ = mloc.scatterplot()
plt.close(fig)
# also test with quadrant and mask
fig, _ = mloc.scatterplot(figsize=(10,20))
fig, _ = mloc.scatterplot(fitline_kwds=dict(color='#4393c3'))
plt.close(fig)


Expand All @@ -155,12 +155,13 @@ def test_LISA_map(self):
w = lp.Queen.from_dataframe(gdf)
w.transform = 'r'
moran_loc = moran.Moran_Local(y, w)
fig, _ = moran_loc.LISA_map(gdf)
fig, _ = moran_loc.lisa_map(gdf)
plt.close(fig)
# also test with quadrant and mask
fig, _ = moran_loc.LISA_map(gdf, figsize=(10,20))
fig, _ = moran_loc.lisa_map(gdf, legend=False)
plt.close(fig)


class Moran_Local_BV_Tester(unittest.TestCase):
def setUp(self):
np.random.seed(10)
Expand Down

0 comments on commit 8fc12e0

Please sign in to comment.