import pysal as ps import numpy as np import matplotlib.pyplot as plt from scipy.linalg import inv %matplotlib inline draw_map(0.95) from IPython.html.widgets import interact interact(draw_map, lamb=(-0.9, 0.9)) def draw_map(lamb): s = 20 n = s**2 w = ps.lat2W(s, s, rook=False) w.transform = 'R' e = np.random.random((n, 1)) u = inv(np.eye(n) - lamb * w.full()[0]) u = np.dot(u, e) ul = ps.lag_spatial(w, u) u = (u - u.mean()) / np.std(u) ul = (ul - ul.mean()) / np.std(ul) gu = u.reshape((s, s)) # Figure f = plt.figure(figsize=(9, 4)) ax1 = f.add_subplot(121) ax1.matshow(gu, cmap=plt.cm.YlGn) ax1.set_frame_on(False) ax1.axes.get_xaxis().set_visible(False) ax1.axes.get_yaxis().set_visible(False) #--- ax2 = f.add_subplot(122) sc = ax2.scatter(u, ul, linewidth=0) ols = ps.spreg.OLS(ul, u) tag = "b = %.3f"%ols.betas[1][0] ax2.plot(u, ols.predy, c='red', label=tag) ax2.axvline(0, c='0.5') ax2.axhline(0, c='0.5') ax2.legend() plt.xlabel('u') plt.ylabel('Wu') plt.suptitle("$\lambda$ = %.2f"%lamb) plt.show()