Exemple #1
0
def mark_pixel(pixels, color='g', ax=None, linewidth=None):
    ''' surrounds pixels given by pixels with a border '''
    pixel_x, pixel_y = get_pixel_coords()

    if ax is None:
        ax = plt.gca()

    patches = []
    for xy in zip(pixel_x[pixels], pixel_y[pixels]):
        patches.append(
            RegularPolygon(
                xy=xy,
                numVertices=6,
                radius=9.5 / np.sqrt(3),
                orientation=0.,  # in radians
                fill=False,
            ))

    if linewidth is None:
        linewidth = calc_linewidth(ax=ax)

    collection = PatchCollection(patches, picker=0)
    collection.set_linewidth(linewidth)
    collection.set_edgecolors(color)
    collection.set_facecolor('none')

    ax.add_collection(collection)

    plt.draw_if_interactive()
    return collection
def add_blobs(rows, columns, sizes, color=None, ax=None):
    """The function draws circles around given pixel coordinates
    Parameters
    -----------
    rows: array
        pixel positions, y axis
    columns: array
        pixel positions, x axis
    sizes:array

    Returns
    -------
    star_circles:
    """
    ax = ax or plt.gca()

    star_circles = PatchCollection([
        Circle((col, row), radius=3 * sigma)
        for row, col, sigma in zip(rows, columns, sizes)
    ])
    star_circles.set_facecolor('none')
    color = color or next(ax._get_lines.prop_cycler).get('color')

    star_circles.set_edgecolor(color)
    ax.add_collection(star_circles)

    return star_circles
Exemple #3
0
    def _create_patch(self):
        """Creates a matplotlib polygon patch from the Shape points.
        This is used when making 2d images of the Shape object.

        :raises ValueError: No points defined for the Shape

        :return: a plotable polygon shape
        :rtype: Matplotlib object patch
        """

        if self.points is None:
            ValueError("No points defined for", self)

        patches = []
        xylist = []

        for x1, z1 in zip([row[0] for row in self.points],
                          [row[1] for row in self.points]):
            xylist.append([x1, z1])

        polygon = Polygon(xylist, closed=True)
        patches.append(polygon)

        p = PatchCollection(patches)

        if self.color is not None:
            p.set_facecolor(self.color)
            p.set_color(self.color)
            p.color = self.color
            p.edgecolor = self.color
            # checks to see if an alpha value is provided in the color
            if len(self.color) == 4:
                p.set_alpha = self.color[-1]
        self.patch = p
        return p
def make_plot_delta(date, start_date, end_date, dframe, save=False):
    fig, ax = plt.subplots(figsize=(15, 10))
    m = Basemap(llcrnrlon=-105.0,
                llcrnrlat=45.4,
                urcrnrlon=-95.7,
                urcrnrlat=49.5,
                projection='merc',
                resolution='h')

    cmap = plt.get_cmap('seismic')
    pc = PatchCollection(dframe.shapes, zorder=2)
    norm = Normalize(vmin=-500, vmax=500)
    pc.set_facecolor(cmap(norm(dframe['Delta GDD (5yr)'].fillna(0).values)))
    ax.add_collection(pc)
    m.drawcoastlines()
    m.drawcountries()
    m.drawstates()
    m.readshapefile('./cb_2017_us_county_500k/cb_2017_us_county_500k',
                    'counties')
    mapper = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)

    mapper.set_array(dframe['Delta GDD (5yr)'])
    cbar = m.colorbar(mapper,
                      location='bottom',
                      ticks=[-500, -250, -100, 0, 100, 250, 500])
    cbar.set_label('Delta GDDs: Corn ' + str(date.strftime('%Y-%b-%d')))
    plt.title('Difference from 5yr Average: Corn GDDs for period ' +
              str(start_date) + ' to ' + str(end_date),
              fontsize=18)
    if save:
        fig.savefig('./maps/Delta/delta5yr-' + str(date.strftime('%Y-%m-%d')) +
                    '.png',
                    format='png')

    return m
Exemple #5
0
def draw_map(df, measure, Colors, Title):
    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111, axisbg='None')
    ax.set_xlim(minx - 0.2 * w, maxx + 0.2 * w)
    ax.set_ylim(miny - 0.2 * h, maxy + 0.2 * h)
    ax.set_aspect(1)

    cmap = plt.get_cmap(Colors)
    df['patches'] = df['Poly'].map(
        lambda x: PolygonPatch(x, ec='#555555', lw=0.2, alpha=0.5, zorder=4))
    pc = PatchCollection(df['patches'].values, match_original=True)
    norm = mpl.colors.Normalize()
    pc.set_facecolor(cmap(norm(df[measure].values)))
    ax.add_collection(pc)
    plt.title(Title)

    #Add a colorbar for the PolyCollection
    #Classified the prices into 7 classes using natural break
    breaks = nb(df[df[measure].notnull()][measure].values, initial=500, k=7)
    jb = pd.DataFrame({'jenks_bins': breaks.yb},
                      index=df[df[measure].notnull()].index)
    df = df.join(jb)
    df.jenks_bins.fillna(-1, inplace=True)
    jenks_labels = ["<= Above %0.1f" % b for b in breaks.bins]
    cb = colorbar_index(ncolors=len(jenks_labels),
                        cmap=cmap,
                        shrink=0.5,
                        labels=jenks_labels)
    cb.ax.tick_params(labelsize=8)

    fig.set_size_inches(10, 10)
    plt.savefig(Title, ext='png', close=True, dpi=400, bbox_inches='tight')
Exemple #6
0
def draw_map(ratings_data, title, save_name):
  fig = plt.figure(figsize=(16,9))
  fig.suptitle(title, fontsize=20, y=.90)
  ax = fig.add_subplot(111, facecolor='w', frame_on=False)
  m = Basemap(lon_0=0, projection='robin')
  m.drawmapboundary(color='w')
  m.readshapefile(shapefile, 'units', color='#888888', linewidth=.2)

  for info, shape in zip(m.units_info, m.units):
    country = info["NAME"]
    if country not in ratings_data:
      color = inactive_color
    else:
      color = active_color
      try:
        non_visited_country.remove(country)
      except:
        pass

    pc = PatchCollection([Polygon(np.array(shape), True)])
    pc.set_facecolor(color)
    if country in ratings_data:
      pc.set_alpha((ratings_data[country]+rating_shift) / (max_rating+rating_shift))
    ax.add_collection(pc)

  ax.axhspan(0, 1000 * 1800, facecolor='w', edgecolor='w', zorder=2)

  ax_legend = fig.add_axes([0.35, 0.14, 0.3, 0.03], zorder=3)
  index  = [0.0, 1.0]
  cm = LinearSegmentedColormap.from_list('ramen_colormap', zip(index, colors))
  cb = mpl.colorbar.ColorbarBase(ax_legend, cmap=cm, ticks=[0, 1], orientation='horizontal')
  cb.ax.set_xticklabels(["3/5 Stars", "5/5 Stars"])

  plt.savefig("output/%s.png" % save_name, bbox_inches='tight', pad_inches=.2)
Exemple #7
0
    def make_plot(self,
                  val,
                  cct,
                  clm,
                  cmap='jet',
                  cmap_norm=None,
                  cmap_vmin=None,
                  cmap_vmax=None):

        # make color mapping
        smap = ScalarMappable(cmap_norm, cmap)
        smap.set_clim(cmap_vmin, cmap_vmax)
        smap.set_array(val)
        bin_colors = smap.to_rgba(val)

        # make patches
        patches = []
        for i_c, i_clm in enumerate(clm):
            patches.append(
                Rectangle((i_clm[0], i_clm[2]), i_clm[1] - i_clm[0],
                          i_clm[3] - i_clm[2]))
        patches_colle = PatchCollection(patches)
        patches_colle.set_edgecolor('face')
        patches_colle.set_facecolor(bin_colors)

        return patches_colle, smap
def make_plot_gdd(date, start_date, end_date, dframe, save=False):
    fig, ax = plt.subplots(figsize=(15, 10))
    m = Basemap(llcrnrlon=-105.0,
                llcrnrlat=45.4,
                urcrnrlon=-95.7,
                urcrnrlat=49.5,
                projection='merc',
                resolution='h')

    cmap = plt.get_cmap('nipy_spectral')
    pc = PatchCollection(dframe.shapes, zorder=2)
    norm = Normalize(vmin=100, vmax=2800)
    pc.set_facecolor(cmap(norm(dframe['Corn AGDD (F)'].fillna(0).values)))
    ax.add_collection(pc)
    m.drawcoastlines()
    m.drawcountries()
    m.drawstates()
    m.readshapefile('./cb_2017_us_county_500k/cb_2017_us_county_500k',
                    'counties')
    mapper = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)

    mapper.set_array(dframe['Corn AGDD (F)'])
    cbar = m.colorbar(mapper, location='bottom')
    cbar.set_label('Accumulated GDDs: Corn ' + str(date.strftime('%Y-%b-%d')))
    plt.title('Corn Growing Season GDDs for period ' + str(start_date) +
              ' to ' + str(end_date),
              fontsize=18)
    if save:
        fig.savefig('./maps/AGDD/gdds-' + str(date.strftime('%Y-%m-%d')) +
                    '.png',
                    format='png')

    return m
Exemple #9
0
def plot_ax2_rejected(ax2):
    ax2.set_title('Points per\nrejected-cloud', pad=5, fontsize=SMALL_SIZE + 1)
    ax2.set_xlim(-0.1, 2.1)
    ax2.set_ylim(-0.1, 1.1)
    x_coord = np.linspace(0.0, 2.0, num=nrejected + 1)
    widths = x_coord[1:] - x_coord[:-1]
    patches = []

    for i in range(nrejected):
        x_patch = x_coord[i]
        patches.append(
            mpatches.FancyBboxPatch(
                (x_patch, 0.0),
                widths[i],
                1.0,
                boxstyle=mpatches.BoxStyle("Round", pad=0.0,
                                           rounding_size=0.2)))
        add_label(ax2, (x_patch + 0.5 * widths[i], 0.5),
                  data_size[~ids_bool][i],
                  color='white')
    if nrejected == 0:
        patches.append(
            mpatches.FancyBboxPatch(
                (0.0, 0.0),
                2.0,
                1.0,
                boxstyle=mpatches.BoxStyle("Round", pad=0.0,
                                           rounding_size=0.2)))
        add_label(ax2, (1.0, 0.5), 'None', color='black')

    collection = PatchCollection(patches, alpha=1.0)
    collection.set_edgecolor('k')
    collection.set_facecolor(colors_all[~ids_bool])
    ax2.add_collection(collection)
    ax2.axis('off')
Exemple #10
0
def create_pixels(lons,lats,widths,heights,alphas,color,label):
	"""
	Plot of pixels using centers (lons,lats), with sizes (widhts,heights), angle alphas, and color color.

	:param lons: array of longitude coordinates at the center of the pixels
	:param lats: array of latitude coordinates at the center of the pixels
	:param widths: array of widhts of the pixels
	:param heights: array of heights of the pixels
	:param alphas: array of angles of the pixels
	:param color: tuple with RGB color values
	:return col: collection of rectangles with the pixels

	Developed in Python 2.7.15 :: Anaconda 4.5.10, on MACINTOSH.
	Angel Farguell ([email protected]) 2018-12-28
	"""

	# Create pixels
	pixels=[]
	for x, y, h, w, a in zip(lons, lats, heights, widths, alphas):
		xpos = x - w/2 # The x position will be half the width from the center
		ypos = y - h/2 # same for the y position, but with height
		rect = Rectangle( (xpos, ypos), w, h, a) # Create a rectangle
		pixels.append(rect) # Add the rectangle patch to our list

	# Create a collection from the rectangles
	col = PatchCollection(pixels)
	# set the alpha for all rectangles
	col.set_alpha(0.5)
	# Set the colors using the colormap
	col.set_facecolor( [color]*len(lons) )
	# No lines
	col.set_linewidth( 0 )

	return col
Exemple #11
0
    def _create_patch(self):
        """Creates a matplotlib polygon patch from the Shape points. This is
        used when making 2d images of the Shape object.

        Raises:
            ValueError: No points defined for the Shape

        Returns:
            Matplotlib object patch: a plotable polygon shape
        """

        if self.points is None:
            raise ValueError("No points defined for", self)

        patches = []
        xylist = []

        for point in self.points:
            xylist.append([point[0], point[1]])

        polygon = Polygon(xylist, closed=True)
        patches.append(polygon)

        patch = PatchCollection(patches)

        if self.color is not None:
            patch.set_facecolor(self.color)
            patch.set_color(self.color)
            patch.color = self.color
            patch.edgecolor = self.color
            # checks to see if an alpha value is provided in the color
            if len(self.color) == 4:
                patch.set_alpha = self.color[-1]
        self.patch = patch
        return patch
def print_map(m, shapefile, colored_map_dict, ax, dict_key='NAME'):
    """ plots using pyplot a basemap object 

    m -- the base map object
    shapefile -- a file with the coordinates of the countries
    colored_map_dict -- a dictionary that points from a legal name of a country
    in shapefile to a python color: {'Argentina':'r'}
    ax -- axis object
    """

    m.readshapefile(shapefile, 'units', drawbounds=True, linewidth=.2)

    for info, shape in zip(m.units_info, m.units):

        unitname = info['NAME']
        try:
            color = colored_map_dict[info[dict_key]]
        except:  # no color for this country
            continue

        patches = [Polygon(np.array(shape), True)]
        pc = PatchCollection(patches)
        pc.set_facecolor(color)
        ax.add_collection(pc)

    plt.show()
