def _moran_loc_scatterplot(moran_loc, zstandard=True, p=None, ax=None, scatter_kwds=None, fitline_kwds=None): """ Moran Scatterplot with option of coloring of Local Moran Statistics Parameters ---------- moran_loc : esda.moran.Moran_Local 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 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 >>> from pysal.lib.weights.contiguity import Queen >>> from pysal.lib import examples >>> from pysal.explore.esda.moran import Moran_Local >>> from pysal.viz.splot.esda import moran_scatterplot Load data and calculate Moran Local statistics >>> link = examples.get_path('Guerry.shp') >>> gdf = gpd.read_file(link) >>> y = gdf['Donatns'].values >>> w = Queen.from_dataframe(gdf) >>> w.transform = 'r' >>> m = Moran_Local(y, w) plot >>> moran_scatterplot(m) >>> plt.show() customize plot >>> moran_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, Moran_Local): raise ValueError("`moran_loc` is not a\n " + "esda.moran.Moran_Local instance") if 'color' in scatter_kwds or 'c' in scatter_kwds or 'cmap' in scatter_kwds: warnings.warn( 'To change the color use cmap with a colormap of 5,\n' + ' color defines the LISA category') # colors spots = moran_hot_cold_spots(moran_loc, 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 Local Scatterplot') # plot and set standards if zstandard is True: lag = lag_spatial(moran_loc.w, moran_loc.z) fit = OLS(moran_loc.z[:, 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) ax.plot(lag, fit.predy, **fitline_kwds) ax.scatter(moran_loc.z, 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.z, fit.predy, **scatter_kwds) else: lag = lag_spatial(moran_loc.w, moran_loc.y) b, a = np.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='--') # dashed horizontal at mean of lagged attribute ax.hlines(lag.mean(), moran_loc.y.min(), moran_loc.y.max(), alpha=0.5, linestyle='--') if p is not None: fitline_kwds.setdefault('color', 'k') scatter_kwds.setdefault('cmap', hmap) scatter_kwds.setdefault('c', spots) ax.plot(moran_loc.y, a + b * moran_loc.y, **fitline_kwds) ax.scatter(moran_loc.y, lag, **scatter_kwds) else: scatter_kwds.setdefault('c', splot_colors['moran_base']) fitline_kwds.setdefault('color', splot_colors['moran_fit']) ax.plot(moran_loc.y, a + b * moran_loc.y, **fitline_kwds) ax.scatter(moran_loc.y, lag, **scatter_kwds) return fig, ax
def _moran_global_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 >>> from pysal.lib.weights.contiguity import Queen >>> from pysal.lib import examples >>> import geopandas as gpd >>> from pysal.explore.esda.moran import Moran >>> from pysal.viz.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 = 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_kwds=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 = 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 = 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 _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 >>> from pysal.lib.weights.contiguity import Queen >>> from pysal.lib import examples >>> import geopandas as gpd >>> from pysal.explore.esda.moran import Moran_BV >>> from pysal.viz.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) >>> x = gdf['Suicids'].values >>> y = gdf['Donatns'].values >>> w = Queen.from_dataframe(gdf) >>> w.transform = 'r' Calculate Bivariate Moran >>> moran_bv = Moran_BV(x, y, w) plot >>> moran_scatterplot(moran_bv) >>> plt.show() customize plot >>> moran_scatterplot(moran_bv, ... 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 = 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
import numpy as np import pysal.lib from pysal.model.spreg import OLS db = pysal.lib.io.open(pysal.lib.examples.get_path('columbus.dbf'), 'r') hoval = db.by_col("HOVAL") y = np.array(hoval) y.shape = (len(hoval), 1) X = [] X.append(db.by_col("INC")) X.append(db.by_col("CRIME")) X = np.array(X).T ols = OLS(y, X, name_y='home value', name_x=['income', 'crime'], name_ds='columbus', white_test=True) print(ols.summary)