{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "cvt_census.ipynb\n", "\n", "Discussion: Compute Florida congressional districts using census data.\n", "\n", "Licensing: This code is distributed under the GNU LGPL license.\n", " \n", "Modified: 14 November 2016\n", "\n", "Author: John Burkardt, Lukas Bystricky" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using matplotlib backend: agg\n" ] } ], "source": [ "# Import necessary libraries and set plot option\n", "%matplotlib\n", "%matplotlib inline\n", "%config InlineBackend.figure_format = 'svg'\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import math\n", "import scipy.spatial as spatial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 27 Florida Congressional Districts from Census Data #\n", "\n", "Legally, Florida must be divided up into 27 congressional districts of roughly equal population. This process can be manipulated to favor a particular party or candidate. It is interesting to investigate the shape of districts that might be computed by an approach inspired by CVT's. \n", "\n", "We will look at:\n", "* how to read a file containing usable census data;\n", "* how to set up the PDF and CDF data from the census data;\n", "* how to use the CDF data to randomly select a single person at random in Florida;\n", "* how to sample N persons from Florida, with their latitudes and longitudes;\n", "* how to initialize each congressional district's \"generator\" point;\n", "* how to compute the nearest generator point to each selected person;\n", "* how to carry out a CVT iteration that, on each step, selects N random persons, assigns them to nearest generator, and replaces generator by centroid." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# A Census Data File#\n", "\n", "The file \"florida_census.txt\" is available from the web page \n", "http://people.sc.fsu.edu/~jburkardt/classes/urop_2016/florida_census.txt\n", "\n", "For 4,245 locations in Florida, the US Census Bureau recorded an identifying\n", "ID number (which we will ignore), the population, the longitude and latitude.\n", "\n", "Note that this file is not the original data from the Census Bureau website.\n", "Many piece of information were removed to reduce the file size and simplify\n", "the processing step. Also, a number of legitimate locations (especially in\n", "Key West!) fall outside the polygon that we use to represent the shape of Florida.\n", "In order to avoid some confusing results, we set the populations of those\n", "sites to zero!\n", "\n", "Now we need to read this file and make its data available to our Python codes." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " I Pop Lon Lat\n", "\n", " 0 7431 -82.3334 29.6511\n", " 1 3308 -82.3314 29.6680\n", " 2 2770 -82.3301 29.6842\n", " 3 5665 -82.3081 29.6795\n", " 4 4976 -82.3169 29.6568\n", " 5 4350 -82.2885 29.6558\n", " 6 6075 -82.2902 29.6317\n", " 7 2180 -82.3322 29.6390\n", " 8 3630 -82.3335 29.6274\n", " 9 1364 -82.3493 29.6305\n", "...\n", " 4235 2506 -86.0550 30.3097\n", " 4236 7367 -86.3621 30.3869\n", " 4237 0 -86.1947 30.3021\n", " 4238 1651 -85.5683 30.7151\n", " 4239 3335 -85.5911 30.7873\n", " 4240 3228 -85.4889 30.7741\n", " 4241 3624 -85.7855 30.6721\n", " 4242 2282 -85.5749 30.5019\n", " 4243 6615 -85.7626 30.5286\n", " 4244 4161 -85.5266 30.6417\n" ] } ], "source": [ "# Code to read the census data file.\n", "\n", "def florida_census_read ( ):\n", "\n", " import numpy as np\n", "\n", " input = open ( 'florida_census.txt', 'r' )\n", "\n", " m = 4245\n", " cen_pop = np.zeros ( m, dtype = np.int32 )\n", " cen_lon = np.zeros ( m, dtype = np.float64 )\n", " cen_lat = np.zeros ( m, dtype = np.float64 )\n", "\n", " i = 0\n", " for line in input:\n", "\n", " data = line.split ( )\n", " id = data[0]\n", " cen_pop[i] = data[1]\n", " cen_lon[i] = data[2]\n", " cen_lat[i] = data[3]\n", " i = i + 1\n", "\n", " input.close ( )\n", "\n", " return cen_pop, cen_lon, cen_lat\n", "#\n", "# Test the function.\n", "#\n", "cen_pop, cen_lon, cen_lat = florida_census_read ( )\n", "m = len ( cen_pop )\n", "\n", "print ( '' )\n", "print ( ' I Pop Lon Lat' )\n", "print ( '' )\n", "for i in range ( 0, 10 ):\n", " print ( ' %3d %6d %8.4f %8.4f' % ( i, cen_pop[i], cen_lon[i], cen_lat[i] ) ) \n", "print ( '...')\n", "for i in range ( m - 10, m ):\n", " print ( ' %3d %6d %8.4f %8.4f' % ( i, cen_pop[i], cen_lon[i], cen_lat[i] ) ) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Polygon contains point? #\n", "\n", "In the polygon notebook, we showed that we used triangulation\n", "to determine if a polygon contained a point. When dealing with\n", "Florida, we will prefer another method, which is made available to\n", "you in a Python file:\n", " \n", "http://people.sc.fsu.edu/~jburkardt/classes/urop_2016/polygon_contains_point.py\n", " \n", "You should copy this and put it in the same directory as your notebook,\n", "and invoke it using the command\n", "\n", " from polygon_contains_point import polygon_contains_point\n", " \n", "The function has the form\n", "```python\n", "def polygon_contains_point ( x, y, px, py ):\n", " ***\n", " return inside\n", "```\n", "where the point is (x,y), the coordinates of the vertices of the \n", "polygon are in the arrays px and py, and the result \"inside\" is \n", "True if the polygon contains the point.\n", "\n", "Test this function using the florida longitude and latitude data\n", "for the polygon coordinates px and py, and the following points:\n", " \n", " Point Lon Lat Where?\n", " 0 -87 26 (In the ocean!)\n", " 1 -81 30 (In the ocean!)\n", " 2 -84 30.5 (Near Tallahassee\n", " 3 -81 26 (Near Miami)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0 -87.0000 26.0000 False\n", " 1 -81.0000 30.0000 False\n", " 2 -84.0000 30.5000 True\n", " 3 -81.0000 26.0000 True\n" ] } ], "source": [ "# Test code for polygon contains point #\n", "\n", "from polygon_contains_point import polygon_contains_point\n", "\n", "x = np.array ( [ -87.0, -81.0, -84.0, -81.0 ] )\n", "y = np.array ( [ 26.0, 30.0, 30.5, 26.0 ] )\n", "\n", "for i in range ( 0, 4 ):\n", " inside = polygon_contains_point ( x[i], y[i], lon, lat )\n", " print ( ' %2d %8.4f %8.4f %s' % ( i, x[i], y[i], inside ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Acceptance/Rejection Sampling of the Florida Polygon #\n", "\n", "In the polygon notebook, we showed that we could use triangulation\n", "to sample the polygon uniformly. But because Florida is a complicated\n", "polygon, we will try a simpler method. \n", "\n", "To get a uniformly random point from Florida:\n", "* Surround Florida by a rectangular box;\n", "* Get a random point in that box (easy);\n", "* If the Florida polygon does not contain that point, go back one step;\n", "\n", "A box that contains Florida has the lower left corner (-88,25) and\n", "the upper right corner (-80,31).\n", "\n", "To get a sample point, then, we get unit random numbers r1 and r2 and \n", "compute:\n", "\n", " x = -88 + 8 * r1\n", " y = 25 + 6 * r2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Code for acceptance/rejection sampling of the Florida Polygon\n", "\n", "def florida_sample ( m ):\n", " xy = np.zeros ( [ m, 2 ] )\n", " i = 0\n", " while ( i < m ):\n", " r1 = np.random.rand ( )\n", " r2 = np.random.rand ( )\n", " x = -88.0 + 8.0 * r1\n", " y = 25.0 + 6.0 * r2\n", " inside = polygon_contains_point ( x, y, lon, lat )\n", " if ( inside ):\n", " xy[i,0] = x\n", " xy[i,1] = y\n", " i = i + 1\n", " return xy\n", "#\n", "# Test the code.\n", "#\n", "m = 100\n", "xy = florida_sample ( m )\n", "plt.plot ( lon, lat, 'b-' )\n", "plt.plot ( xy[:,0], xy[:,1], 'ro' )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# CVT step for Florida #\n", "\n", "Now we can write the CVT step for Florida.\n", "\n", "Given N current generators G, and using M sample points, we want to\n", "estimate the N centroids C of the corresponding Voronoi regions.\n", "\n", "Use the following seven Florida cities for G:\n", "\n", " -87.20, 30.43 Pensacola\n", " -84.25, 30.46 Tallahassee\n", " -81.66, 30.34 Jacksonville\n", " -82.48, 27.97 Tampa\n", " -82.32, 29.65 Gainesville\n", " -81.30, 28.42 Orlando\n", " -80.21, 25.78 Miami" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-86.61426552 30.71168878]\n", " [-84.54031681 30.32754013]\n", " [-81.70440116 30.21489764]\n", " [-82.12693031 27.66841477]\n", " [-82.50193766 29.61688588]\n", " [-81.17685053 28.12548008]\n", " [-80.83812526 26.16262678]]\n" ] } ], "source": [ "# cvt_step_florida()\n", "#\n", "def cvt_step_florida ( g, m ):\n", " import numpy as np\n", " n = g.shape[0]\n", " s = florida_sample ( m )\n", " ni = np.zeros ( n )\n", " c = np.zeros ( [ n, 2 ])\n", " for i in range ( 0, m ):\n", " k = -1\n", " d = np.Inf\n", " for j in range ( 0, n ):\n", " dj = np.linalg.norm ( s[i,:] - g[j,:] )\n", " if ( dj < d ):\n", " d = dj\n", " k = j\n", " ni[k] = ni[k] + 1\n", " c[k,:] = c[k,:] + s[i,:]\n", " \n", " for i in range ( 0, n ):\n", " c[i,:] = c[i,:] / float ( ni[i] )\n", " \n", " return c\n", "#\n", "# Test the function using 7 Florida cities to start with.\n", "#\n", "import numpy as np\n", "g = np.array ( [ \\\n", " [ -87.20, 30.43 ], \\\n", " [ -84.25, 30.46 ], \\\n", " [ -81.66, 30.34 ], \\\n", " [ -82.48, 27.97 ], \\\n", " [ -82.32, 29.65 ], \\\n", " [ -81.30, 28.42 ], \\\n", " [ -80.21, 25.78 ] ] )\n", "\n", "m = 1000\n", "c = cvt_step_florida ( g, m )\n", "print ( c )" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# CVT Iteration for Florida #\n", "\n", "Once we have a CVT step function, the CVT iteration is easy. Try taking 10 steps,\n", "and plot the locations of G before the iteration, and then at the end.\n", "\n", "You may also be able to plot the Voronoi diagram on top of the plot of the cities." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Code for CVT Iteration for Florida\n", "#\n", "def cvt_florida ( n, m, step_num ):\n", "\n", " g = florida_sample ( n )\n", "\n", "# vor = spatial.Voronoi ( g )\n", "# spatial.voronoi_plot_2d ( vor )\n", " plt.plot ( g[:,0], g[:,1], 'go' )\n", " plt.plot ( lon, lat, 'b-' )\n", " plt.axis ( 'Equal' )\n", " plt.title ( 'Before CVT iteration')\n", " plt.show ( )\n", "\n", " for step in range ( 0, step_num ):\n", " c = cvt_step_florida ( g, m )\n", " g = c.copy ( )\n", " return g\n", "#\n", "# Test the code.\n", "#\n", "n = 7\n", "m = 5000\n", "step_num = 10\n", "g = cvt_florida ( n, m, step_num )\n", "\n", "#vor = spatial.Voronoi ( g )\n", "#spatial.voronoi_plot_2d ( vor )\n", "plt.plot ( g[:,0], g[:,1], 'ro' )\n", "plt.plot ( lon, lat, 'b-' )\n", "plt.axis ( 'Equal' )\n", "plt.title ( 'After CVT iteration')\n", "plt.show ( )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.8" } }, "nbformat": 4, "nbformat_minor": 0 }