Exemple #13
0
def show_uboxes(uboxes,S=None,col='b',ecol='k', fig=None):
	if uboxes.dim != 2:
		raise Exception("show_uboxes: dimension must be 2")
	if S is None:
		S = range(uboxes.size)

	patches = []
	for i in S:
		art = mpatches.Rectangle(uboxes.corners[i],uboxes.width[0],uboxes.width[1])
		patches.append(art)

	if not fig:
		fig = plt.figure()

	ax = fig.gca()
	ax.hold(True)
	collection = PatchCollection(patches)
	collection.set_facecolor(col)
	collection.set_edgecolor(ecol)
	ax.add_collection(collection,autolim=True)
	ax.autoscale_view()
	plt.draw()
        plt.show()

	return fig
def draw_zips_3(zctashapes, dfl, colorcol='', gamma=1.0):

    if len(colorcol)>0:
        c = dfl[colorcol].copy()#**gamma

    shapes = zctashapes
    Nshp    = len(shapes)

    cm    = plt.get_cmap('Dark2')
    cccol = cm(1.*c/max(c))

    #   -- plot --
    fig     = plt.figure()
    ax      = fig.add_subplot(111)
    for nshp in xrange(Nshp):
        ptchs   = []
        pts     = np.array(shapes[nshp].points)
        prt     = shapes[nshp].parts
        par     = list(prt) + [pts.shape[0]]

        for pij in xrange(len(prt)):
            ptchs.append(Polygon(pts[par[pij]:par[pij+1]]))

#        ax.add_collection(PatchCollection(ptchs,facecolor=cccol[nshp,:],edgecolor='k', linewidths=.1))
        pc = PatchCollection(ptchs,facecolor=cccol[nshp],norm=colors.PowerNorm(gamma=gamma),edgecolor='k', linewidths=.1)
        # for collection in pc:?
        pc.set_facecolor(cccol[nshp])

        ax.add_collection(pc)
    ax.set_xlim(-91,-82)
    ax.set_ylim(41,48)

    return ax
Exemple #15
0
def show_uboxes_corners( corners, width, S=None, col='b', ecol='k' ):
	"""
	This version takes the corners and width arrays as arguments
	instead.
	"""
	if corners.ndim != 2:
		raise Exception("show_uboxes_corners: dimension must be 2")
	if S is None:
		S = range( len(corners) )

	patches = []
	for i in S:
		art = mpatches.Rectangle( corners[i], width[0], width[1] )
		patches.append(art)

	fig = plt.figure()
	ax = fig.gca()
	ax.hold(True)
	collection = PatchCollection(patches)
	collection.set_facecolor(col)
	collection.set_edgecolor(ecol)
	ax.add_collection(collection,autolim=True)
	ax.autoscale_view()
	plt.show()
	
	return fig
Exemple #16
0
 def c(p):
     # where p is list of primitives
     # outputs a collecction of primitives. essentially takes union. can treat this as a new primitive
     coll = PatchCollection(p)
     coll.set_edgecolor('k')
     coll.set_facecolor([0, 0, 0, 0])
     #     coll.set_alpha(0.5)
     return coll
Exemple #17
0
def plot_world_chloropleth(datafile,
                           dest,
                           colorscale,
                           bins,
                           nodatacolor='#dddddd',
                           scale=1,
                           projection='robin',
                           resolution='l',
                           usecol='Magnitude',
                           inputkwargs={}):
    """Format: CSV with 'Country Name', 'Country Code', and 'Magnitude' columns."""

    # See http://ramiro.org/notebook/basemap-choropleth/
    shapefile = 'ne_10m_admin_0_countries_lakes/ne_10m_admin_0_countries_lakes'
    num_colors = len(bins) - 1

    gc = GeonamesCache()
    iso3_codes = list(gc.get_dataset_by_key(gc.get_countries(), 'iso3').keys())

    df = pd.read_csv(datafile, **inputkwargs)
    df.set_index('Country Code', inplace=True)
    df = df.reindex(
        iso3_codes)  #.dropna() # Filter out non-countries and missing values.

    values = df[usecol]
    # https://matplotlib.org/api/pyplot_summary.html#matplotlib.pyplot.colormaps
    cm = plt.get_cmap(colorscale)
    scheme = [cm(i / num_colors) for i in range(num_colors)]
    scheme.append(nodatacolor)
    df['bin'] = np.digitize(values, bins) - 1
    df.sort_values('bin', ascending=False).head(10)

    # This doesn't work, is it important?
    # mpl.style.use('map')
    fig = plt.figure(figsize=(default_size * scale, default_size * scale))

    ax = fig.add_subplot(111, facecolor='w', frame_on=False)

    m = Basemap(lon_0=0, projection=projection, resolution=resolution)
    m.drawmapboundary(linewidth=default_map_linewidth * scale, color='w')

    m.readshapefile(shapefile,
                    'units',
                    color='#444444',
                    linewidth=default_border_linewidth * scale)
    for info, shape in zip(m.units_info, m.units):
        iso3 = info['ADM0_A3']
        if iso3 not in df.index:
            color = nodatacolor
        else:
            color = scheme[df.loc[iso3]['bin']]

        patches = [Polygon(np.array(shape), True)]
        pc = PatchCollection(patches)
        pc.set_facecolor(color)
        ax.add_collection(pc)

    plt.savefig(dest, bbox_inches='tight')
class BostonDensity(BostonScatter):
  def __init__(self, dataPoints):
    super(BostonDensity, self).__init__(dataPoints)
    return

  def dataDF(self):
    super(BostonDensity, self).dataDF()

    self.df_map['count'] = self.df_map['poly'].map(lambda x: int(len(filter(prep(x).contains, self.dataPoints))))
    self.df_map['POP100'] = self.df_map['POP100'].apply(float)
    self.df_map['density'] = (self.df_map['count'] * 1000) / self.df_map['POP100']
    # it's easier to work with NaN values when classifying
    self.df_map.replace(to_replace={'density': {0: np.nan}}, inplace=True)

    self.breaks = nb(
      self.df_map[self.df_map['density'].notnull()].density.values,
      initial=300,
      k=5)
    # the notnull method lets us match indices when joining
    jb = pd.DataFrame({'jenks_bins': self.breaks.yb}, index=self.df_map[self.df_map['density'].notnull()].index)
    self.df_map = self.df_map.join(jb)
    self.df_map.jenks_bins.fillna(-1, inplace=True)

    return

  def tracts(self):
    self.pc = PatchCollection(self.df_map['patches'], match_original=True)
    # impose our colour map onto the patch collection
    norm = mplc.Normalize()
    self.pc.set_facecolor(self.cmap(norm(self.df_map['jenks_bins'].values)))
    self.ax.add_collection(self.pc)
    return

  def colorScale(self):
    # Add a colour bar
    cb = colorbar_index(ncolors=len(self.jenks_labels), cmap=self.cmap, shrink=0.5, labels=self.jenks_labels)
    cb.ax.tick_params(labelsize=6)
    return

  def patchesDF(self):
    # use a blue colour ramp - we'll be converting it to a map using cmap()
    self.cmap = plt.get_cmap('Blues')
    # draw tracts with black outline. make suffolk (boston) thicker
    super(BostonDensity, self).patchesDF()

    self.jenks_labels = ["<= %0.1f per 1000 people (%s tracts)" % (b, c) for b, c in zip(
    self.breaks.bins, self.breaks.counts)]
    self.jenks_labels.insert(0, '<= 0.0 per 1000 people (%s tracts)' % len(self.df_map[self.df_map['density'].isnull()]))
    return

  def data(self):
    highest = '\n'.join([row['CT_ID_10'] + "," + 
                        str(row['density']) for i, row in self.df_map[
                        (self.df_map['jenks_bins'] == 4)][:30].sort(columns='density',
                        ascending=False).iterrows()])
    self.highest = 'TRACT_ID,DENSITY\n' + highest
    return
Exemple #19
0
def circles(x, y, s, marker=None, c='b', vmin=None, vmax=None, **kwargs):
    """
    Taken from here: https://gist.github.com/syrte/592a062c562cd2a98a83
    Make a scatter plot of circles.
    Similar to pl.scatter, but the size of circles are in data scale.
    Parameters
    ----------
    x, y : scalar or array_like, shape (n, )
        Input data
    s : scalar or array_like, shape (n, )
        Radius of circles.
    c : color or sequence of color, optional, default : 'b'
        `c` can be a single color format string, or a sequence of color
        specifications of length `N`, or a sequence of `N` numbers to be
        mapped to colors using the `cmap` and `norm` specified via kwargs.
        Note that `c` should not be a single numeric RGB or RGBA sequence
        because that is indistinguishable from an array of values
        to be colormapped. (If you insist, use `color` instead.)
        `c` can be a 2-D array in which the rows are RGB or RGBA, however.
    vmin, vmax : scalar, optional, default: None
        `vmin` and `vmax` are used in conjunction with `norm` to normalize
        luminance data.  If either are `None`, the min and max of the
        color array is used.
    kwargs : `~matplotlib.collections.Collection` properties
        Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls),
        norm, cmap, transform, etc.
    Returns
    -------
    paths : `~matplotlib.collections.PathCollection`
    Examples
    --------
    a = np.arange(11)
    circles(a, a, s=a*0.2, c=a, alpha=0.5, ec='none')
    pl.colorbar()
    License
    --------
    This code is under [The BSD 3-Clause License]
    (http://opensource.org/licenses/BSD-3-Clause)
    """

    # You can set `facecolor` with an array for each patch,
    # while you can only set `facecolors` with a value for all.

    zipped = np.broadcast(x, y, s)
    patches = [Circle((x_, y_), s_) for x_, y_, s_ in zipped]
    collection = PatchCollection(patches, **kwargs)
    if c is not None and np.issubdtype(c.dtype, np.number):
        collection.set_array(c)
        collection.set_clim(vmin, vmax)
    else:
        collection.set_facecolor(c)

    ax = pl.gca()
    ax.add_collection(collection)
    ax.autoscale_view()

    return collection
 def plot_map(self,
              title_name,
              cmap=None,
              figwidth=14,
              bins=[0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.],
              bin_labels=None,
              save_path=None):
     if cmap is None:
         cmap = self._set_colormap()
     self.df_map['bins'] = self.df_map[self.values_col].apply(
         self._get_bins, args=(bins, ))
     if bin_labels is None:
         bin_labels = ["{0}%".format(100 * x) for x in bins]
     fig = plt.figure(figsize=(figwidth, figwidth * self.h / self.w))
     ax = fig.add_subplot(111, axisbg='w', frame_on=False)
     func = lambda x: PolygonPatch(
         x, ec='#111111', lw=.8, alpha=1., zorder=4)
     self.df_map['patches'] = self.df_map['poly'].map(func)
     pc = PatchCollection(self.df_map['patches'], match_original=True)
     #cmap_rng = (self.df_map['bins'].values - min(bins)) / float(max(bins))
     #cmap_rng = (self.df_map['bins'].values - self.df_map['bins'].values.min())/ \
     #           (self.df_map['bins'].values.max() - float(self.df_map['bins'].values.min()))
     #cmap_list = [cmap(val) for val in cmap_rng]
     cmap_list = [cmap(val) for val in self.df_map[self.values_col].values]
     pc.set_facecolor(cmap_list)
     ax.add_collection(pc)
     self.map.drawmapscale(self.coords[0] + 0.08,
                           self.coords[1] + -0.01,
                           self.coords[0],
                           self.coords[1],
                           10.,
                           fontsize=16,
                           barstyle='fancy',
                           labelstyle='simple',
                           fillcolor1='w',
                           fillcolor2='#555555',
                           fontcolor='#555555',
                           zorder=5,
                           ax=ax)
     cbar = self._create_colorbar(cmap,
                                  ncolors=len(bin_labels) + 1,
                                  labels=bin_labels,
                                  shrink=0.5)
     cbar.ax.tick_params(labelsize=16)
     if title_name is not None:
         fig.suptitle(title_name,
                      fontdict={
                          'size': 24,
                          'fontweight': 'bold'
                      },
                      y=0.92)
     if save_path is not None:
         plt.savefig(save_path,
                     dpi=100,
                     frameon=False,
                     bbox_inches='tight',
                     pad_inches=0.5)
Exemple #21
0
def plot_shapefile_polys(shapefile, ax, m, cmap):
    """
    Plot polygons from the given shapefile on a basemap.

    Parameters
    ----------
    shapefile : shapefile object
        Shapefile opened with fiona (``fiona.open(path_to_shapefile)``)
    ax : matplotlib ax object
        Axes to plot on
    m : basemap object
    cmap : matplotlib colormap
        Colormap to use to color the individual polygons in the shapefile

    """
    from descartes import PolygonPatch
    from matplotlib.collections import PatchCollection
    import shapely
    import shapely.ops

    # Break up MultiPolygons into Polygons, keeping track of
    # which shape they belong to
    polygons = []
    names = []
    for i in shapefile:
        # name = i['properties'][name_property]
        name = i['id']
        geom = shapely.geometry.shape(i['geometry'])
        if geom.geom_type == 'MultiPolygon':
            for poly in geom:
                polygons.append(poly)
                names.append(name)
        else:
            polygons.append(geom)
            names.append(name)

    # Create patches with basemap-transformed coordinates
    def TransformedPolygonPatch(poly):
        return PolygonPatch(shapely.ops.transform(m, poly),
                            ec='#555555',
                            lw=.2,
                            alpha=1.,
                            zorder=4)

    polygon_patches = [TransformedPolygonPatch(p) for p in polygons]

    # Create name->color_id mapping dict
    colors = {name: idx for idx, name in enumerate(list(set(names)))}
    n = plt.Normalize(0, len(colors))

    pc = PatchCollection(polygon_patches, match_original=True)
    # Now go through all names and set the color appropriately
    pc.set_facecolor(cmap(n([colors[i] for i in names])))
    pc.set_alpha(0.2)
    ax.add_collection(pc)

    return ax
