Skip to content

Commit 8fc12e0

Browse files
committed
adapt moran.py and test_moran.py to new splot functionality
add examples and documentation
1 parent 329a195 commit 8fc12e0

File tree

2 files changed

+80
-21
lines changed

2 files changed

+80
-21
lines changed

esda/moran.py

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,17 +1009,28 @@ def plot(self, gdf, attribute, p=0.05,
10091009
cmap=cmap,
10101010
figsize=figsize)
10111011
return fig, axs
1012-
1013-
def scatterplot(self, p=0.05, **kwargs):
1014-
"""Plot Local Moran Scatterplot
1012+
1013+
1014+
def scatterplot(self, zstandard=True, p=0.05,
1015+
ax=None, scatter_kwds=None, fitline_kwds=None):
1016+
"""
1017+
Moran Scatterplot with option of coloring of Local Moran Statistics
10151018
10161019
Parameters
10171020
----------
10181021
p : float, optional
1019-
The p-value threshold for significance. Points will
1020-
be colored by LISA and significance. Default=0.05
1021-
**kwargs : keyword arguments, optional
1022-
Keywords used for creating and designing the plot.
1022+
If given, the p-value threshold for significance. Points will
1023+
be colored by significance. By default it will not be colored.
1024+
Default =None.
1025+
ax : Matplotlib Axes instance, optional
1026+
If given, the Moran plot will be created inside this axis.
1027+
Default =None.
1028+
scatter_kwds : keyword arguments, optional
1029+
Keywords used for creating and designing the scatter points.
1030+
Default =None.
1031+
fitline_kwds : keyword arguments, optional
1032+
Keywords used for creating and designing the moran fitline.
1033+
Default =None.
10231034
10241035
Returns
10251036
-------
@@ -1030,6 +1041,24 @@ def scatterplot(self, p=0.05, **kwargs):
10301041
10311042
Examples
10321043
--------
1044+
>>> import matplotlib.pyplot as plt
1045+
>>> import geopandas as gpd
1046+
>>> import libpysal.api as lp
1047+
>>> from libpysal import examples
1048+
>>> from esda.moran import Moran_Local
1049+
Load data and calculate Moran Local statistics
1050+
>>> link = examples.get_path('columbus.shp')
1051+
>>> gdf = gpd.read_file(link)
1052+
>>> y = gdf['HOVAL'].values
1053+
>>> w = lp.Queen.from_dataframe(gdf)
1054+
>>> w.transform = 'r'
1055+
>>> moran_loc = Moran_Local(y, w)
1056+
plot
1057+
>>> moran_loc.scatterplot()
1058+
>>> plt.show()
1059+
customize plot
1060+
>>> moran_loc.scatterplot(fitline_kwds=dict(color='#4393c3'))
1061+
>>> plt.show()
10331062
"""
10341063
try:
10351064
import splot.esda
@@ -1038,13 +1067,16 @@ def scatterplot(self, p=0.05, **kwargs):
10381067
UserWarning)
10391068
raise e
10401069

1041-
fig, ax = splot.esda.moran_loc_scatterplot(self, p=p, **kwargs)
1070+
fig, ax = splot.esda.moran_loc_scatterplot(self, zstandard=zstandard, p=p,
1071+
ax=ax, scatter_kwds=scatter_kwds,
1072+
fitline_kwds=fitline_kwds)
10421073
return fig, ax
10431074

10441075

1045-
def LISA_map(self, gdf, p=0.05,
1046-
legend=True, **kwargs):
1047-
"""Plot LISA cluster map
1076+
def lisa_map(self, gdf, p=0.05, ax=None,
1077+
legend=True, legend_kwds=None, **kwargs):
1078+
"""
1079+
Plot LISA cluster map
10481080
10491081
Parameters
10501082
----------
@@ -1054,12 +1086,19 @@ def LISA_map(self, gdf, p=0.05,
10541086
provided `gdf`. (either using gdf.assign() or gdf.copy())
10551087
p : float, optional
10561088
The p-value threshold for significance. Points will
1057-
be colored by significance. Default =0.05
1089+
be colored by significance.
1090+
ax : matplotlib Axes instance, optional
1091+
Axes in which to plot the figure in multiple Axes layout.
1092+
Default = None
10581093
legend : boolean, optional
10591094
If True, legend for maps will be depicted. Default = True
1095+
legend_kwds : dict, optional
1096+
Dictionary to control legend formatting options. Example:
1097+
``legend_kwds={'loc': 'upper left', 'bbox_to_anchor': (0.92, 1.05)}``
1098+
Default = None
10601099
**kwargs : keyword arguments, optional
1061-
Keywords used for creating and designing the plot.
1062-
1100+
Keywords designing and passed to geopandas.GeoDataFrame.plot().
1101+
10631102
Returns
10641103
-------
10651104
fig : matplotlip Figure instance
@@ -1069,6 +1108,24 @@ def LISA_map(self, gdf, p=0.05,
10691108
10701109
Examples
10711110
--------
1111+
>>> import matplotlib.pyplot as plt
1112+
>>> import geopandas as gpd
1113+
>>> import libpysal.api as lp
1114+
>>> from libpysal import examples
1115+
>>> from esda.moran import Moran_Local
1116+
Load data and calculate Moran Local statistics
1117+
>>> link = examples.get_path('columbus.shp')
1118+
>>> gdf = gpd.read_file(link)
1119+
>>> y = gdf['HOVAL'].values
1120+
>>> w = lp.Queen.from_dataframe(gdf)
1121+
>>> w.transform = 'r'
1122+
>>> moran_loc = Moran_Local(y, w)
1123+
plot
1124+
>>> moran_loc.lisa_map(gdf)
1125+
>>> plt.show()
1126+
customize plot
1127+
>>> moran_loc.lisa_map(gdf, legend=False)
1128+
>>> plt.show()
10721129
"""
10731130
try:
10741131
import splot.esda
@@ -1077,7 +1134,8 @@ def LISA_map(self, gdf, p=0.05,
10771134
UserWarning)
10781135
raise e
10791136

