from kde_maps import kde_grid
import numpy as np
import matplotlib.pyplot as plt
import pysal as ps
from pysal.contrib.viz import mapping as maps
pts = np.random.random((100, 2))
xyz, gdim = kde_grid(pts, spaced=0.03)
x = xyz[:, 0].reshape(gdim)
y = xyz[:, 1].reshape(gdim)
z = xyz[:, 2].reshape(gdim)
levels = np.linspace(0, z.max(), 25)
f = plt.figure(figsize=(8, 4))
ax1 = f.add_subplot(121)
ax1.contourf(x, y, z, levels=levels, cmap=plt.cm.bone)
ax1.set_title("KDE")
ax2 = f.add_subplot(122)
ax2.scatter(pts[:, 0], pts[:, 1])
ax2.set_title("Original points")
plt.show()
link = ps.examples.get_path("NAT.shp")
maps.plot_poly_lines(link)
cents = np.array([poly.centroid for poly in ps.open(link)])
xyz, gdim = kde_grid(cents, shp_link=link, spaced=0.01)
x = xyz[:, 0].reshape(gdim)
y = xyz[:, 1].reshape(gdim)
z = xyz[:, 2].reshape(gdim)
levels = np.linspace(0, z.max(), 25)
shp = ps.open(link)
f = plt.figure(figsize=(12, 4))
ax1 = f.add_subplot(121)
base = maps.map_poly_shp(shp)
base.set_facecolor('none')
base.set_edgecolor('0.7')
ax1 = maps.setup_ax([base], ax=ax1)
ax1.contourf(x, y, z, levels=levels, cmap=plt.cm.Greys, alpha=0.75)
ax1.set_title("KDE")
ax2 = f.add_subplot(122)
base = maps.map_poly_shp(shp)
base.set_facecolor('none')
base.set_edgecolor('0.7')
ax2 = maps.setup_ax([base], ax=ax2)
ax2.scatter(cents[:, 0], cents[:, 1], linewidth=0, c='k')
ax2.set_title("Original points")
plt.show()
from IPython.display import HTML
license = """
This work is
licensed under a Creative Commons
Attribution 4.0 International License
"""
HTML(license)