Exemple #22
0
    def update(self, element: Element, time: Timestamp, µcluster: MicroCluster,
               clusters: Optional[list[Cluster]],
               unclustered: Optional[Set[MicroCluster]],
               eliminated: Optional[Set[MicroCluster]]):
        max_density = max(
            [µcluster.density(time) for µcluster in self.dyclee.all_µclusters])

        if µcluster in self.patch_map:
            self.patch_map[µcluster].remove()

        patch_collection = PatchCollection([
            Rectangle(
                µcluster.centroid - µcluster.context.hyperbox_lengths / 2,
                *µcluster.context.hyperbox_lengths)
        ])

        patch_collection.set_edgecolor('w')
        patch_collection.set_facecolor(
            (0, 0, 0, 0.5 * µcluster.density(time) / max_density))
        self.patch_map[µcluster] = patch_collection

        self.ax.add_collection(patch_collection)

        if clusters is not None:
            for cluster in clusters:
                for µcluster in cluster.µclusters:
                    self.patch_map[µcluster].set_edgecolor(
                        self.colour_manager.get_colour(cluster.label))
                    self.patch_map[µcluster].set_facecolor(
                        (0, 0, 0, 0.5 * µcluster.density(time) / max_density))

            order = sorted(range(len(clusters)),
                           key=lambda i: clusters[i].label)
            self.ax.legend([
                Rectangle((0, 0),
                          1,
                          1,
                          facecolor='none',
                          edgecolor=self.colour_manager.get_colour(
                              clusters[i].label)) for i in order
            ], [clusters[i].label for i in order],
                           loc=self.legend_loc)

        if unclustered is not None:
            for µcluster in unclustered:
                self.patch_map[µcluster].set_edgecolor(
                    self.colour_manager.get_colour(µcluster.label,
                                                   self.unclustered_opacity))
                self.patch_map[µcluster].set_facecolor(
                    (0, 0, 0, 0.5 * µcluster.density(time) / max_density))

        if eliminated is not None:
            for µcluster in eliminated:
                self.patch_map[µcluster].remove()
                del self.patch_map[µcluster]
class GreaterBostonDensity(GreaterBostonScatter):
  def __init__(self, dataPoints):
    super(GreaterBostonDensity, self).__init__(dataPoints)
    self.routeColor = 'r'
    return

  def dataDF(self):
    super(GreaterBostonDensity, self).dataDF()

    self.df_map['count'] = self.df_map['poly'].map(lambda x: int(len(filter(prep(x).contains, self.dataPoints))))
    self.df_map['density_m'] = self.df_map['count'] / self.df_map['area_m']
    self.df_map['density_km'] = self.df_map['count'] / self.df_map['area_km']
    # it's easier to work with NaN values when classifying
    self.df_map.replace(to_replace={'density_m': {0: np.nan}, 'density_km': {0: np.nan}}, inplace=True)

    self.breaks = nb(
      self.df_map[self.df_map['density_km'].notnull()].density_km.values,
      initial=300,
      k=5)
    # the notnull method lets us match indices when joining
    jb = pd.DataFrame({'jenks_bins': self.breaks.yb}, index=self.df_map[self.df_map['density_km'].notnull()].index)
    self.df_map = self.df_map.join(jb)
    self.df_map.jenks_bins.fillna(-1, inplace=True)

    return

  def patchesDF(self):
    # use a blue colour ramp - we'll be converting it to a map using cmap()
    self.cmap = plt.get_cmap('Blues')
    # draw tracts with black outline. make suffolk (boston) thicker
    self.df_map['patches'] = [PolygonPatch(row['poly'], ec='k',
      lw=0.25 if (row['COUNTY'] != suffolkID) else 0.8,
      alpha=.9, zorder=4) for i,row in self.df_map.iterrows()]

    self.jenks_labels = ["<=$%0.1f/km$^2$ (%s tracts)" % (b, c) for b, c in zip(
    self.breaks.bins, self.breaks.counts)]
    self.jenks_labels.insert(0, '<=$0.0/km$^2$ (%s tracts)' % len(self.df_map[self.df_map['density_km'].isnull()]))
    return

  def data(self):
    highest = '\n'.join([row['TRACT'] + "," + 
                        str(row['density_km']) for i, row in self.df_map[
                        (self.df_map['jenks_bins'] == 4)][:30].sort(columns='density_km',
                        ascending=False).iterrows()])
    self.highest = 'TRACT_ID,DENSITY\n' + highest
    return # function returns nothing

  def tracts(self):
    self.pc = PatchCollection(self.df_map['patches'], match_original=True)
    # impose our colour map onto the patch collection
    norm = mplc.Normalize()
    self.pc.set_facecolor(self.cmap(norm(self.df_map['jenks_bins'].values)))
    self.ax.add_collection(self.pc)
    return
Exemple #24
0
def plot_rectangles(fld, xp, yp, title = None, cntr_lat=None, cntr_lon=None, 
                    c_width=2500., ctables=None, norm=None, ax=None, fig=None):
               
    if ctables == None:
        ctables = P.cm.viridis
        
    if title == None:
        title = "No Title"

    if norm == None:
        norm = [fld.min(), fld.max()]
        
      
    Fnorm = mpl.colors.Normalize(vmin=norm[0], vmax=norm[1], clip=True)
    # Here's where you have to make a ScalarMappable with the colormap
    mappable = P.cm.ScalarMappable(norm=Fnorm, cmap=ctables)
    
    # normalize the data for the colormapping
    colors_norm = fld/(norm[1]-norm[0])
    
    # Give it your non-normalized color data
    mappable.set_array(fld)

    rects = []
    for p in zip(xp, yp):
        xpos = p[0] - c_width/2 # The x position will be half the width from the center
        ypos = p[1] - c_width/2 # same for the y position, but with height
        rect = Rectangle( (xpos, ypos), c_width, c_width ) # Create a rectangle
        rects.append(rect) # Add the rectangle patch to our list

    # Create a collection from the rectangles
    col = PatchCollection(rects)
    
    # set the alpha for all rectangles
    col.set_alpha(0.8)
    
    # Set the colors using the colormap
    col.set_facecolor( ctables(colors_norm) )
    
    # create figure if fig == None
    if fig == None:
        fig = plt.figure()
        
    if ax == None:
        ax = fig.add_subplot(111)

    # plot collection of rectangles
    ax.add_collection(col)    
    # add a colorbar
    P.colorbar(mappable)
    
    P.title(title, fontsize=10)

    return
 def plot_polygons(self):
     p = PatchCollection(self.__array_polygons, alpha=.5)
     p.set_facecolor([0, 0, 0])
     p.set_edgecolor([0, 0, 0])
     self.__ax.set_xlim((0, self.__x_lim))
     self.__ax.set_ylim((0, self.__y_lim))
     self.__ax.set_title(self.__title)
     self.__ax.add_collection(p)
     plt.axis('off')
     plt.title('')
     plt.plot()
     plt.savefig('Results/' + self.__title, bbox_inches='tight', pad_inches=0)
Exemple #26
0
    def add_circles(self, ax, centers, radis):
        circles = []
        for c, r in zip(centers, radis):
            circles.append(patches.Circle(tuple(c), r))

        patch_collection = PatchCollection(circles)
        patch_collection.set_facecolor("none")
        patch_collection.set_edgecolor("blue")
        patch_collection.set_linewidth(1.5)
        patch_collection.set_linestyle("dashed")
        ax.add_collection(patch_collection)
        print "added %d circles" % len(circles)
def gen_heat_map(m, pickups):
    df_map = pd.DataFrame({
        'poly': [Polygon(xy) for xy in m.nyc],
        'zone_id': [zone['LocationID'] for zone in m.nyc_info]})
    df_map['area_km'] = df_map['poly'].map(lambda x: x.area) / 1000000
    area_map = df_map.groupby(['zone_id'])['area_km'].sum()
    count = 0
    fig = plt.figure()
    cmap = plt.get_cmap('Greys_r')
    color_map = np.zeros(len(df_map['poly']))
    pickup_map = {}
    plt.clf()
    norm = Normalize()

    # dateIndex, go over all the predicted lines
    for dateIndex in xrange(len(pickups)):
        pickups_count = pickups[dateIndex][2:]

        for i in xrange(len(pickups_count) - 2):
            index = i + 1
            if index in LOCATIONID_MAP.keys():
                pickup_map[LOCATIONID_MAP[index]] += pickups_count[i]
            else:
                pickup_map[index] = pickups_count[i]

        for i in xrange(len(m.nyc_info)):
            index = m.nyc_info[i]['LocationID']
            if index in LOCATIONID_MAP.keys():
                color_map[i] = pickup_map[LOCATIONID_MAP[index]]
            else:
                color_map[i] = pickup_map[index]

        ax = fig.add_subplot(111, frame_on=False, xticks=[], yticks=[])
        ax.set_title("Prediction")
        df_map['patches'] = [PolygonPatch(x, fc='white', ec='grey', lw=.25, alpha=1.0,
                                          zorder=4) for x in df_map['poly']]
        pc = PatchCollection(df_map['patches'].values, match_original=True)
        pc.set_facecolor(cmap(norm(color_map)))
        ax.add_collection(pc)
        min_x, min_y = m(lower_left[0], lower_left[1])
        max_x, max_y = m(upper_right[0], upper_right[1])

        ax.set_xlim(min_x, max_x)
        ax.set_ylim(min_y, max_y)
        ax.set_aspect(1)
        plt.tight_layout()
        fig.set_size_inches(16.76, 16.88)
        plt.show()

        #plt.savefig('data2/heat_map/nyc-' + str(date) + '-' + str(time) + '.png', dpi=100, alpha=True)
        count += 1
        if count % 100 == 0:
            print count
Exemple #28
0
    def plotpatchcollection(self, mypatches, mycolors=[], mylw=[]):
        self.canvas.figure.clf()
        ax = self.canvas.figure.subplots()

        p = PatchCollection(mypatches)  # , alpha=0.4)
        p.set_facecolor(tuple(mycolors))
        p.set_linewidth(mylw)

        ax.add_collection(p)
        ax.autoscale(enable=True, axis="both", tight=None)

        self.canvas.draw()
Exemple #29
0
def ZoomPerspectiveEtopo():

    fig, ax = plt.subplots(figsize=(3, 2.5))

    # setup Lambert Conformal basemap.
    print("I am trying to get rid of these bloody lakes")
    m = Basemap(width=1800000,
                height=1500000,
                projection='lcc',
                area_thresh=10000000.,
                resolution='h',
                lat_1=45.,
                lat_2=55,
                lat_0=25.7,
                lon_0=91.5)
    # draw coastlines.
    #m.drawrivers()
    m.drawcoastlines()
    # draw a boundary around the map, fill the background.
    # this background will end up being the ocean color, since
    # the continents will be drawn on top.
    m.drawmapboundary(fill_color='aqua')
    # fill continents, set lake color same as ocean color.
    m.fillcontinents(color='peru', lake_color='white')
    # draw parallels and meridians.
    # label parallels on right and top
    # meridians on bottom and left
    parallels = np.arange(0., 81, 5.)
    # labels = [left,right,top,bottom]
    m.drawparallels(parallels, labels=[False, True, True, False])
    meridians = np.arange(10., 351., 5.)
    m.drawmeridians(meridians, labels=[True, False, False, True])
    m.drawcountries()
    m.readshapefile('Mega_divide_footprint', 'MDF')

    # All this stuff from:
    # http://www.datadependence.com/2016/06/creating-map-visualisations-in-python/
    df_poly = pd.DataFrame(
        {'shapes': [Polygon(np.array(shape), True) for shape in m.MDF]})

    print(df_poly)

    #df_poly = df_poly.merge(new_areas, on='area', how='left')
    cmap = plt.get_cmap('Oranges')
    pc = PatchCollection(df_poly.shapes, zorder=2, alpha=0.5)
    pc.set_facecolor("gray")
    ax.add_collection(pc)

    FigFileName = "Test.svg"
    FigFormat = "svg"

    plt.savefig(FigFileName, format=FigFormat, dpi=200)
def paw_plot(sdata=None, sdata_bg=None, cdata=None, cmap=None, clim=None, edgecolor='#555555', bgcolor='#cccccc', legend=True, ax=None):
    ax = ax if ax else plt.gca()
        
    r = np.array([ 0, 1, 1, 1, 1, 1 ])
    theta = np.array([ 0, 30, 75, 120, 165, 210 ]) * np.pi / 180.0
    theta = np.array([ -5, 25, 70, 115, 160, 205 ]) * np.pi / 180.0
#    theta = np.array([ 0, 30, 72, 114, 156, 198 ]) * np.pi / 180.0
    
    if sdata is None:
        sdata = np.array([4,1,1,1,1,1])*.08
                
    if cdata is None:
        cdata = np.ones(len(r))
    
    if cmap is None:
        cmap = 'magma'
        
    if clim is None:
        clim = [0,1]
    
    xx = r * np.cos(theta)
    yy = r * np.sin(theta)            
    
    patches = [ mpatch.Circle((x,y),rad) for (x,y,rad) in zip(xx,yy,np.sqrt(sdata)) ]
    col = PatchCollection(patches, zorder=2)
    col.set_array(cdata)    
    col.set_cmap(cmap)
    col.set_edgecolor(edgecolor)
    col.set_clim(vmin=clim[0], vmax=clim[1])        
    
    ax.add_patch(mpatch.Arc((0.0, 0.0), 2.0, 2.0, angle=0.0, 
                            theta1=theta[1]*180.0/np.pi, theta2=theta[-1]*180.0/np.pi, 
                            edgecolor=edgecolor,facecolor='none', zorder=0, linestyle='--'))
   
    ax.add_collection(col)
    
    
    if legend:
        plt.colorbar(col)
        
    if sdata_bg is not None: 
        patches = [ mpatch.Circle((x,y),rad) for (x,y,rad) in zip(xx,yy,np.sqrt(sdata_bg)) ]
        col = PatchCollection(patches, zorder=1)
        col.set_edgecolor(edgecolor)
        col.set_facecolor(bgcolor)
        ax.add_collection(col)   
    
    ax.axis('equal')   
    ax.axis('off')
    
    ax.set_xlim([-1.2,1.2])
    ax.set_ylim([-.8,1.35])