1080-
fig, ax = splot.esda.lisa_cluster(self, gdf, p=p, **kwargs)
1137+
fig, ax = splot.esda.lisa_cluster(self, gdf, p=p, ax=ax,
1138+
legend=legend, legend_kwds=legend_kwds, **kwargs)
10811139
return fig, ax
10821140

10831141

esda/tests/test_moran.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ def test_plot(self):
116116
y = gdf['HOVAL'].values
117117
w = lp.Queen.from_dataframe(gdf)
118118
w.transform = 'r'
119-
mloc = moran.Moran_Local(y, w)
120-
fig, _ = mloc.plot(gdf, 'HOVAL')
119+
moran_loc = moran.Moran_Local(y, w)
120+
fig, _ = moran_loc.plot(gdf, 'HOVAL')
121121
plt.close(fig)
122122
# also test with quadrant and mask
123-
fig, _ = mloc.plot(gdf, 'HOVAL', p=0.05,
123+
fig, _ = moran_loc.plot(gdf, 'HOVAL', p=0.05,
124124
region_column='POLYID',
125125
mask=['1', '2', '3'], quadrant=1)
126126
plt.close(fig)
@@ -140,7 +140,7 @@ def test_scatterplot(self):
140140
fig, _ = mloc.scatterplot()
141141
plt.close(fig)
142142
# also test with quadrant and mask
143-
fig, _ = mloc.scatterplot(figsize=(10,20))
143+
fig, _ = mloc.scatterplot(fitline_kwds=dict(color='#4393c3'))
144144
plt.close(fig)
145145

146146

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

164+
164165
class Moran_Local_BV_Tester(unittest.TestCase):
165166
def setUp(self):
166167
np.random.seed(10)

0 commit comments

Comments
 (0)