def _calc(self, Y, w, k): wY = lag_spatial(w, Y) dx = Y[:, -1] - Y[:, 0] dy = wY[:, -1] - wY[:, 0] self.wY = wY self.Y = Y r = np.sqrt(dx * dx + dy * dy) theta = np.arctan2(dy, dx) neg = theta < 0.0 utheta = theta * (1 - neg) + neg * (2 * np.pi + theta) counts, bins = np.histogram(utheta, self.cuts) results = {} results['counts'] = counts results['theta'] = theta results['bins'] = bins results['r'] = r results['lag'] = wY results['dx'] = dx results['dy'] = dy return results
def moran_loc_bv_scatterplot(moran_loc_bv, p=None, ax=None, scatter_kwds=None, fitline_kwds=None): """ Moran Bivariate Scatterplot with option of coloring of Local Moran Statistics Parameters ---------- moran_loc : esda.moran.Moran_Local_BV instance Values of Moran's I Local Autocorrelation Statistics 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 : Matplotlib Figure instance Bivariate Moran Local scatterplot figure ax : matplotlib Axes instance Axes in which the figure is plotted Examples -------- Imports >>> import matplotlib.pyplot as plt >>> import geopandas as gpd >>> import libpysal.api as lp >>> from libpysal import examples >>> from esda.moran import Moran_Local_BV >>> from splot.esda import moran_loc_bv_scatterplot Load data and calculate Moran Local statistics >>> link = examples.get_path('Guerry.shp') >>> gdf = gpd.read_file(link) >>> x = gdf['Suicids'].values >>> y = gdf['Donatns'].values >>> w = lp.Queen.from_dataframe(gdf) >>> w.transform = 'r' >>> m = Moran_Local_BV(x, y, w) Plot >>> moran_loc_bv_scatterplot(m) Customize plot >>> moran_loc_bv_scatterplot(m, p=0.05, ... fitline_kwds=dict(color='#4393c3'))) >>> plt.show() """ # to set default as an empty dictionary that is later filled with defaults if scatter_kwds is None: scatter_kwds = dict() if fitline_kwds is None: fitline_kwds = dict() if p is not None: if not isinstance(moran_loc_bv, Moran_Local_BV): raise ValueError("`moran_loc_bv` is not a\n" + "esda.moran.Moran_Local_BV instance") if 'color' in scatter_kwds or 'cmap' in scatter_kwds: warnings.warn("To change the color use cmap with a colormap of 5,\n" + "c defines the LISA category, color will interfere with c") # colors spots_bv = moran_hot_cold_spots(moran_loc_bv, p) hmap = colors.ListedColormap(['#bababa', '#d7191c', '#abd9e9', '#2c7bb6', '#fdae61']) # define customization scatter_kwds.setdefault('alpha', 0.6) scatter_kwds.setdefault('s', 40) fitline_kwds.setdefault('alpha', 0.9) # get fig and ax fig, ax = _create_moran_fig_ax(ax, figsize=(7,7)) # set labels ax.set_xlabel('Attribute') ax.set_ylabel('Spatial Lag') ax.set_title('Moran BV Local Scatterplot') # plot and set standards lag = lp.lag_spatial(moran_loc_bv.w, 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='--') ax.axhline(0, alpha=0.5, color='k', linestyle='--') if p is not None: fitline_kwds.setdefault('color', 'k') scatter_kwds.setdefault('cmap', hmap) scatter_kwds.setdefault('c', spots_bv) ax.plot(lag, fit.predy, **fitline_kwds) ax.scatter(moran_loc_bv.zx, fit.predy, **scatter_kwds) else: scatter_kwds.setdefault('color', splot_colors['moran_base']) fitline_kwds.setdefault('color', splot_colors['moran_fit']) ax.plot(lag, fit.predy, **fitline_kwds) ax.scatter(moran_loc_bv.zy, fit.predy, **scatter_kwds) return fig, ax
def moran_scatterplot(moran, zstandard=True, ax=None, scatter_kwds=None, fitline_kwds=None): """ Global Moran's I Scatterplot. Parameters ---------- moran : esda.moran.Moran instance Values of Moran's I Global Autocorrelation Statistics 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 Examples -------- Imports >>> import matplotlib.pyplot as plt >>> import libpysal.api as lp >>> from libpysal import examples >>> import geopandas as gpd >>> from esda.moran import Moran >>> from splot.esda import moran_scatterplot Load data and calculate weights >>> link_to_data = examples.get_path('Guerry.shp') >>> gdf = gpd.read_file(link_to_data) >>> y = gdf['Donatns'].values >>> w = lp.Queen.from_dataframe(gdf) >>> w.transform = 'r' Calculate Global Moran >>> moran = Moran(y, w) plot >>> moran_scatterplot(moran) >>> plt.show() customize plot >>> fig, ax = moran_scatterplot(moran, zstandard=False, ... fitline_kws=dict(color='#4393c3')) >>> ax.set_xlabel('Donations') >>> plt.show() """ # to set default as an empty dictionary that is later filled with defaults if scatter_kwds is None: scatter_kwds = dict() if fitline_kwds is None: fitline_kwds = dict() # define customization defaults scatter_kwds.setdefault('alpha', 0.6) scatter_kwds.setdefault('color', splot_colors['moran_base']) scatter_kwds.setdefault('s', 40) fitline_kwds.setdefault('alpha', 0.9) fitline_kwds.setdefault('color', splot_colors['moran_fit']) # get fig and ax fig, ax = _create_moran_fig_ax(ax, figsize=(7, 7)) # set labels ax.set_xlabel('Attribute') ax.set_ylabel('Spatial Lag') ax.set_title('Moran Scatterplot' + ' (' + str(round(moran.I, 2)) + ')') # plot and set standards if zstandard is True: lag = lp.lag_spatial(moran.w, moran.z) fit = OLS(moran.z[:, None], lag[:, None]) # plot ax.scatter(moran.z, lag, **scatter_kwds) ax.plot(lag, fit.predy, **fitline_kwds) # v- and hlines ax.axvline(0, alpha=0.5, color='k', linestyle='--') ax.axhline(0, alpha=0.5, color='k', linestyle='--') else: lag = lp.lag_spatial(moran.w, moran.y) b, a = np.polyfit(moran.y, lag, 1) # plot ax.scatter(moran.y, lag, **scatter_kwds) ax.plot(moran.y, a + b*moran.y, **fitline_kwds) # dashed vert at mean of the attribute ax.vlines(moran.y.mean(), lag.min(), lag.max(), alpha=0.5, linestyle='--') # dashed horizontal at mean of lagged attribute ax.hlines(lag.mean(), moran.y.min(), moran.y.max(), alpha=0.5, linestyle='--') return fig, ax
def plot_local_autocorrelation(moran_loc, 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 visualisation of Moran Scatteprlot, LISA cluster and Choropleth maps, with Local Moran region and quadrant masking Parameters ---------- moran_loc : esda.moran.Moran_Local instance Values of Moran's Local Autocorrelation Statistic 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 : Matplotlib figure instance Moran Scatterplot, LISA cluster map and Choropleth. axs : list of Matplotlib axes Lisat of Matplotlib axes plotted. Examples -------- Imports >>> import matplotlib.pyplot as plt >>> import libpysal.api as lp >>> from libpysal import examples >>> import geopandas as gpd >>> from esda.moran import Moran_Local >>> from splot.esda import plot_local_autocorrelation Data preparation and analysis >>> link = examples.get_path('Guerry.shp') >>> gdf = gpd.read_file(link) >>> y = gdf['Donatns'].values >>> w = lp.Queen.from_dataframe(gdf) >>> w.transform = 'r' >>> moran_loc = Moran_Local(y, w) Plotting with quadrant mask and region mask >>> fig = plot_local_autocorrelation(moran_loc, gdf, 'HOVAL', p=0.05, ... region_column='POLYID', ... mask=['1', '2', '3'], quadrant=1) >>> plt.show() ''' fig, axs = plt.subplots(1, 3, figsize=figsize, subplot_kw={'aspect': 'equal'}) # Moran Scatterplot if isinstance (moran_loc, Moran_Local): moran_loc_scatterplot(moran_loc, p=p, ax=axs[0], scatter_kwds=scatter_kwds, fitline_kwds=fitline_kwds) else: moran_loc_bv_scatterplot(moran_loc, p=p, ax=axs[0], scatter_kwds=scatter_kwds, fitline_kwds=fitline_kwds) axs[0].set_aspect('auto') # Lisa cluster map # TODO: Fix legend_kwds: display boxes instead of points lisa_cluster(moran_loc, gdf, p=p, ax=axs[1], legend=legend, legend_kwds={'loc': 'upper left', 'bbox_to_anchor': (0.92, 1.05)}) axs[1].set_aspect('equal') # Choropleth for attribute gdf.plot(column=attribute, scheme=scheme, cmap=cmap, legend=legend, legend_kwds={'loc': 'upper left', 'bbox_to_anchor': (0.92, 1.05)}, ax=axs[2], alpha=1) axs[2].set_axis_off() axs[2].set_aspect('equal') # MASKING QUADRANT VALUES if quadrant is not None: # Quadrant masking in Scatterplot mask_angles = {1: 0, 2: 90, 3: 180, 4: 270} # rectangle angles # We don't want to change the axis data limits, so use the current ones xmin, xmax = axs[0].get_xlim() ymin, ymax = axs[0].get_ylim() # We are rotating, so we start from 0 degrees and # figured out the right dimensions for the rectangles for other angles mask_width = {1: abs(xmax), 2: abs(ymax), 3: abs(xmin), 4: abs(ymin)} mask_height = {1: abs(ymax), 2: abs(xmin), 3: abs(ymin), 4: abs(xmax)} axs[0].add_patch(patches.Rectangle((0, 0), width=mask_width[quadrant], height=mask_height[quadrant], angle=mask_angles[quadrant], color='grey', zorder=-1, alpha=0.8)) # quadrant selection in maps non_quadrant = ~(moran_loc.q == quadrant) mask_quadrant = gdf[non_quadrant] df_quadrant = gdf.iloc[~non_quadrant] union2 = df_quadrant.unary_union.boundary # LISA Cluster mask and cluster boundary mask_quadrant.plot(column=attribute, scheme=scheme, color='white', ax=axs[1], alpha=0.7, zorder=1) gpd.GeoSeries([union2]).plot(linewidth=2, ax=axs[1], color='darkgrey') # CHOROPLETH MASK mask_quadrant.plot(column=attribute, scheme=scheme, color='white', ax=axs[2], alpha=0.7, zorder=1) gpd.GeoSeries([union2]).plot(linewidth=2, ax=axs[2], color='darkgrey') # REGION MASKING if region_column is not None: # masking inside axs[0] or Moran Scatterplot ix = gdf[region_column].isin(mask) df_mask = gdf[ix] x_mask = moran_loc.z[ix] y_mask = lp.lag_spatial(moran_loc.w, moran_loc.z)[ix] axs[0].plot(x_mask, y_mask, color=mask_color, marker='o', markersize=14, alpha=.8, linestyle="None", zorder=-1) # masking inside axs[1] or Lisa cluster map union = df_mask.unary_union.boundary gpd.GeoSeries([union]).plot(linewidth=2, ax=axs[1], color=mask_color) # masking inside axs[2] or Chloropleth gpd.GeoSeries([union]).plot(linewidth=2, ax=axs[2], color=mask_color) return fig, axs
def moran_bv_scatterplot(moran_bv, ax=None, scatter_kwds=None, fitline_kwds=None): """ Bivariate Moran Scatterplot. Parameters ---------- moran_bv : esda.moran.Moran_BV instance Values of Bivariate Moran's I Autocorrelation Statistics 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 Bivariate moran scatterplot figure ax : matplotlib Axes instance Axes in which the figure is plotted Examples -------- Imports >>> import matplotlib.pyplot as plt >>> import libpysal.api as lp >>> from libpysal import examples >>> import geopandas as gpd >>> from esda.moran import Moran_BV >>> from splot.esda import moran_bv_scatterplot Load data and calculate weights >>> link_to_data = examples.get_path('Guerry.shp') >>> gdf = gpd.read_file(link_to_data) >>> x = gdf['Suicids'].values >>> y = gdf['Donatns'].values >>> w = lp.Queen.from_dataframe(gdf) >>> w.transform = 'r' Calculate Bivariate Moran >>> moran_bv = Moran_BV(x, y, w) plot >>> moran_bv_scatterplot(moran_bv) >>> plt.show() customize plot >>> moran_bv_scatterplot(moran_bv, zstandard=False, ... fitline_kwds=dict(color='#4393c3')) >>> plt.show() """ # to set default as an empty dictionary that is later filled with defaults if scatter_kwds is None: scatter_kwds = dict() if fitline_kwds is None: fitline_kwds = dict() # define customization scatter_kwds.setdefault('alpha', 0.6) scatter_kwds.setdefault('color', splot_colors['moran_base']) scatter_kwds.setdefault('s', 40) fitline_kwds.setdefault('alpha', 0.9) fitline_kwds.setdefault('color', splot_colors['moran_fit']) # get fig and ax fig, ax = _create_moran_fig_ax(ax, figsize=(7,7)) # set labels ax.set_xlabel('Attribute X') ax.set_ylabel('Spatial Lag of Y') ax.set_title('Bivariate Moran Scatterplot' + ' (' + str(round(moran_bv.I, 2)) + ')') # plot and set standards lag = lp.lag_spatial(moran_bv.w, moran_bv.zy) fit = OLS(moran_bv.zy[:, None], lag[:, None]) # plot ax.scatter(moran_bv.zx, lag, **scatter_kwds) ax.plot(lag, fit.predy, **fitline_kwds) # v- and hlines ax.axvline(0, alpha=0.5, color='k', linestyle='--') ax.axhline(0, alpha=0.5, color='k', linestyle='--') return fig, ax