Exemple #31
0
    def _detailed_map_setup(self):
        """
        Loads the shapefile from the files, stores country information and
        geometery for determing which country a tweet falls in and coloring
        that country according to the analyzed sentiment.

        Also ticks the progress bar as the countries are loaded.
        """
        # Get the shapefile path name
        dir_filepath = path.dirname(path.abspath(__file__))
        shapefile = path.join(dir_filepath, 'data', 'ne_10m_admin_0_countries')

        # Draw the outer map
        self.map_.drawmapboundary(color='#d3d3d3')
        # read in the shapefile
        self.map_.readshapefile(shapefile,
                                'countries',
                                color='black',
                                linewidth=.2)

        # iterate over the country info and shape info. Use `index` to record
        # loading progress
        for index, (info, shape) in enumerate(
                zip(self.map_.countries_info, self.map_.countries)):

            # `iso3` is a 3 letter country code
            iso3 = info['ADM0_A3']
            # don't map the antartic
            if iso3 == 'ATA':
                continue
            # convert shape to numpy array for use with basemap
            shape = np.array(shape)
            # basemap/matplotlib specific data wrangling
            polygon = Polygon(shape, True)
            patches = [polygon]
            # store the (iso_name, path) in cache. Will use `contains_points`
            # method to later determine in which country tweets fall
            self._cache_path.append((iso3, matplotlib.path.Path(shape)))

            # basemap/matplotlib specific data wrangling
            patch_collection = PatchCollection(patches)
            # store the (iso_name, patch_collection) to change the country
            # color
            self._cache_patches.append((iso3, patch_collection))
            # Set default country facecolor to be gray.
            patch_collection.set_facecolor('#d3d3d3')
            # basemap/matplotlib specific data wrangling
            self.axis.add_collection(patch_collection)

            # There's ~4268 items. 1/100 or one tick mark is every 42 paces
            if index % 42 == 0:
                self.country_progress.emit(index)
    def _plot_ward_budget_with_color(self, to_map, axes):
        """Plot the budget per city ward, coloring each polygon according to
        "the ward's budget.

        to_map: the Basemap with the map of Toronto

        axes: the axes
        """

        # Plot the City Wards in Toronto. The borders of these polygons are
        # plot as they are in the Shapefile

        dummy = to_map.readshapefile(shapefile='./shp_dir/icitw_wgs84',
                                     name='city_wards',
                                     drawbounds=False, color='green')

        # The set of polygons (city-wards in Toronto) to add (to plot)
        patches = []
        # The ten-years budget per city-ward (in same order as its
        # geographical polygon
        ten_yrs_bdg = []

        for shape, shp_info in zip(to_map.city_wards, to_map.city_wards_info):
            # print shp_info
            # The GIS shapefile 'city_wards' ('icitw_wgs84') has the ward
            # number as the field with key 'SCODE_NAME' in that shapefile
            # (you need to .lstrip('0') from it, because, e.g., wards whose
            # number has a single digit are padded with left '0's in the
            # shapefile, but are nevertheless in the decimal system -just
            # padded with '0's, that's it- but Python will understand it
            # as in octal, not decimal syste.)

            shape_ward_number = int(shp_info['SCODE_NAME'].lstrip('0'))

            # the shapefile doesn't have the budget for this ward-number,
            # but the Excel spreadsheet we had done the ETL on it did

            ward_budget = self._total_budget_per_ward[shape_ward_number]
            # print "Ward: %02d, 10-years budget: %f, Ward-name: %s" % \
            #      (shape_ward_number, ward_budget, shp_info['NAME'])

            # append this budget and its associated geographical polygon
            ten_yrs_bdg.append(ward_budget)
            patches.append(Polygon(np.array(shape), True))

        cmap = plt.get_cmap('Greens')
        patch_collection = PatchCollection(patches, match_original=True)
        norm = Normalize(vmin=min(ten_yrs_bdg),
                         vmax=max(ten_yrs_bdg))
        patch_collection.set_facecolor(cmap(norm(ten_yrs_bdg)))

        axes.add_collection(patch_collection)
Exemple #33
0
def get_shape_collections(data: Dict):
    shapes = []

    for points in data.values():
        shapes.append(
            Polygon(np.array([points['lons'], points['lats']]).T, closed=True))

    collection = PatchCollection(shapes)
    collection.set_facecolor('#eeeeee')
    collection.set_edgecolor('black')
    collection.set_linewidth(0.2)

    return collection
Exemple #34
0
def draw_bboxes(bboxes, ax=None, color='red', linewidth=1, **kw):
    if ax is None:
        ax = plt.gca()
    patches = [
        mpatches.Rectangle((i[1], i[0]), i[3] - i[1], i[2] - i[0])
        for i in bboxes
    ]
    boxcoll = PatchCollection(patches, **kw)
    boxcoll.set_facecolor('none')
    boxcoll.set_edgecolor(color)
    boxcoll.set_linewidth(linewidth)
    ax.collections = []
    ax.add_collection(boxcoll)
def Createheatmap(m, breaks, MapData, jenksLabels, Coordinates, ComplaintChoice, FirstDate, LastDate):
    """ Create the heatmap charts. This module saves the chart as a png file. """

    CHFig = plt.figure()
    
    # use a blue color ramp - we'll be converting it to a map using cmap()
    cmap = plt.get_cmap('Blues')
    
    # draw NYC neighborhoods with grey (given by the hex number #555555) outlines.
    MapData['patches'] = MapData['poly'].map(lambda polys: PolygonPatch(polys, ec='#555555', lw=.2, alpha=1., zorder=4))
    
    
    PC = PatchCollection(MapData['patches'], match_original=True)  
    # impose our color map onto the patch collection.
    norm = Normalize()
    PC.set_facecolor(cmap(norm(MapData['jenks_bins'].values)))
    
    Axis = CHFig.add_subplot(111, axisbg='w', frame_on=False)
    Axis.add_collection(PC)

    # Provide some facts about classification method and where the data came from. 
    Axis.text(
        1.03, 0,
        'Classification method: natural breaks\n Contains 311 Complaint data. $\copyright$ 311 data from: https://nycopendata.socrata.com/',
        ha='right', va='bottom',
        size=5,
        color='#555555',
        transform=Axis.transAxes)
    
    # Add a color bar
    ColorBar = ColorBarIndex(ncolors=len(jenksLabels), cmap=cmap, shrink=0.5, labels=jenksLabels)
    ColorBar.ax.tick_params(labelsize=6)
    
    # Add a title
    plt.title("Densities of Complaint type: {0} \n  Date Range: {1} to {2}".format(ComplaintChoice, FirstDate, LastDate))
 
    # Draw a map scale.
    m.drawmapscale(
        Coordinates[0] + 0.08, Coordinates[1] + 0.015,
        Coordinates[0], Coordinates[1],
        10.,
        barstyle='fancy', labelstyle='simple',
        fillcolor1='w', fillcolor2='#555555',
        # #555555 refers to the gray color. 
        fontcolor='#555555',
        zorder=5)
    
    CHFig.set_size_inches(7.22, 5.25)
    title = 'Output/Heatmap_' + str(ComplaintChoice)+ "_" + str(pd.to_datetime(FirstDate).date()) + "_" + str(pd.to_datetime(LastDate).date())  + '.png'
    plt.savefig(title, dpi=100, alpha=True)
    plt.close()
def plot_population_map(df, region_path, figsize=(10, 20)):
    """Plot a map of population, broken down by region."""

    # Create axes and figure, with the map centred on England.
    fig, axes = plt.subplots(figsize=size)
    data_map = Basemap(
        resolution='i',  # c, l, i, h, f or None.
        projection='merc',
        lat_0=54.5,
        lon_0=-4.36,
        llcrnrlon=-6.0,
        llcrnrlat=49.5,
        urcrnrlon=2.0,
        urcrnrlat=55.2)

    # Draw general geography.
    data_map.drawmapboundary(fill_color='#46bcec')
    data_map.fillcontinents(color='#f2f2f2', lake_color='#46bcec')
    data_map.drawcoastlines()

    # Read in the shape file and record it in a more useful format.
    data_map.readshapefile(region_path, 'regions', color='none')
    regions = dict([
        (info['NAME'], shape)
        for info, shape in zip(data_map.regions_info, data_map.regions)
    ])

    # Get populations in each region.
    region_counts = df.groupby('Region').size()

    # Create Polygon objects from shape file.
    shapes = [
        Polygon(np.array(regions[index]), True)
        for index in region_counts.index
    ]

    # Define colour map and attach data to each region the appropriate colour.
    cmap = plt.get_cmap('Oranges')
    pc = PatchCollection(shapes, zorder=2)
    norm = Normalize()
    pc.set_facecolor(cmap(norm(region_counts)))
    axes.add_collection(pc)

    # Create mapper object to apply colour to shapes.
    mapper = matplotlib.cm.ScalarMappable(cmap=cmap)
    mapper.set_array(region_counts)
    plt.colorbar(mapper, shrink=0.4)

    # Set title and show.
    plt.title('Population by Region')
    plt.show()
Exemple #37
0
def IndicatorHeatMap(YEAR, LOCATION="Maryland", INDICATOR="None"):
    # Using a CSV of economic indicators, create a heatmap that
    # shows the data. This will be used under scatter and hexbin maps
    # if an indicator is selected.
    # This can probably be turned into an if/elif/else statement that
    # sets a common variable to a string based on the indicator chosen
    # and passes that variable through the breaks and jenks process.
    df_map = pd.merge(df_map, indicator_list, on="county_name", how="left")
    if INDICATOR == "None":
        pass
    elif INDICATOR == "Median Household Income":
        # Select county or neighborhood MHHI data from the passed year
        breaks = nb(
            df_map[df_map["mhhi"].notnull()].mhhi.values,
            initial=300,
            k=5)
            
        jb = pd.DataFrame({"jenks_bins":breaks.yb}, index=df_map[df_map["mhhi"].notnull()].index)
        df_map = df_map.join(jb)
        df_map.jenks_bins.fillna(-1, inplace=True)

        jenks_labels = ["Median Household Income:\n<= %f" % b for b in breaks.bins]
    elif INDICATOR == "Millennial Population Growth":
        # Select county or neighborhood Millennial population data from the passed year
        pass
    else:
        print "The %s indicator is not yet available in this program." % INDICATOR
    
    ax = fig.add_subplot(111, axisbg = "w", frame_on = False)

    # Change get_cmap color based on INDICATOR    
    cmap = plt.get_cmap("Blues")
    df_map["patches"] = df_map["poly"].map(lambda x: PolygonPatch(x, ec="#555555", lw=.2, alpha=1, zorder=4))
    pc = PatchCollection(df_map["patches"], match_original=True)
    norm = Normalize()
    pc.set_facecolor(cmap(norm(df_map["jenks_bins"].values)))
    ax.add_collection(pc)

    cb = colorbar_index(ncolors=len(jenks_labels), cmap=cmap, shrink=0.5, labels=jenks_labels)
    cb.ax.tick_params(labelsize=8)

    m.drawmapscale(
        -125, 20,
        -125, 20,
        10.,
        barstyle = "fancy", labelstyle = "simple",
        fillcolor1 = "w", fillcolor2 = "w",
        fontcolor = "w",
        zorder=9,
        units = "m",
        fontsize =7)    
Exemple #38
0
def plot_map(m, coords, df_map, info, savefig=False):
    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111, axisbg='w', frame_on=True)
    # draw wards with grey outlines
    norm = Normalize()
    for i in xrange(5):
        color = colormaps[i]
        cmap = plt.get_cmap(color)
        cond = (df_map['class'] == (i+1))
        inx = df_map[cond].index
        if cond.sum() > 0:
            pc = PatchCollection(df_map[cond]['patches'],
                                 match_original=True, alpha=0.75)
            pc.set_facecolor(cmap(norm(df_map.loc[inx, 'cls_%d'%(i+1)].values)))
            ax.add_collection(pc)
    if (df_map['class'] == 0).sum() > 0:
        pc = PatchCollection(df_map[df_map['class'] == 0]['patches'],
                             match_original=True, alpha=0.1
                             )
        pc.set_facecolor('grey')
        ax.add_collection(pc)
    x, y = m(coords[0], coords[3]+0.006)

    details = ax.annotate(info, xy=(x, y), size=20, color='k')

    # Draw a map scale
    m.drawmapscale(
        coords[0]+0.02, coords[1]-0.004,
        coords[0], coords[1],
        2,
        barstyle='fancy', labelstyle='simple',
        fillcolor1='w', fillcolor2='#555555',
        fontcolor='#555555', units='mi',
        zorder=5)

    legend_patches = []
    for i in range(5):
        legend_patches.append(mpatches.Patch(color='C%d' % i,
                                             label=classes[i]))
    ax.legend(handles=legend_patches, loc='upper right')

    fig.set_size_inches(12, 12)
    plt.tight_layout()
    if savefig:
        plt.savefig(savefig, dpi=200, alpha=True)
