From 33267f642dc603ed0bce37fab9ed30cad3afd4f4 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Wed, 20 Jun 2018 17:52:03 -0700 Subject: [PATCH 01/17] add `.plot()`, `.scatterplot()` and `.LISA_map()` methods to `esda.Moran_Local` object therefore call `splot` functonality --- esda/moran.py | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/esda/moran.py b/esda/moran.py index 8b558524..a607ce29 100644 --- a/esda/moran.py +++ b/esda/moran.py @@ -1006,6 +1006,143 @@ def by_col(cls, df, cols, w=None, inplace=False, pvalue='sim', outvals=None, **s return _univariate_handler(df, cols, w=w, inplace=inplace, pvalue=pvalue, outvals=outvals, stat=cls, swapname=cls.__name__.lower(), **stat_kws) + + def plot(self, gdf, attribute, p=0.05, + region_column=None, mask=None, + mask_color='#636363', quadrant=None, + legend=True, scheme='Quantiles', + cmap='YlGnBu', figsize=(15,4)): + """Produce three-plot visualization of Moran Scatteprlot, + LISA cluster and Choropleth, with Local Moran + region and quadrant masking + + Parameters + ---------- + gdf : geopandas dataframe + The Dataframe containing information to plot the two maps. + attribute : str + Column name of attribute which should be depicted in Choropleth map. + p : float, optional + The p-value threshold for significance. Points and polygons will + be colored by significance. Default = 0.05. + region_column: string, optional + Column name containing mask region of interest. Default = None + mask: str, optional + Identifier or name of the region to highlight. Default = None + mask_color: str, optional + Color of mask. Default = '#636363' + quadrant : int, optional + Quadrant 1-4 in scatterplot masking values in LISA cluster and + Choropleth maps. Default = None + figsize: tuple, optional + W, h of figure. Default = (15,4) + legend: boolean, optional + If True, legend for maps will be depicted. Default = True + scheme: str, optional + Name of PySAL classifier to be used. Default = 'Quantiles' + cmap: str, optional + Name of matplotlib colormap used for plotting the Choropleth. + Default = 'YlGnBU' + + Returns + ------- + fig : matplotlip Figure instance + Figure of LISA cluster map + ax : matplotlib Axes instance + Axes in which the figure is plotted + + Examples + -------- + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method will be available as soon as splot is released in September 2018', + UserWarning) + raise e + + fig, axs = splot.esda.plot_local_autocorrelation(self, gdf=gdf, + attribute=attribute, + p=p, + region_column=region_column, + mask=mask, + mask_color=mask_color, + quadrant=quadrant, + legend=legend, + scheme=scheme, + cmap=cmap, + figsize=figsize) + return fig, axs + + def scatterplot(self, p=0.05, **kwargs): + """Plot Local Moran Scatterplot + + 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. + + Returns + ------- + fig : matplotlip Figure instance + Figure of LISA cluster map + ax : matplotlib Axes instance + Axes in which the figure is plotted + + Examples + -------- + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method will be available as soon as splot is released in September 2018', + UserWarning) + raise e + + fig, ax = splot.esda.moran_loc_scatterplot(self, p=p, **kwargs) + return fig, ax + + + def LISA_map(self, gdf, p=0.05, + legend=True, **kwargs): + """Plot LISA cluster map + + Parameters + ---------- + gdf : geopandas dataframe instance + The Dataframe containing information to plot. Note that `gdf` will be + modified, so calling functions should use a copy of the user + 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 + legend : boolean, optional + If True, legend for maps will be depicted. Default = True + **kwargs : keyword arguments, optional + Keywords used for creating and designing the plot. + + Returns + ------- + fig : matplotlip Figure instance + Figure of LISA cluster map + ax : matplotlib Axes instance + Axes in which the figure is plotted + + Examples + -------- + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method will be available as soon as splot is released in September 2018', + UserWarning) + raise e + + fig, ax = splot.esda.lisa_cluster(self, p=p, **kwargs) + return fig, ax class Moran_Local_BV(object): From 0b02b04e706f97549ba755415c03f917c1d78c17 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Wed, 20 Jun 2018 18:00:11 -0700 Subject: [PATCH 02/17] add test for methods `.plt()`, `.scatterplot()` and `.LISA_map()` fix parameters in `splot.esda.lisa_cluster()` called in `moran.py` --- esda/moran.py | 2 +- esda/tests/test_moran.py | 56 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/esda/moran.py b/esda/moran.py index a607ce29..06b046be 100644 --- a/esda/moran.py +++ b/esda/moran.py @@ -1141,7 +1141,7 @@ def LISA_map(self, gdf, p=0.05, UserWarning) raise e - fig, ax = splot.esda.lisa_cluster(self, p=p, **kwargs) + fig, ax = splot.esda.lisa_cluster(self, gdf, p=p, **kwargs) return fig, ax diff --git a/esda/tests/test_moran.py b/esda/tests/test_moran.py index 055c9106..4413a9ce 100644 --- a/esda/tests/test_moran.py +++ b/esda/tests/test_moran.py @@ -113,6 +113,60 @@ def test_by_col(self): self.assertAlmostEqual(lm.z_z_sim[0], -0.68493799168603808) self.assertAlmostEqual(lm.z_p_z_sim[0], 0.24669152541631179) + def test_plot(self): + import matplotlib.pyplot as plt + import libpysal.api as lp + from libpysal import examples + import geopandas as gpd + link = examples.get_path('columbus.shp') + gdf = gpd.read_file(link) + self.y = gdf['HOVAL'].values + self.w = lp.Queen.from_dataframe(gdf) + self.w.transform = 'r' + self = moran.Moran_Local(self.y, self.w) + fig, _ = moran.Moran_Local.plot(self, gdf, 'HOVAL') + plt.close(fig) + # also test with quadrant and mask + fig, _ = moran.Moran_Local.plot(self, gdf, 'HOVAL', p=0.05, + region_column='POLYID', + mask=['1', '2', '3'], quadrant=1) + plt.close(fig) + + + def test_scatterplot(self): + import matplotlib.pyplot as plt + import libpysal.api as lp + from libpysal import examples + import geopandas as gpd + link = examples.get_path('columbus.shp') + gdf = gpd.read_file(link) + self.y = gdf['HOVAL'].values + self.w = lp.Queen.from_dataframe(gdf) + self.w.transform = 'r' + self = moran.Moran_Local(self.y, self.w) + fig, _ = moran.Moran_Local.scatterplot(self) + plt.close(fig) + # also test with quadrant and mask + fig, _ = moran.Moran_Local.scatterplot(self, figsize=(10,20)) + plt.close(fig) + + + def test_LISA_map(self): + import matplotlib.pyplot as plt + import libpysal.api as lp + from libpysal import examples + import geopandas as gpd + link = examples.get_path('columbus.shp') + gdf = gpd.read_file(link) + self.y = gdf['HOVAL'].values + self.w = lp.Queen.from_dataframe(gdf) + self.w.transform = 'r' + self = moran.Moran_Local(self.y, self.w) + fig, _ = moran.Moran_Local.LISA_map(self, gdf) + plt.close(fig) + # also test with quadrant and mask + fig, _ = moran.Moran_Local.LISA_map(self, gdf, figsize=(10,20)) + plt.close(fig) class Moran_Local_BV_Tester(unittest.TestCase): def setUp(self): @@ -144,7 +198,7 @@ def test_by_col(self): self.assertAlmostEqual(bvz[0], 1.657427, 5) self.assertAlmostEqual(bvzp[0], 0.048717, 5) - + class Moran_Local_Rate_Tester(unittest.TestCase): def setUp(self): np.random.seed(10) From 475628e3244255ceab8f2bf23499cb5dbf4ff069 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Wed, 20 Jun 2018 18:23:01 -0700 Subject: [PATCH 03/17] add `matplotlib` and `splot` install to `.travis.yml` --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 59293df0..2e5151fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,8 @@ install: - conda install --yes --file requirements.txt; - pip install libpysal - if [[ PYSAL_PLUS ]]; then conda install --yes numba; fi + - pip install https://github.com/pysal/splot/archive/master.zip + - pip install matplotlib script: - pwd From f220208064cd64f918037469a257193dfe134eff Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Wed, 20 Jun 2018 18:33:51 -0700 Subject: [PATCH 04/17] add `pip install https://github.com/geopandas/geopandas/archive/master.zip` to `.travis.yml` for testing --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 2e5151fc..80e5dccf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,9 @@ install: - conda install --yes --file requirements.txt; - pip install libpysal - if [[ PYSAL_PLUS ]]; then conda install --yes numba; fi + # We need geopandas master, the legend_kwds is not in 0.3.0 + - pip install https://github.com/geopandas/geopandas/archive/master.zip + # We need splot master, since splot is not released yet - pip install https://github.com/pysal/splot/archive/master.zip - pip install matplotlib From be4e6a0b3090fb3710a861a88104357e50147d17 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Wed, 20 Jun 2018 19:01:30 -0700 Subject: [PATCH 05/17] add `import warnings` to `moran.py` --- esda/moran.py | 1 + 1 file changed, 1 insertion(+) diff --git a/esda/moran.py b/esda/moran.py index 06b046be..5f6e8159 100644 --- a/esda/moran.py +++ b/esda/moran.py @@ -9,6 +9,7 @@ from .tabular import _univariate_handler, _bivariate_handler import scipy.stats as stats import numpy as np +import warnings __all__ = ["Moran", "Moran_Local", "Moran_BV", "Moran_BV_matrix", "Moran_Local_BV", "Moran_Rate", "Moran_Local_Rate"] From 60c458c2ee3fe1037a00c36fa2e05cd51f15f536 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Wed, 20 Jun 2018 23:02:48 -0700 Subject: [PATCH 06/17] debug `test_moran.py` change `self = moran.Moran_Local(self.y, self.w)` to `mloc = moran.Moran_Local(self.y, self.w)` --- esda/tests/test_moran.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/esda/tests/test_moran.py b/esda/tests/test_moran.py index 4413a9ce..bb98d418 100644 --- a/esda/tests/test_moran.py +++ b/esda/tests/test_moran.py @@ -123,11 +123,11 @@ def test_plot(self): self.y = gdf['HOVAL'].values self.w = lp.Queen.from_dataframe(gdf) self.w.transform = 'r' - self = moran.Moran_Local(self.y, self.w) - fig, _ = moran.Moran_Local.plot(self, gdf, 'HOVAL') + mloc = moran.Moran_Local(self.y, self.w) + fig, _ = mloc.plot(self, gdf, 'HOVAL') plt.close(fig) # also test with quadrant and mask - fig, _ = moran.Moran_Local.plot(self, gdf, 'HOVAL', p=0.05, + fig, _ = mloc.plot(self, gdf, 'HOVAL', p=0.05, region_column='POLYID', mask=['1', '2', '3'], quadrant=1) plt.close(fig) @@ -143,11 +143,11 @@ def test_scatterplot(self): self.y = gdf['HOVAL'].values self.w = lp.Queen.from_dataframe(gdf) self.w.transform = 'r' - self = moran.Moran_Local(self.y, self.w) - fig, _ = moran.Moran_Local.scatterplot(self) + mloc = moran.Moran_Local(self.y, self.w) + fig, _ = mloc.scatterplot(self) plt.close(fig) # also test with quadrant and mask - fig, _ = moran.Moran_Local.scatterplot(self, figsize=(10,20)) + fig, _ = mloc.scatterplot(self, figsize=(10,20)) plt.close(fig) @@ -161,11 +161,11 @@ def test_LISA_map(self): self.y = gdf['HOVAL'].values self.w = lp.Queen.from_dataframe(gdf) self.w.transform = 'r' - self = moran.Moran_Local(self.y, self.w) - fig, _ = moran.Moran_Local.LISA_map(self, gdf) + moran_loc = moran.Moran_Local(self.y, self.w) + fig, _ = moran_loc.LISA_map(self, gdf) plt.close(fig) # also test with quadrant and mask - fig, _ = moran.Moran_Local.LISA_map(self, gdf, figsize=(10,20)) + fig, _ = moran_loc.LISA_map(self, gdf, figsize=(10,20)) plt.close(fig) class Moran_Local_BV_Tester(unittest.TestCase): From 1a2308e54c2a0111ffbc6de32b451ae097f44e84 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Thu, 21 Jun 2018 14:22:04 -0700 Subject: [PATCH 07/17] debug `test_moran.py` remove `self` inside test funciton for plots Please enter the commit message for your changes. Lines starting --- esda/tests/test_moran.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/esda/tests/test_moran.py b/esda/tests/test_moran.py index bb98d418..d2e67296 100644 --- a/esda/tests/test_moran.py +++ b/esda/tests/test_moran.py @@ -120,16 +120,16 @@ def test_plot(self): import geopandas as gpd link = examples.get_path('columbus.shp') gdf = gpd.read_file(link) - self.y = gdf['HOVAL'].values - self.w = lp.Queen.from_dataframe(gdf) - self.w.transform = 'r' - mloc = moran.Moran_Local(self.y, self.w) - fig, _ = mloc.plot(self, gdf, 'HOVAL') + y = gdf['HOVAL'].values + w = lp.Queen.from_dataframe(gdf) + w.transform = 'r' + mloc = moran.Moran_Local(y, w) + fig, _ = mloc.plot(gdf, 'HOVAL') plt.close(fig) # also test with quadrant and mask - fig, _ = mloc.plot(self, gdf, 'HOVAL', p=0.05, - region_column='POLYID', - mask=['1', '2', '3'], quadrant=1) + fig, _ = mloc.plot(gdf, 'HOVAL', p=0.05, + region_column='POLYID', + mask=['1', '2', '3'], quadrant=1) plt.close(fig) @@ -140,14 +140,14 @@ def test_scatterplot(self): import geopandas as gpd link = examples.get_path('columbus.shp') gdf = gpd.read_file(link) - self.y = gdf['HOVAL'].values - self.w = lp.Queen.from_dataframe(gdf) - self.w.transform = 'r' - mloc = moran.Moran_Local(self.y, self.w) - fig, _ = mloc.scatterplot(self) + y = gdf['HOVAL'].values + w = lp.Queen.from_dataframe(gdf) + w.transform = 'r' + mloc = moran.Moran_Local(y, w) + fig, _ = mloc.scatterplot() plt.close(fig) # also test with quadrant and mask - fig, _ = mloc.scatterplot(self, figsize=(10,20)) + fig, _ = mloc.scatterplot(figsize=(10,20)) plt.close(fig) @@ -158,14 +158,14 @@ def test_LISA_map(self): import geopandas as gpd link = examples.get_path('columbus.shp') gdf = gpd.read_file(link) - self.y = gdf['HOVAL'].values - self.w = lp.Queen.from_dataframe(gdf) - self.w.transform = 'r' - moran_loc = moran.Moran_Local(self.y, self.w) - fig, _ = moran_loc.LISA_map(self, gdf) + y = gdf['HOVAL'].values + w = lp.Queen.from_dataframe(gdf) + w.transform = 'r' + moran_loc = moran.Moran_Local(y, w) + fig, _ = moran_loc.LISA_map(gdf) plt.close(fig) # also test with quadrant and mask - fig, _ = moran_loc.LISA_map(self, gdf, figsize=(10,20)) + fig, _ = moran_loc.LISA_map(gdf, figsize=(10,20)) plt.close(fig) class Moran_Local_BV_Tester(unittest.TestCase): From 2345dd9689fe081570a8b698bfa0a9c3fc1e3e37 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Tue, 26 Jun 2018 10:55:38 -0700 Subject: [PATCH 08/17] update testing in line 69 to `np.testing.assert_allclose(pval, 0.009, rtol=1e-7, atol=0)` --- esda/tests/test_moran.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esda/tests/test_moran.py b/esda/tests/test_moran.py index d2e67296..8dae84c1 100644 --- a/esda/tests/test_moran.py +++ b/esda/tests/test_moran.py @@ -73,7 +73,7 @@ def test_by_col(self): sidr = np.unique(mi["SID79-BIR79_moran_rate"].values) pval = np.unique(mi["SID79-BIR79_p_sim"].values) np.testing.assert_allclose(sidr, 0.16622343552567395, rtol=RTOL, atol=ATOL) - self.assertAlmostEqual(pval, 0.009) + np.testing.assert_allclose(pval, 0.009, rtol=1e-7, atol=0) From 054d33186d67f6d03bbb612c8296f553c0aadbab Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Tue, 26 Jun 2018 18:03:19 -0700 Subject: [PATCH 09/17] set random seed and change reference `pval` to `0.008` in `test_by_col` --- esda/tests/test_moran.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esda/tests/test_moran.py b/esda/tests/test_moran.py index 8dae84c1..eeb99d5e 100644 --- a/esda/tests/test_moran.py +++ b/esda/tests/test_moran.py @@ -55,6 +55,7 @@ def test_by_col(self): class Moran_Rate_Tester(unittest.TestCase): def setUp(self): + np.random.seed(15432) self.w = pysal.open(pysal.examples.get_path("sids2.gal")).read() f = pysal.open(pysal.examples.get_path("sids2.dbf")) self.e = np.array(f.by_col['SID79']) @@ -73,8 +74,7 @@ def test_by_col(self): sidr = np.unique(mi["SID79-BIR79_moran_rate"].values) pval = np.unique(mi["SID79-BIR79_p_sim"].values) np.testing.assert_allclose(sidr, 0.16622343552567395, rtol=RTOL, atol=ATOL) - np.testing.assert_allclose(pval, 0.009, rtol=1e-7, atol=0) - + np.testing.assert_allclose(pval, 0.008, rtol=RTOL, atol=ATOL) class Moran_BV_matrix_Tester(unittest.TestCase): From c571a2133a32ca244763d8664b034da8b01b0f3e Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Tue, 26 Jun 2018 18:25:09 -0700 Subject: [PATCH 10/17] try to debug TravisCI package install. also change version number to fix a warning in the TravisCI log instal matplotlib with conda --- .travis.yml | 4 ++-- setup.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 80e5dccf..fa911093 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,11 +26,11 @@ install: - conda install --yes --file requirements.txt; - pip install libpysal - if [[ PYSAL_PLUS ]]; then conda install --yes numba; fi + - conda install matplotlib # We need geopandas master, the legend_kwds is not in 0.3.0 - pip install https://github.com/geopandas/geopandas/archive/master.zip # We need splot master, since splot is not released yet - - pip install https://github.com/pysal/splot/archive/master.zip - - pip install matplotlib + - pip install -v https://github.com/pysal/splot/archive/master.zip script: - pwd diff --git a/setup.py b/setup.py index 3c698d00..62793ddb 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from distutils.command.build_py import build_py setup(name='esda', # name of package - version='1.0.1dev', + version='1.0.1.dev0', description='Package with statistics for exploratory spatial data analysis', url='https://github.com/pysal/esda', maintainer='Sergio Rey', From fb19fb3b8e8fe12c8ff34edd3da616ba032f5d70 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Mon, 9 Jul 2018 13:38:28 -0700 Subject: [PATCH 11/17] adapt `moran.py` and `test_moran.py` to new `splot` functionality add examples and documentation --- esda/moran.py | 88 +++++++++++++++++++++++++++++++++------- esda/tests/test_moran.py | 13 +++--- 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/esda/moran.py b/esda/moran.py index 5f6e8159..17848ca5 100644 --- a/esda/moran.py +++ b/esda/moran.py @@ -1054,6 +1054,24 @@ def plot(self, gdf, attribute, 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.scatterplot() + >>> plt.show() + customize plot + >>> moran_loc.scatterplot(fitline_kwds=dict(color='#4393c3')) + >>> plt.show() """ try: import splot.esda @@ -1074,17 +1092,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 ------- @@ -1095,6 +1124,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.lisa_map(gdf) + >>> plt.show() + customize plot + >>> moran_loc.lisa_map(gdf, legend=False) + >>> plt.show() """ try: import splot.esda @@ -1103,13 +1150,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 ---------- @@ -1119,12 +1169,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 @@ -1142,7 +1199,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 diff --git a/esda/tests/test_moran.py b/esda/tests/test_moran.py index eeb99d5e..13f2d4ca 100644 --- a/esda/tests/test_moran.py +++ b/esda/tests/test_moran.py @@ -123,11 +123,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) @@ -147,7 +147,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) @@ -162,12 +162,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) From 734ac4a52672a27eeecd803509479dd59d23cd9d Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Mon, 9 Jul 2018 17:14:08 -0700 Subject: [PATCH 12/17] add `--yes` to `matplotlib` install in `.travis.yml` --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fa911093..950940a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,7 @@ install: - conda install --yes --file requirements.txt; - pip install libpysal - if [[ PYSAL_PLUS ]]; then conda install --yes numba; fi - - conda install matplotlib + - conda install matplotlib --yes # We need geopandas master, the legend_kwds is not in 0.3.0 - pip install https://github.com/geopandas/geopandas/archive/master.zip # We need splot master, since splot is not released yet From a0abd72911548ad0e978ff0d7f27a8244a98ed9a Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Wed, 11 Jul 2018 15:49:06 -0500 Subject: [PATCH 13/17] add env `MPLBACKEND='pdf'` to `.travis.yml` --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 950940a3..48f5a01b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ python: env: - PYSAL_PLUS=false - PYSAL_PLUS=true + - MPLBACKEND='pdf' before_install: - wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh From d6c398858f354a686272e4dcb1e221635c4ffd21 Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Thu, 12 Jul 2018 14:23:39 -0500 Subject: [PATCH 14/17] change position of `MPLBACKEND='pdf'` in `.travis.yml` --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 48f5a01b..79e929f3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,9 +8,8 @@ python: - "3.6" env: - - PYSAL_PLUS=false - - PYSAL_PLUS=true - - MPLBACKEND='pdf' + - PYSAL_PLUS=false MPLBACKEND='pdf' + - PYSAL_PLUS=true MPLBACKEND='pdf' before_install: - wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh From b6e1b267b685f54fcc664c10553dccb370aaa8ea Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Sat, 14 Jul 2018 11:18:25 -0500 Subject: [PATCH 15/17] add `conda install descartes` to `.travis.yml` due to `geopandas` dependency --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 79e929f3..36f2a0c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,8 @@ install: - pip install libpysal - if [[ PYSAL_PLUS ]]; then conda install --yes numba; fi - conda install matplotlib --yes + # we need descartes for `geopandas` dependency + - conda install descartes --yes # We need geopandas master, the legend_kwds is not in 0.3.0 - pip install https://github.com/geopandas/geopandas/archive/master.zip # We need splot master, since splot is not released yet From f15eb1fb87e88f44efb7d516638db23703a6861a Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Sat, 14 Jul 2018 11:54:59 -0500 Subject: [PATCH 16/17] add `conda install mapclassify` to `.travis.yml` fro `geopandas` dependency note: this will only work once the PySAL refactore is released --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 36f2a0c7..c0d91ba1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,7 @@ install: - conda install matplotlib --yes # we need descartes for `geopandas` dependency - conda install descartes --yes + - conda install mapclassify --yes # We need geopandas master, the legend_kwds is not in 0.3.0 - pip install https://github.com/geopandas/geopandas/archive/master.zip # We need splot master, since splot is not released yet From dcca1da85e056f66cbeb3c0b575eb47cb8fc636f Mon Sep 17 00:00:00 2001 From: Stefanie Lumnitz Date: Thu, 9 Aug 2018 15:33:27 -0700 Subject: [PATCH 17/17] add `splot` methods to `Moran_BV`, `Moran`, `Moran_Local` and `Moran_Local_BV` --- esda/moran.py | 555 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 538 insertions(+), 17 deletions(-) diff --git a/esda/moran.py b/esda/moran.py index 17848ca5..4346e72e 100644 --- a/esda/moran.py +++ b/esda/moran.py @@ -258,6 +258,127 @@ def by_col(cls, df, cols, w=None, inplace=False, pvalue='sim', outvals=None, **s return _univariate_handler(df, cols, w=w, inplace=inplace, pvalue=pvalue, outvals=outvals, stat=cls, swapname=cls.__name__.lower(), **stat_kws) + + + def plot(self, zstandard=True, + scatter_kwds=None, + fitline_kwds=None, **kwargs): + """ + Global Moran's I simulated reference distribution and scatterplot. + + Parameters + ---------- + zstandard : bool, optional + If True, Moran Scatterplot will show z-standardized attribute and + spatial lag values. Default =True. + 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 + and vertical fitline. Default =None. + **kwargs : keyword arguments, optional + Keywords used for creating and designing the figure, + passed to seaborne.kdeplot. + + Returns + ------- + fig : Matplotlib Figure instance + Moran scatterplot and reference distribution figure + ax : matplotlib Axes instance + Axes in which the figure is plotted + + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method requires an `splot` installation', + UserWarning) + raise e + + fig, ax = splot.esda.plot_moran(moran=self, zstandard=zstandard, + scatter_kwds=scatter_kwds, + fitline_kwds=fitline_kwds, **kwargs) + return fig, ax + + + def scatterplot(self, zstandard=True, ax=None, + scatter_kwds=None, fitline_kwds=None): + """ + Moran Scatterplot + + Parameters + ---------- + zstandard : bool, optional + If True, Moran Scatterplot will show z-standardized attribute and + spatial lag values. Default =True. + 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 + ------- + fig : Matplotlib Figure instance + Moran scatterplot figure + ax : matplotlib Axes instance + Axes in which the figure is plotted + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method requires an `splot` installation', + UserWarning) + raise e + + fig, ax = splot.esda.moran_scatterplot(moran=self, zstandard=zstandard, + ax=ax, + scatter_kwds=scatter_kwds, + fitline_kwds=fitline_kwds,) + return fig, ax + + + def plot_moran_simulation(self, ax=None, + fitline_kwds=None, **kwargs): + """ + Global Moran's I simulated reference distribution. + + Parameters + ---------- + ax : Matplotlib Axes instance, optional + If given, the Moran plot will be created inside this axis. + Default =None. + fitline_kwds : keyword arguments, optional + Keywords used for creating and designing the + vertical moran fitline. Default =None. + **kwargs : keyword arguments, optional + Keywords used for creating and designing the figure, + passed to seaborn.kdeplot. + + Returns + ------- + fig : Matplotlib Figure instance + Simulated reference distribution figure + ax : matplotlib Axes instance + Axes in which the figure is plotted + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method requires an `splot` installation', + UserWarning) + raise e + + fig, ax = splot.esda.plot_moran_simulation(moran=self, ax=ax, + fitline_kwds=fitline_kwds, + **kwargs) + return fig, ax + class Moran_BV(object): """ @@ -452,6 +573,120 @@ def by_col(cls, df, x, y=None, w=None, inplace=False, pvalue='sim', outvals=None return _bivariate_handler(df, x, y=y, w=w, inplace=inplace, pvalue = pvalue, outvals = outvals, swapname=cls.__name__.lower(), stat=cls,**stat_kws) + + + def plot(self, + scatter_kwds=None, + fitline_kwds=None, **kwargs): + """ + BV Moran's I simulated reference distribution and scatterplot. + + Parameters + ---------- + 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 + and vertical fitline. Default =None. + **kwargs : keyword arguments, optional + Keywords used for creating and designing the figure, + passed to seaborne.kdeplot. + + Returns + ------- + fig : Matplotlib Figure instance + Moran scatterplot and reference distribution figure + ax : matplotlib Axes instance + Axes in which the figure is plotted + + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method requires an `splot` installation', + UserWarning) + raise e + + fig, ax = splot.esda.plot_moran_bv(moran=self, + scatter_kwds=scatter_kwds, + fitline_kwds=fitline_kwds, **kwargs) + return fig, ax + + + def scatterplot(self, ax=None, + scatter_kwds=None, fitline_kwds=None): + """ + Moran Scatterplot + + Parameters + ---------- + 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 + ------- + fig : Matplotlib Figure instance + Moran scatterplot figure + ax : matplotlib Axes instance + Axes in which the figure is plotted + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method requires an `splot` installation', + UserWarning) + raise e + + fig, ax = splot.esda.moran_scatterplot(moran=self, ax=ax, + scatter_kwds=scatter_kwds, + fitline_kwds=fitline_kwds,) + return fig, ax + + + def plot_moran_simulation(self, ax=None, + fitline_kwds=None, **kwargs): + """ + BV Moran's I simulated reference distribution. + + Parameters + ---------- + + ax : Matplotlib Axes instance, optional + If given, the Moran plot will be created inside this axis. + Default =None. + fitline_kwds : keyword arguments, optional + Keywords used for creating and designing the + vertical moran fitline. Default =None. + **kwargs : keyword arguments, optional + Keywords used for creating and designing the figure, + passed to seaborn.kdeplot. + + Returns + ------- + fig : Matplotlib Figure instance + Simulated reference distribution figure + ax : matplotlib Axes instance + Axes in which the figure is plotted + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method requires an `splot` installation', + UserWarning) + raise e + + fig, ax = splot.esda.plot_moran_simulation_bv(moran=self, ax=ax, + fitline_kwds=fitline_kwds, + **kwargs) + return fig, ax def Moran_BV_matrix(variables, w, permutations=0, varnames=None): @@ -561,6 +796,56 @@ def Moran_BV_matrix(variables, w, permutations=0, varnames=None): varnames=varnames) return results +#TODO is this possible? + def plot(self, figsize=(16,12), + scatter_bv_kwds=None, fitline_bv_kwds=None, + scatter_glob_kwds=dict(color='#737373'), fitline_glob_kwds=None): + """ + Moran Facette visualization. + Includes BV Morans and Global Morans on the diagonal. + + Parameters + ---------- + figsize : tuple, optional + W, h of figure. Default =(16,12) + scatter_bv_kwds : keyword arguments, optional + Keywords used for creating and designing the scatter points of + off-diagonal Moran_BV plots. + Default =None. + fitline_bv_kwds : keyword arguments, optional + Keywords used for creating and designing the moran fitline of + off-diagonal Moran_BV plots. + Default =None. + scatter_glob_kwds : keyword arguments, optional + Keywords used for creating and designing the scatter points of + diagonal Moran plots. + Default =None. + fitline_glob_kwds : keyword arguments, optional + Keywords used for creating and designing the moran fitline of + diagonal Moran plots. + Default =None. + + Returns + ------- + fig : Matplotlib Figure instance + Bivariate Moran Local scatterplot figure + axarr : matplotlib Axes instance + Axes in which the figure is plotted + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method requires an `splot` installation', + UserWarning) + raise e + + fig, ax = splot.esda.moran_facet(moran_matrix=self, figsize=figsize, + scatter_bv_kwds=scatter_bv_kwds, + fitline_bv_kwds=fitline_bv_kwds, + scatter_glob_kwds=scatter_glob_kwds, + fitline_glob_kwds=fitline_glob_kwds) + return fig, ax + def _Moran_BV_Matrix_array(variables, w, permutations=0, varnames=None): """ @@ -1012,8 +1297,10 @@ def plot(self, gdf, attribute, p=0.05, region_column=None, mask=None, mask_color='#636363', quadrant=None, legend=True, scheme='Quantiles', - cmap='YlGnBu', figsize=(15,4)): - """Produce three-plot visualization of Moran Scatteprlot, + cmap='YlGnBu', figsize=(15,4), + scatter_kwds=None, fitline_kwds=None): + """ + Produce three-plot visualization of Moran Scatteprlot, LISA cluster and Choropleth, with Local Moran region and quadrant masking @@ -1043,7 +1330,13 @@ def plot(self, gdf, attribute, p=0.05, Name of PySAL classifier to be used. Default = 'Quantiles' cmap: str, optional Name of matplotlib colormap used for plotting the Choropleth. - Default = 'YlGnBU' + Default = 'YlGnBu' + 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 + in the scatterplot. Default =None. Returns ------- @@ -1056,21 +1349,28 @@ def plot(self, gdf, attribute, p=0.05, -------- >>> import matplotlib.pyplot as plt >>> import geopandas as gpd - >>> import libpysal.api as lp + >>> from libpysal.weights.Contiguity import Queen >>> 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 = Queen.from_dataframe(gdf) >>> w.transform = 'r' >>> moran_loc = Moran_Local(y, w) + plot - >>> moran_loc.scatterplot() + + >>> moran_loc.plot(gdf, 'HOVAL') >>> plt.show() + customize plot - >>> moran_loc.scatterplot(fitline_kwds=dict(color='#4393c3')) + + >>> moran_loc.plot(gdf, 'HOVAL', + ... fitline_kwds=dict(color='#4393c3')) >>> plt.show() """ try: @@ -1090,7 +1390,9 @@ def plot(self, gdf, attribute, p=0.05, legend=legend, scheme=scheme, cmap=cmap, - figsize=figsize) + figsize=figsize, + scatter_kwds=scatter_kwds, + fitline_kwds=fitline_kwds) return fig, axs @@ -1126,22 +1428,24 @@ def scatterplot(self, zstandard=True, p=0.05, -------- >>> import matplotlib.pyplot as plt >>> import geopandas as gpd - >>> import libpysal.api as lp + >>> from libpysal.weights.Contiguity import Queen >>> 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 = 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) + + >>> moran_loc.scatterplot() >>> plt.show() + """ try: import splot.esda @@ -1150,9 +1454,9 @@ def scatterplot(self, zstandard=True, p=0.05, UserWarning) raise e - fig, ax = splot.esda.moran_loc_scatterplot(self, zstandard=zstandard, p=p, - ax=ax, scatter_kwds=scatter_kwds, - fitline_kwds=fitline_kwds) + fig, ax = splot.esda.moran_scatterplot(self, zstandard=zstandard, p=p, + ax=ax, scatter_kwds=scatter_kwds, + fitline_kwds=fitline_kwds) return fig, ax @@ -1449,6 +1753,223 @@ def by_col(cls, df, x, y=None, w=None, inplace=False, pvalue='sim', outvals=None pvalue = pvalue, outvals = outvals, swapname=cls.__name__.lower(), stat=cls,**stat_kws) + def plot(self, gdf, attribute, p=0.05, + region_column=None, mask=None, + mask_color='#636363', quadrant=None, + legend=True, scheme='Quantiles', + cmap='YlGnBu', figsize=(15,4), + scatter_kwds=None, fitline_kwds=None): + """ + Produce three-plot visualization of Moran Scatteprlot, + LISA cluster and Choropleth, with Local Moran + region and quadrant masking + + Parameters + ---------- + gdf : geopandas dataframe + The Dataframe containing information to plot the two maps. + attribute : str + Column name of attribute which should be depicted in Choropleth map. + p : float, optional + The p-value threshold for significance. Points and polygons will + be colored by significance. Default = 0.05. + region_column: string, optional + Column name containing mask region of interest. Default = None + mask: str, optional + Identifier or name of the region to highlight. Default = None + mask_color: str, optional + Color of mask. Default = '#636363' + quadrant : int, optional + Quadrant 1-4 in scatterplot masking values in LISA cluster and + Choropleth maps. Default = None + figsize: tuple, optional + W, h of figure. Default = (15,4) + legend: boolean, optional + If True, legend for maps will be depicted. Default = True + scheme: str, optional + Name of PySAL classifier to be used. Default = 'Quantiles' + cmap: str, optional + Name of matplotlib colormap used for plotting the Choropleth. + Default = 'YlGnBu' + 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 + in the scatterplot. Default =None. + + Returns + ------- + fig : matplotlip Figure instance + Multi-view figure. + ax : matplotlib Axes instance + Axes in which the figure is plotted + + Examples + -------- + >>> import matplotlib.pyplot as plt + >>> import geopandas as gpd + >>> from libpysal.weights.Contiguity import Queen + >>> from libpysal import examples + >>> from esda.moran import Moran_Local_BV + + Load data and calculate Moran Local statistics + + >>> link = examples.get_path('columbus.shp') + >>> gdf = gpd.read_file(link) + >>> y = gdf['HOVAL'].values + >>> x = gdf['CRIME'].values + >>> w = Queen.from_dataframe(gdf) + >>> w.transform = 'r' + >>> moran_loc = Moran_Local_BV(y, x, w) + + plot + + >>> moran_loc.plot(gdf, 'HOVAL') + >>> plt.show() + + customize plot + + >>> moran_loc.plot(gdf, 'HOVAL', + ... fitline_kwds=dict(color='#4393c3')) + >>> plt.show() + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method will be available as soon as splot is released in September 2018', + UserWarning) + raise e + + fig, axs = splot.esda.plot_local_autocorrelation(self, gdf=gdf, + attribute=attribute, + p=p, + region_column=region_column, + mask=mask, + mask_color=mask_color, + quadrant=quadrant, + legend=legend, + scheme=scheme, + cmap=cmap, + figsize=figsize, + scatter_kwds=scatter_kwds, + fitline_kwds=fitline_kwds) + return fig, axs + + + def scatterplot(self, zstandard=True, p=0.05, + ax=None, scatter_kwds=None, fitline_kwds=None): + """ + BV Moran Scatterplot with option of coloring of Local Moran Statistics + + Parameters + ---------- + p : float, optional + 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 + ------- + fig : matplotlip Figure instance + Figure of Moran Scatterplot + ax : matplotlib Axes instance + Axes in which the figure is plotted + + Examples + -------- + >>> import matplotlib.pyplot as plt + >>> import geopandas as gpd + >>> from libpysal.weights.Contiguity import Queen + >>> from libpysal import examples + >>> from esda.moran import Moran_Local_BV + + Load data and calculate Moran Local statistics + + >>> link = examples.get_path('columbus.shp') + >>> gdf = gpd.read_file(link) + >>> y = gdf['HOVAL'].values + >>> x = gdf['CRIME'].values + >>> w = Queen.from_dataframe(gdf) + >>> w.transform = 'r' + >>> moran_loc = Moran_Local_BV(y, x, w) + + plot + + >>> moran_loc.scatterplot() + >>> plt.show() + + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method will be available as soon as splot is released in September 2018', + UserWarning) + raise e + + fig, ax = splot.esda.moran_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, ax=None, + legend=True, legend_kwds=None, **kwargs): + """ + Plot LISA cluster map + + Parameters + ---------- + gdf : geopandas dataframe instance + The Dataframe containing information to plot. Note that `gdf` will be + modified, so calling functions should use a copy of the user + provided `gdf`. (either using gdf.assign() or gdf.copy()) + p : float, optional + The p-value threshold for significance. Points will + 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 designing and passed to geopandas.GeoDataFrame.plot(). + + Returns + ------- + fig : matplotlip Figure instance + Figure of LISA cluster map + ax : matplotlib Axes instance + Axes in which the figure is plotted + + Examples + -------- + """ + try: + import splot.esda + except ImportError as e: + warnings.warn('This method will be available as soon as splot is released in September 2018', + UserWarning) + raise e + + fig, ax = splot.esda.lisa_cluster(self, gdf, p=p, ax=ax, + legend=legend, legend_kwds=legend_kwds, **kwargs) + return fig, ax + + class Moran_Local_Rate(Moran_Local): """ Adjusted Local Moran Statistics for Rate Variables [Assuncao1999]_