Example #1
0
 def plot(self, xys, neighborhoods=None, shpfile=None, outfile=None,
         title=None):
     if self.happy_ending:
         cm = get_cmap('RdBu')
         cm = get_cmap('Accent')
         f = plt.figure()
         ax = f.add_subplot(111)
         grid = ax.scatter(xys[:, 0], xys[:, 1], alpha=0.75, linewidths=0, \
                 s=80, marker=None, color='0.6', cmap=cm)
         axys = xys[self.agent_xyids, :]
         ax.scatter(axys[:, 0], axys[:, 1], alpha=0.8, linewidths=0, \
                 s=20, marker='o', c=self.group_map, cmap=cm)
         props = self.prop_groups + [1. - sum(self.prop_groups)]
         if title == None:
             title = "%i agents | %s share | %.2f similar wanted"\
                     %(self.pop_size, '_'.join(map(str, props)), \
                             self.pct_similar_wanted)
         plt.title(title, color='0.3')
         ax.set_frame_on(False)
         ax.axes.get_yaxis().set_visible(False)
         ax.axes.get_xaxis().set_visible(False)
         if neighborhoods:
             nr, nc = neighborhoods
             r = len(set(xys[:, 1]))
             ir = int(np.round((r*1.)/nr))
             nrb = [i+ir for i in range(-ir, r, ir)]
             nrb[-1] += 0.5
             nrb[0] -= 0.5
             c = len(set(xys[:, 0]))
             ic = int(np.round((c*1.)/nc))
             ncb = [i+ic for i in range(-ic, c, ic)]
             ncb[0] -= 0.5
             ncb[-1] += 0.5
             # Horizontal lines
             for i in nrb:
                 plt.hlines(i-0.5, xmin=-1, xmax=c, color='k')
             # Vertical lines
             for i in ncb:
                 plt.vlines(i-0.5, ymin=-1, ymax=r, color='k')
         elif shpfile:
             from pysal.contrib.viz import mapping as viz
             shp = ps.open(shpfile)
             patchco = viz.map_poly_shp(shp)
             patchco.set_facecolor('none')
             patchco.set_edgecolor('0.3')
             patchco.set_linewidth(0.3)
             ax = viz.setup_ax([patchco], ax)
         fc = '1'
         f.set_facecolor(fc)
         if not outfile:
             plt.show()
         else:
             plt.savefig(outfile, facecolor=fc)
     else:
         print 'No use in plotting a bad ending'
Example #2
0
def lisa_mapa(variavel, shapefile, p_thres=0.05, **kws):

    w = ps.queen_from_shapefile(shapefile)
    lisa = ps.Moran_Local(variavel, w)

    fig = plt.figure(figsize=(9, 7))
    shp = ps.open(shapefile)
    base = maps.map_poly_shp(shp)
    base = maps.base_lisa_cluster(base, lisa, p_thres=p_thres)
    base.set_edgecolor('1')
    base.set_linewidth(0.1)
    ax = maps.setup_ax([base], [shp.bbox])

    boxes, labels = maps.lisa_legend_components(lisa, p_thres=p_thres)
    plt.legend(boxes, labels, fancybox=True, **kws)

    plt.show();
Example #3
0
lisa = ps.Moran_Local(hr90, w, permutations=9999)

# _ = maps.plot_lisa_cluster(shp_link, lisa, figsize=(9, 6))




import matplotlib.pyplot as plt
import cartopy.crs as ccrs

orig_crs = ccrs.PlateCarree()
projection = ccrs.LambertConformal()
p_thres = 0.01

shp = ps.open(shp_link)
polys = maps.map_poly_shp(shp)
polys = maps.base_lisa_cluster(polys, lisa, p_thres=p_thres)
polys.set_edgecolor('1')
polys.set_linewidth(0.2)
polys.set_transform(orig_crs)

f = plt.figure(figsize=(12, 8))

ax = plt.axes(projection=projection)
extent = [shp.bbox[0], shp.bbox[2], shp.bbox[1], shp.bbox[3]]
ax.set_extent(extent, crs=ccrs.PlateCarree())
ax.add_collection(polys)
ax.outline_patch.set_visible(False)

boxes, labels = maps.lisa_legend_components(lisa, p_thres=p_thres)
plt.legend(boxes, labels, loc='lower left', fancybox=True)
Example #4
0
import numpy as np
import pysal as ps
import random as rdm
from pysal.contrib.viz import mapping as maps
import matplotlib.pyplot as plt

shp_link = 'transportation.shp'
shp = ps.open(shp_link)

fig = plt.figure()

base = maps.map_poly_shp(shp)
base.set_facecolor('none')
base.set_linewidth(0.75)
base.set_edgecolor('0.8')

ax = maps.setup_ax([base])
fig.add_axes(ax)
plt.show()
def plot_lisa(lisa, st, msa, outfile=None, thr=0.05, title=''):
    '''
    Plot LISA results for MSAs on background map of US states

    NOTE: shapefiles hardcoded linked to paths inside the function
    ...

    Arguments
    ---------
    lisa    : Moran_Local
              LISA object from PySAL
    st      : str
              Path to states shape
    msa     : str
              Path to MSA points shape
    outfile : str
              [Optional] Path to png to be written
    thr     : float
              [Optional] Significance value to identify clusters
    title   : str
              [Optional] Title for the figure
    title   : str
    Returns
    -------
    None
    '''
    sig = (lisa.p_sim < thr) * 1
    vals = pd.Series(lisa.q * sig)

    states = ps.open(st)
    pts = ps.open(msa)

    fig = plt.figure(figsize=(9, 5))

    base = maps.map_poly_shp(states)
    base.set_facecolor('0.85')
    base.set_linewidth(0.75)
    base.set_edgecolor('0.95')

    msas = pd.np.array([pt for pt in ps.open(msa)])
    sizes = vals.apply(lambda x: 4 if x==0 else 50)
    colors = vals.map(q_mapper)
    colors = pd.np.array(list(colors))
    pts = []
    for clas in q_mapper:
        i = vals[vals==clas].index
        p = plt.scatter(msas[i, 0], msas[i, 1], s=sizes[i], \
                c=colors[i, :], label=q_names[clas])
        p.set_linewidth(0)
        pts.append(p)
    plt.legend(loc=3, ncol=2, fontsize=14, scatterpoints=1, frameon=False)

    ax = maps.setup_ax([base] + pts)
    #ax = maps.setup_ax(pts)
    fig.add_axes(ax)
    if title:
        plt.title(title)
    if outfile:
        plt.savefig(outfile)
    else:
        plt.show()
    return None