def plot_trajectory_ellipse(frame, varx="attr_VARX", vary="attr_VARY", covxy="attr_COVXY", opacity_factor=1):
    """
    Draw the trajectory and uncertainty ellipses around teach point.
    1) Scatter of points 
    2) Trajectory lines
    3) Ellipses 
    :param frame: Trajectory
    :param opacity_factor: all opacity values are multiplied by this. Useful when used to plot multiple Trajectories in
     an overlay plot.
    :return: axis
    """
    ellipses = []    
    segments = []
    start_point = None

    for i, pnt in frame.iterrows():  
        # The ellipse
        U, s, V = np.linalg.svd(np.array([[pnt[varx], pnt[covxy]], 
                                          [pnt[covxy], pnt[vary]]]), full_matrices=True)
        w, h = s**.5 
        theta = np.arctan(V[1][0]/V[0][0])   # == np.arccos(-V[0][0])              
        ellipse = {"xy":pnt[list(frame.geo_cols)].values, "width":w, "height":h, "angle":theta}
        ellipses.append(Ellipse(**ellipse))
        
        # The line segment
        x, y = pnt[list(frame.geo_cols)][:2]
        if start_point:           
            segments.append([start_point, (x, y)])
        start_point = (x, y)

    ax = plt.gca()
    ellipses = PatchCollection(ellipses)
    ellipses.set_facecolor('none')
    ellipses.set_color("green")
    ellipses.set_linewidth(2)
    ellipses.set_alpha(.4*opacity_factor)
    ax.add_collection(ellipses)

    frame.plot(kind="scatter", x=frame.geo_cols[0], y=frame.geo_cols[1], marker=".", ax=plt.gca(), alpha=opacity_factor)

    lines = LineCollection(segments)
    lines.set_color("gray")
    lines.set_linewidth(1)
    lines.set_alpha(.2*opacity_factor)
    ax.add_collection(lines)
    return ax
Exemple #40
0
 def _get_fpt_ell_collection(dm, fpts, T_data, alpha, edgecolor):
     ell_patches = []
     for (x, y, a, c, d) in fpts:  # Manually Calculated sqrtm(inv(A))
         with catch_warnings():
             simplefilter("ignore")
             aIS = 1 / sqrt(a)
             cIS = (c / sqrt(a) - c / sqrt(d)) / (a - d + eps(1))
             dIS = 1 / sqrt(d)
         transEll = Affine2D([(aIS, 0, x), (cIS, dIS, y), (0, 0, 1)])
         unitCirc1 = Circle((0, 0), 1, transform=transEll)
         ell_patches = [unitCirc1] + ell_patches
     ellipse_collection = PatchCollection(ell_patches)
     ellipse_collection.set_facecolor("none")
     ellipse_collection.set_transform(T_data)
     ellipse_collection.set_alpha(alpha)
     ellipse_collection.set_edgecolor(edgecolor)
     return ellipse_collection
def ZoomPerspectiveEtopo():
    
    fig, ax = plt.subplots(figsize=(3, 2.5))

    # setup Lambert Conformal basemap.
    print("I am trying to get rid of these bloody lakes")
    m = Basemap(width=1800000,height=1500000,projection='lcc',area_thresh=10000000.,
                resolution='h',lat_1=45.,lat_2=55,lat_0=25.7,lon_0=91.5)
    # draw coastlines.
    #m.drawrivers()
    m.drawcoastlines()
    # draw a boundary around the map, fill the background.
    # this background will end up being the ocean color, since
    # the continents will be drawn on top.
    m.drawmapboundary(fill_color='aqua')
    # fill continents, set lake color same as ocean color.
    m.fillcontinents(color='peru',lake_color='white')
    # draw parallels and meridians.
    # label parallels on right and top
    # meridians on bottom and left
    parallels = np.arange(0.,81,5.)
    # labels = [left,right,top,bottom]
    m.drawparallels(parallels,labels=[False,True,True,False])
    meridians = np.arange(10.,351.,5.)
    m.drawmeridians(meridians,labels=[True,False,False,True])
    m.drawcountries()
    m.readshapefile('Mega_divide_footprint', 'MDF')

    # All this stuff from:
    # http://www.datadependence.com/2016/06/creating-map-visualisations-in-python/
    df_poly = pd.DataFrame({
            'shapes': [Polygon(np.array(shape), True) for shape in m.MDF]})

    print(df_poly)

    #df_poly = df_poly.merge(new_areas, on='area', how='left')
    cmap = plt.get_cmap('Oranges')   
    pc = PatchCollection(df_poly.shapes, zorder=2, alpha = 0.5)
    pc.set_facecolor("gray")
    ax.add_collection(pc)  

    FigFileName = "Test.svg"
    FigFormat = "svg"

    plt.savefig(FigFileName,format=FigFormat,dpi=200)       
 def plot_map(self, title_name, cmap=None, figwidth=14,
              bins=[0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.],
              bin_labels=None, save_path=None):
     if cmap is None:
         cmap = self._set_colormap()
     self.df_map['bins'] = self.df_map[self.values_col].apply(self._get_bins, args=(bins,))
     if bin_labels is None:
         bin_labels = ["{0}%".format(100 * x) for x in bins]
     fig = plt.figure(figsize=(figwidth, figwidth * self.h / self.w))
     ax = fig.add_subplot(111, axisbg = 'w', frame_on = False)
     func = lambda x: PolygonPatch(x, ec='#111111', lw=.8, alpha=1.,
                                   zorder=4)
     self.df_map['patches'] = self.df_map['poly'].map(func)
     pc = PatchCollection(self.df_map['patches'], match_original=True)
     #cmap_rng = (self.df_map['bins'].values - min(bins)) / float(max(bins))
     #cmap_rng = (self.df_map['bins'].values - self.df_map['bins'].values.min())/ \
     #           (self.df_map['bins'].values.max() - float(self.df_map['bins'].values.min()))
     #cmap_list = [cmap(val) for val in cmap_rng]
     cmap_list = [cmap(val) for val in self.df_map[self.values_col].values]
     pc.set_facecolor(cmap_list)
     ax.add_collection(pc)
     self.map.drawmapscale(self.coords[0] + 0.08,
                           self.coords[1] + -0.01,
                           self.coords[0],
                           self.coords[1],
                           10.,
                           fontsize=16,
                           barstyle='fancy',
                           labelstyle='simple',
                           fillcolor1='w',
                           fillcolor2='#555555',
                           fontcolor='#555555',
                           zorder=5,
                           ax=ax)
     cbar = self._create_colorbar(cmap, ncolors=len(bin_labels) + 1,
                                  labels=bin_labels, shrink=0.5)
     cbar.ax.tick_params(labelsize=16)
     if title_name is not None:
         fig.suptitle(title_name, fontdict={'size':24, 'fontweight':'bold'},
                      y=0.92)
     if save_path is not None:
         plt.savefig(save_path, dpi=100, frameon=False, bbox_inches='tight', pad_inches=0.5)
 def plotGeometry(self, ax = None):
     if (ax is None):
         fig = plt.figure()
         ax = fig.add_subplot(111)
     ax.set_aspect(1)
     
     self.blockPatch.set_facecolor('#F0F0F0')
     self.blockPatch.set_edgecolor('k')
     ax.add_patch(self.blockPatch)    
     
     pc = PatchCollection(self.channelPatches, match_original = True)
     pc.set_facecolor('w')
     pc.set_edgecolor('k')
     pc.set_zorder(2)
     ax.add_collection(pc)
        
     self.plotDimensions(ax)
     ax.set_xlim(self.xMin, self.xMax)
     ax.set_ylim(self.yMin, self.yMax)
     plt.show()
def show_box(b, col="b", ecol="k", alpha=1):
    patches = []

    # lower left corner at b[0], followed by width and height
    xy = b[0]
    width = np.absolute(b[0, 1] - b[0, 0])
    height = np.absolute(b[1, 1] - b[1, 0])
    art = mpatches.Rectangle(xy, width, height)
    # art = mpatches.Rectangle(b[0],b[1,0],b[1,1])
    patches.append(art)

    ax = plt.gca()
    ax.hold(True)
    collection = PatchCollection(patches)
    collection.set_facecolor(col)
    collection.set_edgecolor(ecol)
    collection.set_alpha(alpha)
    ax.add_collection(collection, autolim=True)
    ax.autoscale_view()
    plt.show()
def show_boxes(boxes, S=None, col="b", ecol="k", alpha=1):
    if boxes.dim != 2:
        raise Exception("show_boxes: dimension must be 2")
    if S is None:
        S = range(boxes.size)

    patches = []
    for i in S:
        art = mpatches.Rectangle(boxes.corners[i], boxes.widths[i][0], boxes.widths[i][1])
        patches.append(art)

    ax = plt.gca()
    ax.hold(True)
    collection = PatchCollection(patches)
    collection.set_facecolor(col)
    collection.set_edgecolor(ecol)
    collection.set_alpha(alpha)
    ax.add_collection(collection, autolim=True)
    ax.autoscale_view()
    plt.show()
def plot_sparse_trajectory(trajectory, r=50, opacity_factor=1, plot_text=True,
                           num_col="segment_sparcify_n", min_static=100):
    """
    Plots a sparsified trajectory as circles with the number of points they represent as a number inside.
    :param trajectory: Trajectory object
    :param r: the radius of circles
    :param num_col: where to find the number to put in the circles
    :param min_static: minimum count to change color of circle
    :param plot_text: put the text with num of points in the circle?
    :return: ax
    """
    ax = plt.gca()
    trajectory.plot(kind="scatter", x=trajectory.geo_cols[0], y=trajectory.geo_cols[1], marker=".",
                    ax=plt.gca(), alpha=0.0*opacity_factor)

    circles = []
    segments = []
    start_point = None

    for i, pnt in trajectory.iterrows():
        circles.append(Circle(pnt[list(trajectory.geo_cols)].values, radius=r))

        if plot_text:
            plt.text(*pnt[list(trajectory.geo_cols)], s=str(int(pnt[num_col])), fontsize=12)

        x, y = pnt[list(trajectory.geo_cols)][:2]
        if start_point:
            segments.append([start_point, (x, y)])
        start_point = (x, y)

    circles = PatchCollection(circles)
    circles.set_facecolor(['none' if cnt < min_static else 'red' for cnt in trajectory[num_col].values])
    circles.set_alpha(.5*opacity_factor)
    ax.add_collection(circles)

    lines = LineCollection(segments)
    lines.set_color("gray")
    lines.set_alpha(.2*opacity_factor)
    ax.add_collection(lines)

    return ax
Exemple #47
0
  def make_plot(self, val, cct, clm,
                cmap = 'jet', cmap_norm = None,
                cmap_vmin = None, cmap_vmax = None):

    # make color mapping
    smap = ScalarMappable(cmap_norm, cmap)
    smap.set_clim(cmap_vmin, cmap_vmax)
    smap.set_array(val)
    bin_colors = smap.to_rgba(val)

    # make patches
    patches = []
    for i_c, i_clm in enumerate(clm):
      patches.append(Rectangle((i_clm[0],  i_clm[2]),
                                i_clm[1] - i_clm[0],
                                i_clm[3] - i_clm[2]))
    patches_colle = PatchCollection(patches)
    patches_colle.set_edgecolor('face')
    patches_colle.set_facecolor(bin_colors)

    return patches_colle, smap
Exemple #48
0
def show_box(b,col='b',ecol='k',alpha=1, fig=None):
	patches = []
	
	# lower left corner at b[0], followed by width and height
	art = mpatches.Rectangle(b[0],b[1,0],b[1,1])
	patches.append(art)

	if fig:
		ax = fig.gca()
	else:
                fig = plt.figure()
		ax = fig.gca()
	ax.hold(True)
	collection = PatchCollection(patches)
	collection.set_facecolor(col)
	collection.set_edgecolor(ecol)
	collection.set_alpha(alpha)
	ax.add_collection(collection,autolim=True)
	ax.autoscale_view()
	plt.show()

	return fig
Exemple #49
0
def build_map_nmf(df_map, m, coords, info, title, CrimePatterns):
    # plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111, axisbg='w', frame_on=True)
    
    # draw wards with grey outlines
    norm = Normalize()
    for i in xrange(5):
        color = colormaps[i]
        cmap = plt.get_cmap(color)
        pc = PatchCollection(df_map[df_map['class'] == i+1]['patches'], match_original=True, alpha=0.8)
        pc.set_facecolor(cmap(norm(df_map.loc[(df_map['class'] == i+1), i].values)))
        ax.add_collection(pc)
    pc = PatchCollection(df_map[df_map['class'] == 0]['patches'], match_original=True, alpha=0.2)
    pc.set_facecolor('grey')
    ax.add_collection(pc)
    x, y = m(coords[0] + 0.02, coords[1] + 1.0)
    details = plt.annotate(info, xy=(x, y), size=24, color='#555555')
    
    # Draw a map scale
    m.drawmapscale(
        coords[0] + 0.2, coords[1] + 0.95,
        coords[0], coords[1],
        20., fontsize=8,
        barstyle='fancy', labelstyle='simple',
        fillcolor1='w', fillcolor2='#555555',
        fontcolor='#555555', units='mi',
        zorder=5)
    legend_patches = []
    for i in range(6):
        legend_patches.append(mpatches.Patch(color=colors[i],
                                             label=CrimePatterns[i]))
    plt.legend(handles=legend_patches, loc='lower right')
    x1, y1 = m(coords[0] + 0.05, 33.62)
    colorinfo = 'Color represent each cluster of community;\nBrightness represent the severities of crime in each community'
    plt.annotate(colorinfo, xy=(x1, y1), size=16, color='#555555')
    plt.tight_layout()
    fig.set_size_inches(12, 13)
    plt.savefig(title, dpi=300, alpha=True)
Exemple #50
0
    def chloropleth(self, query, color = "Blues"):
        """shows a chloropleth map of crimes
        
        Args: 
            query: name of sql
        """
        self.load()
        data = pd.read_sql_query(con=self.con, sql=query)
        points = self.gen_points(data, self.data_map)
        self.data_map['count'] = self.data_map['poly'].map(lambda x: len(list(filter(prep(x).contains, points))))
        self.data_map['density_m'] = self.data_map['count'] / self.data_map['area_m']
        self.data_map['density_km'] = self.data_map['count'] / self.data_map['area_km']
        self.data_map.replace(to_replace={'density_m': {0: np.nan}, 'density_km': {0: np.nan}}, inplace=True)
    
        breaks = nb(
            self.data_map[self.data_map['density_km'].notnull()].density_km.values,
            initial=300,
            k=5)

        jb = pd.DataFrame({'jenks_bins': breaks.yb}, index=self.data_map[self.data_map['density_km'].notnull()].index)
        self.data_map = self.data_map.join(jb)
        self.data_map.jenks_bins.fillna(-1, inplace=True)

        jenks_labels = ["<= %0.1f/km$^2$(%s communities)" % (b, c) for b, c in zip(
            breaks.bins, breaks.counts)]
        jenks_labels.insert(0, 'None (%s communities)' % len(self.data_map[self.data_map['density_km'].isnull()]))
    
        cmap = plt.get_cmap(color)
        self.data_map['patches'] = self.data_map['poly'].map(lambda x: PolygonPatch(x, ec='#555555', lw=.2, alpha=1., zorder=4))
        pc = PatchCollection(self.data_map['patches'], match_original=True)
        norm = Normalize()
        pc.set_facecolor(cmap(norm(self.data_map['jenks_bins'].values)))
        self.ax.add_collection(pc)

        cb = self.gen_colorbar(colors=len(jenks_labels), color_map=cmap, shrink=0.5, labels=jenks_labels)
        cb.ax.tick_params(labelsize=6)

        plt.tight_layout()
        plt.show()
Exemple #51
0
def basemap(data='deg_OsakaBayMap_okada.bln'):

    """
    Title: Function of plotting map in Osaka Bay
    Created on 2014/07/17
    Referrence: http://matplotlib.org/examples/api/patch_collection.html
    """

    # Get land data
    patches = _read_lands(data)

    # Plot patches
    p = PatchCollection(patches, lw=0.5)
    p.set_facecolor('#eeeeee') #'w') #, alpha=0.5
    ax = plt.gca()
    ax.add_collection(p)

    # Set figure options
    ax.set_xlabel(u'Longitude [\N{DEGREE SIGN}E]')
    ax.set_ylabel(u'Latitude [\N{DEGREE SIGN}N]')
    ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
    ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
    ax.set_xlim([134.82, 135.48])
    ax.set_ylim([34.20, 34.76])
def visualize_investment_in_toronto():
    """Function to visualize the investment in the neighborhoods of
    Toronto.

    This function is too big, it will be splitted in several parts according
    to the visualization they do.

    It plots these ESRI Shapefiles from Toronto using matplotlib/basemap/etc.
    (These shapefiles are available as Open Data from the City of Toronto):

     . The City Wards in Toronto;
     . The Neighborhoods defined as Priority Investment by the City of Toronto
     . The Business Improvement Areas defined by the City of Toronto
     . The Current Value Assessment on Tax Impact for Residential Neighborhoods
       in the City of Toronto (last year available, 2011)
    """

    fig = plt.figure()

    axes = [None, None, None]
    # grid of 2x2 rows and colums for the subplots with differ. visualizations
    axes[0] = plt.subplot2grid((2, 2), (0, 0))
    axes[1] = plt.subplot2grid((2, 2), (0, 1))
    axes[2] = plt.subplot2grid((2, 2), (1, 0), colspan=2)

    # axes = fig.add_subplot(111)

    # First map is the Priority investment by the City of Toronto

    axes[0].set_title("Priority investment by the City of Toronto")
    to_map = draw_toronto_and_city_wards(axis=axes[0])

    # Read the Shapefile of the Priority Investment Neighborhoods in Toronto.
    # The Shapefile is read first and its polygons are filled with the color
    # 'facecolor' in a for-loop below

    dummy = to_map.readshapefile(shapefile="./shp_dir/TO_priority_inv_neighb", name="prio_investm", drawbounds=False)

    patches = []

    for info, shape in zip(to_map.prio_investm_info, to_map.prio_investm):
        # print info
        patches.append(Polygon(np.array(shape), True))

    axes[0].add_collection(PatchCollection(patches, facecolor="m", edgecolor="k", linewidths=1.0, zorder=3))

    # Read the Shapefile of the Business Improvement Areas in Toronto.
    # The Shapefile is read first and its polygons are filled with the color
    # 'facecolor' in a for-loop below

    axes[1].set_title("Business Improvement Areas of Toronto")
    to_map = draw_toronto_and_city_wards(axis=axes[1])

    dummy = to_map.readshapefile(shapefile="./shp_dir/TO_busin_improv_area", name="busin_improv", drawbounds=False)

    patches = []

    for info, shape in zip(to_map.busin_improv_info, to_map.busin_improv):
        # print info
        patches.append(Polygon(np.array(shape), True))

    axes[1].add_collection(PatchCollection(patches, facecolor="g", edgecolor="k", linewidths=1.0, zorder=2))

    # Read the Shapefile of the Estimated Tax Impact in Toronto.
    # The way to plot this Shapefile is different than the previous one, since
    # this shapefile has an Avg Tax Impact value [field 'avgtaximpa']
    # associated to each of its polygons, so the different tonalities of
    # 'facecolor' in its polygons represent the Avg Tax Impact value in each
    # polygon

    axes[2].set_title("Current Value and Assessed Tax Impact per Sub-Ward")
    to_map = draw_basic_map_of_toronto(axis=axes[2])

    dummy = to_map.readshapefile(
        shapefile="shp_dir/CVA_2011_Tax_Impact_WGS84", name="tax_assesm_impact", drawbounds=False
    )

    patches = []
    taxes = []

    # Note that the taxes impact (in taxes[]) is different inside a same ward
    # in the city of Toronto. I.e., a same ward can have different subpolygons
    # with different tax-impact. Example, for 'ward' = 2:
    #    {'subdiv': '19041', 'ward': 2.0, 'avgtaximpa': -56.0554, ...}
    #    {'subdiv': '19042', 'ward': 2.0, 'avgtaximpa': -58.6994, ...}

    for info, shape in zip(to_map.tax_assesm_impact_info, to_map.tax_assesm_impact):
        # print info
        # The estimated taxes in the polygon is the gradient which gives the
        # tonality of red to the polygon
        taxes.append(float(info["avgtaximpa"]))

        patches.append(Polygon(np.array(shape), True))

    cmap = plt.get_cmap("Reds")
    patch_collection = PatchCollection(patches, match_original=True)
    min_taxes = min(taxes)
    max_taxes = max(taxes)
    norm = Normalize(min_taxes, max_taxes)
    patch_collection.set_facecolor(cmap(norm(taxes)))

    axes[2].add_collection(patch_collection)

    # Add a colour bar
    delta_gradient_taxes = max_taxes - min_taxes
    color_bar_taxes = [min_taxes]
    for i in range(1, 6):
        color_bar_taxes.append(min_taxes + (i / 6.0) * delta_gradient_taxes)
    color_bar_taxes.append(max_taxes)

    clor_bar = colorbar_index(
        ncolors=len(color_bar_taxes), cmap=cmap, shrink=0.7, labels=color_bar_taxes, format="%.2f", ax=axes[2]
    )
    # Set the font-size of the tick labels in the color bar
    clor_bar.ax.tick_params(labelsize=7)
    clor_bar.set_label(label="Tax Impact")

    # Show highest densities, in descending order
    # highest = '\n'.join(
    #    value[1] for _, value in df_map[(df_map['jenks_bins'] == 4)][:10].sort().iterrows())
    # highest = 'Most Dense Wards:\n\n' + highest

    # Subtraction is necessary for precise y coordinate alignment

    # details = clor_bar.ax.text(
    #    -1., 0 - 0.007,
    #    highest,
    #    ha='right', va='bottom',
    #    size=5,
    #    color='#555555')

    # Add a small legend
    # ( http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.text )

    # dummy = axes[2].text(
    #    0.98, 0.05,
    #    'This is a map of taxes and investment per subwards in Toronto\n' +
    #    'Obtained from Open Data of the City of Toronto\n' +
    #    'See README of this project for URLs\n',
    #    horizontalalignment='right', verticalalignment='bottom',
    #    size=6,
    #    color='#555555',
    #    bbox=dict(facecolor='red', alpha=0.2),
    #    transform=axes[2].transAxes)

    # Draw a map scale

    #  to_map.drawmapscale(
    #    coords[0] + 0.08, coords[1] + 0.015,
    #    coords[0], coords[1],
    #    10.,
    #    barstyle='fancy', labelstyle='simple',
    #    fillcolor1='w', fillcolor2='#555555',
    #    fontcolor='#555555',
    #    zorder=5)

    fig.set_tight_layout(True)
    fig.set_size_inches(9, 7)

    # plt.title('Toronto Neighborhoods: Priority Investment, Business ' +
    #          'Improvement Areas,\nand Current Value Assessment of Tax ' +
    #          'Impact on Residential Properties')
    # plt.legend()
    fig.savefig("TO_developm_neighborhoods.png", dpi=600)
    plt.show()
    def GenerateMap(self, inputFile):

        start = time.time()

        #Create file which hold statistics for each inputFile (containing mean/median travel times, std, min, max etc.)
        self.statistics = self.createStatistics()
        self.basename = os.path.basename(inputFile)[:-4]
        AttributeParameter = self.A
        coords = self.coords

        #Format figure
        plt.clf()
        fig = plt.figure()

        #Picture frame for Map
        gs = gridspec.GridSpec(12, 12)
        ax = plt.subplot(gs[:,:],axisbg='w', frame_on=False)

        try:
            #Read MetropAccess-matka-aikamatriisi data in
            MatrixData = pd.read_csv(inputFile, sep=';')

            #Join data to shapefile (pandas 'merge' function)
            df_map = pd.merge(left=self.Y, right=MatrixData, how='outer', left_on='YKR_ID', right_on='from_id')

            #CLASSIFY MATRIX DATA
            #Replace -1 values
            df_map.replace(to_replace={AttributeParameter: {-1: np.nan}}, inplace=True)

            #Data for histogram
            histData = pd.DataFrame(df_map[df_map[AttributeParameter].notnull()][AttributeParameter].values)

            maxBin = max(df_map[df_map[AttributeParameter].notnull()][AttributeParameter].values) #.AttributeParameter.values)
            NoData = int(maxBin+1)
            NullCount = len(df_map[df_map[AttributeParameter].isnull()])
            NullP = (NullCount/13230.0)*100

            #Fill NoData values with maxBin+1 value
            df_map[AttributeParameter].fillna(NoData, inplace=True)

            #Manual classification
            if not self.Cl in ['Natural Breaks', 'Quantiles', "Fisher's Jenks"]:
                #Create bins for classification based on chosen classification method
                Manual = True

                if "time" in AttributeParameter:
                    measure = "min"
                    measure2 = "minutes" #Another string-form for summary
                    titleMeas = "time"

                    if self.Cl == "10 Minute Equal Intervals":

                        #Calculate the highest class (10 minutes * Number of classes)
                        maxClass = 10*self.Nclasses

                        #Create 'higher than' info for the colorbar
                        maxClassInfo = str(maxClass-10)

                        #Create array of bins from 0 to highest class with increments of 10
                        bins = np.arange(10, maxClass, 10)

                        #Add extra classes for No Data and higher than maxClass values
                        if maxBin < maxClass:
                            bins = list(np.append(bins, [maxClass+1, maxClass+2]))
                        else:
                            bins = list(np.append(bins, [maxBin, maxBin+1]))

                    elif self.Cl == "5 Minute Equal Intervals":

                        #Calculate the highest class (10 minutes * Number of classes)
                        maxClass = 5*self.Nclasses

                        #Create 'higher than' info for the colorbar
                        maxClassInfo = str(maxClass-5)

                        #Create array of bins from 0 to highest class with increments of 5
                        bins = np.arange(5, maxClass, 5)

                        #Add extra classes for No Data and higher than maxClass values
                        if maxBin < maxClass:
                            bins = list(np.append(bins, [maxClass+1, maxClass+2]))
                        else:
                            bins = list(np.append(bins, [maxBin, maxBin+1]))

                elif "dist" in AttributeParameter:

                    measure = "km"
                    measure2 = "kilometers"
                    titleMeas = "distance"

                    if self.Cl == "5 Km Equal Intervals":

                        #Calculate the highest class (5000 meters * Number of classes)
                        maxClass = 5000*self.Nclasses

                        #Create 'higher than' info for the colorbar
                        maxClassInfo = str((maxClass-5000)/1000)

                        #Create array of bins from 0 to highest class with increments of 5000 (meters)
                        bins = np.arange(5000, maxClass, 5000)

                        #Add extra classes for No Data and higher than maxClass values
                        if maxBin < maxClass:
                            bins = list(np.append(bins, [maxClass+1, maxClass+2]))
                        else:
                            bins = list(np.append(bins, [maxBin, maxBin+1]))

                    elif self.Cl == "10 Km Equal Intervals":

                        #Calculate the highest class (5000 meters * Number of classes)
                        maxClass = 10000*self.Nclasses

                        #Create 'higher than' info for the colorbar
                        maxClassInfo = str((maxClass-10000)/1000)

                        #Create array of bins from 0 to highest class with increments of 5000 (meters)
                        bins = np.arange(0, maxClass, 10000)

                        #Add extra classes for No Data and higher than maxClass values
                        if maxBin < maxClass:
                            bins = list(np.append(bins, [maxClass+1, maxClass+2]))
                        else:
                            bins = list(np.append(bins, [maxBin, maxBin+1]))

                #Classify data based on bins
                breaks = mc.User_Defined(df_map[df_map[AttributeParameter].notnull()][AttributeParameter], bins)

            else:
                Manual = False

                if self.Cl == 'Natural Breaks':
                    breaks = nb(df_map[df_map[AttributeParameter].notnull()][AttributeParameter],initial=100, k=self.Nclasses)
                elif self.Cl == 'Quantiles':
                    breaks = Quantiles(df_map[df_map[AttributeParameter].notnull()][AttributeParameter], k=self.Nclasses)
                elif self.Cl == "Fisher's Jenks":
                    breaks = fj(df_map[df_map[AttributeParameter].notnull()][AttributeParameter], k=self.Nclasses)

                bins = list(breaks.bins)

                if "time" in AttributeParameter:
                    measure = "min"
                    measure2 = "minutes" #Another string-form for summary
                    titleMeas = "time"
                    maxClassInfo = str(bins[-2])
                else:
                    measure = "km"
                    measure2 = "kilometers"
                    titleMeas = "distance"
                    maxClassInfo = str(bins[-2]/1000)

                bins.append(maxBin)
                bins.append(maxBin)


            #the notnull method lets us match indices when joining
            jb = pd.DataFrame({'jenks_bins': breaks.yb}, index=df_map[df_map[AttributeParameter].notnull()].index)
            df_map = df_map.join(jb)

            brksBins = bins[:-1]#breaks.bins[:-1] #Do not take into account NoData values

            if measure2 == "kilometers": #Convert meters (in data) to kilometers for legend
                b = [round((x/1000),0) for x in brksBins]
                brksBins = b
                del b

            brksCounts = breaks.counts[:-1] #Do not take into account NoData values

            #Check if brksCounts and brksBins dismatches --> insert 0 values if necessary (to match the counts)
            if len(brksBins) != len(brksCounts):
                dif = len(brksBins)-len(brksCounts)
                brksCounts = np.append(brksCounts,[0 for x in xrange(dif)])
            else:
                dif=0

            #List for measures which will be inserted to class labels
            measureList = [measure for x in xrange(len(brksBins))]

            #Class labels
            jenks_labels = ["%0.0f %s (%0.1f %%)" % (b, msr, (c/13230.0)*100) for b, msr, c in zip(brksBins[:-1],measureList[:-1],brksCounts[:-1])]

            if Manual == True:
                if "dist" in AttributeParameter:
                    jenks_labels.insert(int(maxBin), '>' + maxClassInfo +' km (%0.1f %%)' % ((brksCounts[-1]/13230.0)*100))
                else:
                    jenks_labels.insert(int(maxBin), '>'+ maxClassInfo +' min (%0.1f %%)' % ((brksCounts[-1]/13230.0)*100))

            jenks_labels.insert(NoData, 'NoData (%0.1f %%)' % (NullP))

            #Use modified colormap ('my_colormap') - Choose here the default colormap which is used as a startpoint --> cm.YourColor'sName (eg. cm.Blues) - See available Colormaps: http://matplotlib.org/examples/color/colormaps_reference.html
            cmap = self.my_colormap(cm.RdYlBu, len(bins))

            #Draw grid with grey outlines
            df_map['Grid'] = df_map['poly'].map(lambda x: PolygonPatch(x, ec='#555555', lw=.2, alpha=1., zorder=4)) #RGB color-codes can be found at http://www.rapidtables.com/web/color/RGB_Color.htm
            pc = PatchCollection(df_map['Grid'], match_original=True)

            #-----------------------------
            #Reclassify data to value range 0.0-1.0 (--> colorRange is 0.0-1.0)
            if Manual == True:

                colbins = np.linspace(0.0,1.0, len(bins))
                colbins = colbins-0.001
                colbins[0], colbins[-1] = 0.0001, 1.0

                reclassification = {}
                for index in range(len(bins)):
                    reclassification[index] = colbins[index]

                reclassification['_reclassify'] = self.reclassify

                reclass = []
                dataList = list(df_map['jenks_bins'])

                for value in dataList:
                    reclass.append(self.reclassify(reclassification, value))

                df_map['jenks_binsR'] = reclass
            else:
                norm = Normalize()
                df_map['jenks_binsR'] = norm(df_map['jenks_bins'].values)

            #-----------------------------

            #Impose colour map onto the patch collection
            pc.set_facecolor(cmap(df_map['jenks_binsR'].values))

            #Add colored Grid to map
            ax.add_collection(pc)

            #Add coastline to the map
            self.C['Polys'] = self.C['poly'].map(lambda x: PolygonPatch(x, fc='#606060', ec='#555555', lw=.25, alpha=.88, zorder=4)) #Alpha adjusts transparency, fc='facecolor', ec='edgecolor'
            cpc = PatchCollection(self.C['Polys'], match_original=True)
            ax.add_collection(cpc)

            #Add roads to the map
            for feature in self.R:
                xx,yy=feature.xy
                self.B.plot(xx,yy, linestyle='solid', color='#606060', linewidth=0.7, alpha=.6)

            #Add metro to the map
            for line in self.M: #metroLines is a shapely MultiLineString object consisting of multiple lines (is iterable)
                x,y=line.xy
                self.B.plot(x,y, color='#FF2F2F', linewidth=0.65, alpha=.4)

            #----------------------
            #GENERATE TARGET POINT
            #----------------------
            #Generate YKR_ID from csv name
            ykrID = int(self.basename.split('_')[2])

            #Find index of target YKR_ID
            tIndex = df_map.YKR_ID[df_map.YKR_ID == ykrID].index.tolist()
            trow = df_map[tIndex[0]:tIndex[0]+1]
            targetPolygon = trow.poly
            centroid = targetPolygon.values[0].centroid #Get centroid of the polygon --> Returns shapely polygon point-type object

            self.B.plot(
                centroid.x,centroid.y,
                'go', markersize=3, label="= Destination")

            #-----------------------------
            #LEGEND
            #-----------------------------

            #Draw a map scale
            self.B.drawmapscale(
                coords[0] + 0.47, coords[1] + 0.013, #Etäisyys vasemmalta, etäisyys alhaalta: plussataan koordinaatteihin asteissa
                coords[0], coords[1],
                #10.,
                10.,
                barstyle='fancy', labelstyle='simple',yoffset=200, #yoffset determines the height of the mapscale
                fillcolor1='w', fillcolor2='#909090', fontsize=6,  # black= #000000
                fontcolor='#202020',
                zorder=5)

            #Set up title
            if "PT" in AttributeParameter:
                tMode = "public transportation"
            elif "Car" in AttributeParameter:
                tMode = "car"
            elif "Walk" in AttributeParameter:
                tMode = "walking"

            titleText = "Travel %s to %s (YKR-ID) \n by %s" % (titleMeas,str(ykrID),tMode)
            plt.figtext(.852,.735,
                        titleText, size=9.5)


            #Plot copyright texts
            copyr = "%s MetropAccess project, University of Helsinki, 2014\nLicensed under a Creative Commons Attribution 4.0 International License" % (unichr(0xa9))

            plt.figtext(.24,.078,copyr,fontsize=4.5)

            #----------------
            #Add a colour bar
            #----------------

            #Set arbitary location (and size) for the colorbar
            axColor = plt.axes([.86, .15, .016,.52]) #([DistFromLeft, DistFromBottom, Width, Height])

            cb = self.colorbar_index(ncolors=len(jenks_labels), cmap=cmap, labels=jenks_labels, cax=axColor)#, shrink=0.5)#, orientation="vertical", pad=0.05,aspect=20)#,cax=cbaxes) #This is a function --> see at the beginning of the code. #, cax=cbaxes shrink=0.5,
            cb.ax.tick_params(labelsize=5.5)

            #Inform travel sum of the whole grid (i.e. centrality of the location)
            #Travel time
            if measure2 == "minutes":
                tMean = histData.mean().values[0]
                tMedian=histData.median().values[0]
                tMax = histData.max().values[0]
                tMin = histData.min().values[0]
                tStd=histData.std().values[0]
                travelSummary = "Summary:"
                travelMean = "Mean: %0.0f %s" % (tMean, measure2)
                travelMedian = "Median: %0.0f %s" % (tMedian, measure2)
                travelStd = "Std: %0.0f %s" % (tStd,measure2)
                travelRange = "Range: %0.0f-%0.0f %s" % (tMin,tMax,measure2)

            #Travel distance
            else:
                h = histData.values/1000
                histData = pd.DataFrame(h)
                del h
                tMean = histData.mean().values[0]
                tMedian=histData.median().values[0]
                tMax = histData.max().values[0]
                tMin = histData.min().values[0]
                tStd=histData.std().values[0]
                travelSummary = "Summary:"
                travelMean = "Mean: %0.1f %s" % (tMean, measure2)
                travelMedian = "Median: %0.1f %s" % (tMedian, measure2)
                travelStd = "Std: %0.1f %s" % (tStd,measure2)
                travelRange = "Range: %0.1f-%0.1f %s" % (tMin,tMax,measure2)


            #Write information to a statistics file
            mInfo = "%s;%0.0f;%0.0f;%0.0f;%0.0f;%0.0f\n" % ( str(ykrID), tMean, tMedian, tStd, tMin, tMax)
            self.writeStatistics(mInfo)

            #Helper variables for moving Summary statistic texts
            initialPos = .58 #.15  #.44
            initialXPos = .975 #.20 #.97
            textSize = 5.25
            split = 0.018

            #Plot Travel Summary title
            plt.figtext(initialXPos, initialPos+split*4,
                       travelSummary, ha='left', va='bottom', color='#404040', size=textSize, style='normal',fontweight='bold')

            #Plot Travel Summary mean
            plt.figtext(initialXPos, initialPos+split*3,
                       travelMean,ha='left', va='bottom', size=textSize, color='b')

            #Plot Travel Summary median
            plt.figtext(initialXPos, initialPos+split*2,
                       travelMedian,ha='left', va='bottom', size=textSize, color='r')

            #Plot Travel Summary Standard deviation
            plt.figtext(initialXPos, initialPos+split,
                       travelStd,ha='left', va='bottom', size=textSize)

            #Plot Travel Summary Range
            plt.figtext(initialXPos, initialPos,
                       travelRange,ha='left', va='bottom', size=textSize)

            #Plot Legend symbol
            ax.legend(bbox_to_anchor=(.97, 0.07), fontsize=5.5, frameon=False, numpoints=1) #1.265     bbox_to_anchor=(x,y)  --> arbitary location for legend, more info: http://matplotlib.org/api/legend_api.html

            #--------------------------------------------------------
            #Travel time and population (catchment areas) histograms
            #--------------------------------------------------------

            #New axes for travel time/distance histogram
            ax = plt.axes([.98, .39, .16, .14], axisbg='w') #([DistFromLeft, DistFromBottom, Width, Height])

            #Add histogram
            n, bins, patches = ax.hist(histData.values, 100, normed=False, facecolor='green', alpha=0.75, rwidth=0.5, orientation="vertical")
            ax.axvline(histData.median(), color='r', linestyle='solid', linewidth=1.8)
            ax.axvline(histData.mean(), color='b', linestyle='solid', linewidth=1.0)

            if measure2 == "minutes":
                ax.set_xlabel("t(min)", fontsize=5,labelpad=1.5)
                xupLim = 250 #upper limit for x-axis
            else:
                ax.set_xlabel("km", fontsize=5,labelpad=1.5)
                xupLim = 100 #upper limit for x-axis

            #Set valuelimits for axes
            ax.set_xlim(0,xupLim-30)

            if max(n) < 1000: #ymax will be set to 1000 if count of individual bin is under 1000, else 1500
                yMax = 1000
            else:
                yMax = 1600

            ax.set_ylim(0,yMax)

            #Set histogram title
            plt.figtext(.975, .535,
                        "Travel %s histogram" % titleMeas,ha='left', va='bottom', size=5.7, style='italic')

            #Adjust tick font sizes and set yaxis to right
            ax.tick_params(axis='both', direction="out",labelsize=4.5, pad=1,
                           labelright=True,labelleft=False, top=False, left=False,
                           color='k', length=3, width=.9)

            ax.xaxis.set_ticks(np.arange(0,xupLim-30,30))

            gridlines = ax.get_xgridlines()
            gridlines.extend( ax.get_ygridlines() )

            for line in gridlines:
                line.set_linewidth(.28)
                line.set_linestyle('dotted')

            ax.grid(True)

            #----------------------------------------------------
            #New axes for population diagram

            ax = plt.axes([.98, .17, .16, .14], axisbg='w') #([DistFromLeft, DistFromBottom, Width, Height])

            #Make dataframe from Ykr-population
            pop = pd.read_csv(self.Ypop, sep=';')

            #Use original Matrix without NoData values
            MatrixData.replace(to_replace={AttributeParameter: {-1: np.nan}}, inplace=True)

            #Join population information and time matrix
            join = pd.merge(left=MatrixData, right=pop, how='outer', left_on='from_id', right_on='YKR_ID')

            #Sort data by attribute parameter
            sorted = join.sort(columns=[AttributeParameter])

            #Aggregate data by AttributeParameter
            aggre = pd.DataFrame(sorted.groupby(AttributeParameter).sum().Population)

            #Create attribute from index
            aggre[AttributeParameter] = aggre.index

            #Create cumulative population attribute
            aggre['cumPop'] = aggre['Population'].cumsum()

            #Reset index and determine AttributeParameter as float (matplotlib requires for it to work)
            aggre.reset_index(inplace=True, drop=True)
            aggre[AttributeParameter].astype(float)

            #print aggre[0:10]

            #Create filled curve plot from the cumulative population
            ax.fill_between(aggre.index,aggre['cumPop']/1000,0, interpolate=True, lw=1, facecolor='green', alpha=0.6)

            #Set valuelimits for axes
            ax.set_xlim(0,xupLim-50)
            ax.set_ylim(-10,aggre['cumPop'].max()/1000+50)


            gridlines = ax.get_xgridlines()
            gridlines.extend( ax.get_ygridlines() )

            for line in gridlines:
                line.set_linewidth(.28)
                line.set_linestyle('dotted')

            ax.grid(True)
            ax.tick_params(axis='both', direction="out",labelsize=4.5, pad=1,
                                        labelright=True,labelleft=False, top=False, left=False,
                                        color='k', length=3, width=.9)
            ax.xaxis.set_ticks(np.arange(0,xupLim-30,30))

            if measure2 == "minutes":
                ax.set_xlabel("t(min)", fontsize=5,labelpad=1.5)
                measure3 = 'minutes'
            else:
                measure3 = 'km'
                ax.set_xlabel("km", fontsize=5,labelpad=1.5)

            #Set histogram title
            plt.figtext(.975, .315,
                        "Population (per 1000) reached within (x) %s" % measure3,ha='left', va='bottom', size=5.7, style='italic')

            #-----------------------
            #Save map to disk
            #-----------------------

            fig.set_size_inches(9.22, 6.35) #(Width, Height)

            outputPath = os.path.join(self.outputFolder, self.basename) + AttributeParameter + ".png"

            plt.savefig(outputPath, dpi=300, alpha=True, bbox_inches='tight')
            plt.close() #or plt.close('all') --> closes all figure windows

            end = time.time()
            lasted = int(end-start)
            return lasted

        except Exception as e:
            return e
Exemple #54
0
def plotChoropleth(filename,imgfile,figNum):
    shapefile = 'data/ne/ne_10m_admin_0_countries'
    cols = ['CC', 'DISCON']
    num_colors = 20
    gc = GeonamesCache()
    iso_codes = list(gc.get_dataset_by_key(gc.get_countries(), 'iso').keys())
    df = pd.read_csv(filename, skiprows=0, usecols=cols)
    df.set_index('CC', inplace=True)
    df = df.ix[iso_codes].dropna() # Filter out non-countries and missing values.
    values = df['DISCON']
    cm = plt.get_cmap('Reds')
    scheme = [cm(float(i) / num_colors) for i in range(num_colors)]
    #bins = np.linspace(values.min(), values.max(), num_colors)
    bins = np.linspace(0, 1, num_colors)
    df['bin'] = np.digitize(values, bins) - 1
    df.sort_values('bin', ascending=False)#.head(10)

    #print(df)

    mpl.style.use('seaborn-pastel')
    print('Plotting Figure {0}: {1}'.format(figNum,imgfile))
    fig = plt.figure(figNum,figsize=(22, 12))

    ax = fig.add_subplot(111, axisbg='w', frame_on=False)
    #plt.title('Disco Choropleth', fontsize=20)#, y=.95)

    m = Basemap(lon_0=0, projection='robin')
    m.drawmapboundary(color='w')

    m.readshapefile(shapefile, 'units', color='#444444', linewidth=.2)
    for info, shape in zip(m.units_info, m.units):
        #iso = info['ADM0_A3']
        iso = info['ISO_A2']
        #print(iso)
        try:
            if iso not in df.index:
                color = '#dddddd'
            else:
                color = scheme[int(df.ix[iso]['bin'])]
        except TypeError:
            print(iso)
            traceback.print_exc()

        patches = [Polygon(np.array(shape), True)]
        pc = PatchCollection(patches)
        pc.set_facecolor(color)
        ax.add_collection(pc)

    # Cover up Antarctica so legend can be placed over it.
    ax.axhspan(0, 1000 * 1800, facecolor='w', edgecolor='w', zorder=2)

    # Draw color legend.
    ax_legend = fig.add_axes([0.35, 0.14, 0.3, 0.03], zorder=3)
    cmap = mpl.colors.ListedColormap(scheme)
    cb = mpl.colorbar.ColorbarBase(ax_legend, cmap=cmap, ticks=bins, boundaries=bins, orientation='horizontal')
    cb.ax.set_xticklabels([str(round(i, 2)) for i in bins],rotation='80')

    # Set the map footer.
    #plt.annotate(descripton, xy=(-.8, -3.2), size=14, xycoords='axes fraction')

    plt.savefig(imgfile, bbox_inches='tight', pad_inches=.2)
cmap.set_under(color=cmap(0))       # if yearBuilt less than min(range)
cmap.set_over(color='white')       # if yearBuilt is more than max(range)
norm = matplotlib.colors.BoundaryNorm(yearBuilt_bins, ncolors)
df_map['color'] = norm(df_map.yearBuilt) 
# normalization changes nans to -1, same as underflow
df_map['color'][df_map.yearBuilt<1700] = -2
#df_map

plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111, axisbg='w', frame_on=False)

# plot parcels by adding the PatchCollection to the axes instance
pc = PatchCollection(df_map['patches'].values, match_original=True)
#pc.set_facecolor(cmap(norm(df_map.yearBuilt)/(ncolors-1)))
pc.set_facecolor(cmap(np.ma.masked_less_equal(list(df_map.color), -2)/(ncolors-1)))
ax.add_collection(pc)

yearBuilt_labels = ['%.0f-%.0f' % (np.ceil(yearBuilt_bins[i]), np.floor(yearBuilt_bins[i+1]))
                        for i in range(ncolors)]
#yearBuilt_labels.append('>%.0f' % yearBuilt_bins[-1])
yearBuilt_labels[0] = '<%.0f' % yearBuilt_bins[1]

#yearBuilt_bins = np.insert(yearBuilt_bins, 0, 1700)
#yearBuilt_bins = np.append(yearBuilt_bins, 2016)
cb = colorbar_index(ncolors=ncolors, cmap=cmap, shrink=0.5, 
                    labels=yearBuilt_labels)
#                    boundaries = [i for i in range(ncolors+1)])
#                    , extend='both')
#                    boundaries = [1700] + list(yearBuilt_bins) + [2016])
cb.ax.tick_params(labelsize=6)
# you could also use 'Oranges' or 'Greens' 
cmap = plt.get_cmap('Blues')


# draw boundaries with grey outlines
df_map['patches'] = df_map['poly'].map(lambda x: PolygonPatch(x, ec='#555555', lw=.2, alpha=1., zorder=4))


# set the PatchCollection with our defined 'patches'
pc = PatchCollection(df_map['patches'], match_original=True)

# normalize our bins between the min and max values within the bins
norm = Normalize(vmin=df_map['bins'].min(), vmax=df_map['bins'].max())

# impose our color map onto the patch collection
pc.set_facecolor(cmap(norm(df_map['bins'].values)))
ax.add_collection(pc)

# Add a color bar which has our bin_labels applied
cb = colorbar_index(ncolors=len(bin_labels), cmap=cmap, shrink=0.5, labels=bin_labels)
# set the font size of the labels (set to size 10 here)
cb.ax.tick_params(labelsize=10)

# Create a bit of small print
smallprint = ax.text(
    # set the x,y location of the smallprint
    1, 1,
    # add whatever text you would like to appear
    'This is a U.S. map showing ' + var_2_analyze + ' per state.',
    # set the horizontal/vertical alignment
    ha='right', va='bottom',
    

# <codecell>

figwidth = 14
fig = plt.figure(figsize=(figwidth, figwidth*h/w))
ax = fig.add_subplot(111, axisbg='w', frame_on=False)

cmap = plt.get_cmap('Blues')
# draw neighborhoods with grey outlines
df_map['patches'] = df_map['poly'].map(lambda x: PolygonPatch(x, ec='#111111', lw=.8, alpha=1., zorder=4))
pc = PatchCollection(df_map['patches'], match_original=True)
# apply our custom color values onto the patch collection
cmap_list = [cmap(val) for val in (df_map.jenks_bins.values - df_map.jenks_bins.values.min())/(
                  df_map.jenks_bins.values.max()-float(df_map.jenks_bins.values.min()))]
pc.set_facecolor(cmap_list)
ax.add_collection(pc)

#Draw a map scale
m.drawmapscale(coords[0] + 0.08, coords[1] + -0.01,
    coords[0], coords[1], 10.,
    fontsize=16, barstyle='fancy', labelstyle='simple',
    fillcolor1='w', fillcolor2='#555555', fontcolor='#555555',
    zorder=5, ax=ax,)

# ncolors+1 because we're using a "zero-th" color
cbar = custom_colorbar(cmap, ncolors=len(jenks_labels)+1, labels=jenks_labels, shrink=0.5)
cbar.ax.tick_params(labelsize=16)

fig.suptitle("    Time Spent in Seattle Neighborhoods", fontdict={'size':24, 'fontweight':'bold'}, y=0.92)
ax.set_title("  Using location data collected from my Android phone via Google Takeout", fontsize=14, y=0.98)
def GenerateBasemapImage(DataDirectory, RasterFile, FigWidthInches = 4, FigHeightInches = 3, bm_width = 2000000, bm_height = 2000000, projection = 'lcc',resolution = 'l', lat_0 = 0, lon_0 = 0, lat_1 = 45,lat_2 = 55, satellite_height = 10000000, FigFormat = "png", fig_dpi = 500, out_fname_prefix = ""):
    """
    This makes the basemap image. 
    
    Args:
        DataDirectory (str): The directory of the raster file
        RasterFile (str): the name of the raster file (include extension)
        FigWidthInches (float): How wide you want the basemap
        FigHeightInches (float): How high you want your basemap
        bm_width (float): The width in metres of your basemap
        bm_height (float): The height in metres covered by your basemap
        projection (str): The projection of your basemap. See basemap docs for details
        resolution (str): Resolution. See basmap documentation. Default is "l" (for low) since higher resolution ("high" or "full") must be installed separately with basemap (and is very large)
        lat_0 (flt): Latitude of centre of your map
        lon_0 (flt): Longitude of centre of your map
        lat_1 (flt): See basemap documentation 
        lon_1 (flt): See basemap documentation
        satellite_height (flt): The satellite height in metres for geostationary projections
        FigFormat (str): Figure format, can be `png`, `svg`, `pdf`, etc.
        fig_dpi (int): Dots per inch of your figure 
        out_fname_prefix (str): The prefix of the image file. If blank uses the fname_prefix
    
    
    Author: SMM
    
    Date: 24/01/2018
    """
    
    # Make sure data directory is in correct format
    if not DataDirectory.endswith(os.sep):
        print("You forgot the separator at the end of the directory, appending...")
        DataDirectory = DataDirectory+os.sep
    
    # Set up the figure. This is needed to both size the figure and get the axis handle for plotting polygons
    fig, ax = plt.subplots(figsize=(FigWidthInches, FigHeightInches))
    
    # get some filenames
    RasterSplit = RasterFile.split(".")
    Raster_prefix = RasterSplit[0]
    Shape_name = DataDirectory+Raster_prefix+"_footprint"
    SName = "Shape"
    Full_Shape_name = Shape_name+".shp"

    # Get the name of the image
    if len(out_fname_prefix) == 0:
        FigFileName = DataDirectory+Base_file+"_basemap."+fig_format
    else:
        FigFileName = DataDirectory+out_fname_prefix+"_basemap."+fig_format    

    
    # Now for the basemap 
    # setup Lambert Conformal basemap.
    #m = Basemap(width=bm_width,height=bm_width,projection=projection,
    #            resolution=resolution,lat_1=lat_1 ,lat_2=lat_2,lat_0=lat_0,lon_0=lon_0, satellite_height = satellite_height, area_thresh = 100000)    
 
    # create the shapefile
    LSDMGDAL.CreateShapefileOfRasterFootprint(DataDirectory, RasterFile)
    
    
    #shape_feature = ShapelyFeature(Reader(fname).geometries(),ccrs.PlateCarree(), facecolor='none')
    #ax.add_feature(shape_feature)

    # draw coastlines.
    #m.drawcoastlines(linewidth = 0.5)
    # draw a boundary around the map, fill the background.
    # this background will end up being the ocean color, since
    # the continents will be drawn on top.
    #m.drawmapboundary(fill_color='snow')
    # fill continents, set lake color same as ocean color.
    #m.fillcontinents(color='lightgray',lake_color='snow')
    # draw parallels and meridians.
    # label parallels on right and top
    # meridians on bottom and left
    parallels = np.arange(0.,90,5.)
    # labels = [left,right,top,bottom]
    #m.drawparallels(parallels,labels=[False,True,True,False])
    meridians = np.arange(10.,351.,5.)
    #m.drawmeridians(meridians,labels=[True,False,False,True])
    #m.drawcountries()

    # Make a patch from the shapefile
    # All this stuff from:
    # http://www.datadependence.com/2016/06/creating-map-visualisations-in-python/
    df_poly = pd.DataFrame({
            'shapes': [Polygon(np.array(shape), True) for shape in m.footprint]})    
    
    #df_poly = df_poly.merge(new_areas, on='area', how='left')
    #cmap = plt.get_cmap('Oranges')   
    pc = PatchCollection(df_poly.shapes, zorder=2, alpha = 0.5)
    pc.set_facecolor("crimson")
    ax.add_collection(pc)  

    plt.savefig(FigFileName,format=FigFormat,dpi=fig_dpi)