readding the projects folder, cleaned from GRASS and moved readme on root dir
10
README.md
|
@ -1,6 +1,6 @@
|
||||||
###Jupyter Notebooks###
|
###OSGeo-live###
|
||||||
|
This directory host a set of **[IPython Notebook](http://ipython.org/notebook.html)** that are used in the **[OSGeo-live](http://live.osgeo.org/en/index.html)** project to demostrate the capabilities of *Open Source Geospatial Tools* installed on the *OSGeo-Live*.
|
||||||
|
To start the *Jupyter Notebook* server on the *OSGeo-Live*, use the *GEOSPATIAL* menu *start Jupyter Notebook*.
|
||||||
|
To access to the server select in the same menu *Jupyter Notebook* it will open the browser pointing to the *Jupyter Notebook* dashboard running on ```htpp://localhost:8883``` .
|
||||||
|
|
||||||
In this repository are stored a set of [Jupyter notebooks](http://jupyter.org/) to demostrate the use of open source geospatial tools.
|
From the OSGeo-Live you can upgrade to the latest version of this repository running the command ```git pull``` from the notebook directory : ```/home/user/jupyter/notebooks```.
|
||||||
The ```OSGeo-live``` directory host notebooks that can be executed on the OSGeo live project.
|
|
||||||
Other Jupyter Notebooks, running open source tools not installed on the live will be stored in the current directory.
|
|
||||||
This repository can be rendered as static html using the [NBViewer](http://nbviewer.jupyter.org/github/OSGeo/OSGeoLive-Notebooks/tree/master/) service.
|
|
||||||
|
|
|
@ -0,0 +1,162 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import fiona\n",
|
||||||
|
"\n",
|
||||||
|
"from matplotlib.patches import Polygon\n",
|
||||||
|
"from shapely.geometry import shape, box\n",
|
||||||
|
"from shapely.ops import cascaded_union\n",
|
||||||
|
"\n",
|
||||||
|
"## Fiona, IPython Notebook interaction\n",
|
||||||
|
"## Live 8.5 * darkblue-b\n",
|
||||||
|
"##\n",
|
||||||
|
"## "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Terminal Commands\n",
|
||||||
|
"----------------------\n",
|
||||||
|
"``Shell script can be executed with results stored into python variables``\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shps = !ls /home/user/data/north_carolina/shape/*shp"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Task: quickly examine the bounding areas of a directory of shapefiles\n",
|
||||||
|
"------------------------------------------------------------------\n",
|
||||||
|
"* use ``fiona.open()`` to read data files on disk\n",
|
||||||
|
"* save the filename and bounding box into a python ``dict``\n",
|
||||||
|
"\n",
|
||||||
|
".\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"td = {}\n",
|
||||||
|
"\n",
|
||||||
|
"for shp in shps:\n",
|
||||||
|
" with fiona.open( shp, 'r') as inp:\n",
|
||||||
|
" td[ inp.name ] = inp.bounds\n",
|
||||||
|
"\n",
|
||||||
|
"## Fiona inp.bounds => ( lower_lng, lower_lat, upper_lng, upper_lat)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Create shapely geometry from the coords\n",
|
||||||
|
"## shapely/geometry/geo.py\n",
|
||||||
|
"## box(minx, miny, maxx, maxy, ccw=True)\n",
|
||||||
|
"\n",
|
||||||
|
"nboxes = []\n",
|
||||||
|
"for k,v in iter(td.iteritems()):\n",
|
||||||
|
" nboxes.append( box( v[0], v[1], v[2], v[3]) )\n",
|
||||||
|
"\n",
|
||||||
|
"print 'Found BBOXs: ',len(nboxes)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## create a single cascaded UNION too\n",
|
||||||
|
"dst_poly = cascaded_union(nboxes)\n",
|
||||||
|
"dst_poly.bounds"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Draw every BBOX for all files, transparently\n",
|
||||||
|
"## use matplotlib.Polygon to draw; let autoscale calculate the area\n",
|
||||||
|
"\n",
|
||||||
|
"fig, ax = plt.subplots(figsize=(12,12))\n",
|
||||||
|
"for polygon in nboxes:\n",
|
||||||
|
" mpl_poly = Polygon(np.array(polygon.exterior), facecolor=\"y\", alpha=0.02)\n",
|
||||||
|
" ax.add_patch(mpl_poly)\n",
|
||||||
|
"\n",
|
||||||
|
"## Indicate the exterior of the study area with a heavy line\n",
|
||||||
|
"ax.add_patch( Polygon(np.array(dst_poly.exterior), fill=False, lw=4, ec=\"b\", alpha=0.9) )\n",
|
||||||
|
"\n",
|
||||||
|
"ax.relim()\n",
|
||||||
|
"ax.autoscale()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,203 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.core.display import Image\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import fiona\n",
|
||||||
|
"import mapnik\n",
|
||||||
|
"import shapely.geometry\n",
|
||||||
|
"\n",
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"## Fiona, mapnik demo\n",
|
||||||
|
"## Live 8.5 * darkblue-b\n",
|
||||||
|
"##\n",
|
||||||
|
"## based on UoLPythonGroup/data-hack-0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"BASE_FOLDER = '/home/user/data/north_carolina/shape'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"c_shapefile = os.path.join(BASE_FOLDER, 'nc_state.shp')\n",
|
||||||
|
"f = fiona.open(c_shapefile)\n",
|
||||||
|
"shps = list(f)\n",
|
||||||
|
"print 'f: ',type(f)\n",
|
||||||
|
"print f.schema"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"type(f[2])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Use the Shapely geometry classes\n",
|
||||||
|
"## instantiate a Polygon from the Fiona collection "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import shapely.geometry\n",
|
||||||
|
"\n",
|
||||||
|
"geo = shapely.geometry.shape(f[0]['geometry'])\n",
|
||||||
|
"geo"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## plt.plot(*geo.xy) ## hmm not implemented"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Mapnik\n",
|
||||||
|
"## load the Mapnik python interfaces\n",
|
||||||
|
"## read the shapefile directly with Mapnik libs\n",
|
||||||
|
"## use the IPython Image interface as the drawing target"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def show_mapnik(m):\n",
|
||||||
|
" \"\"\"Returns an IPython Image of the rendered map.\"\"\"\n",
|
||||||
|
" im = mapnik.Image(m.width, m.height)\n",
|
||||||
|
" mapnik.render(m, im)\n",
|
||||||
|
" return Image(data=im.tostring('png32'))\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import mapnik\n",
|
||||||
|
"\n",
|
||||||
|
"m = mapnik.Map(600, 600)\n",
|
||||||
|
"\n",
|
||||||
|
"layer = mapnik.Layer('contour')\n",
|
||||||
|
"layer.datasource = mapnik.Shapefile(file=c_shapefile)\n",
|
||||||
|
"\n",
|
||||||
|
"style = mapnik.Style()\n",
|
||||||
|
"rule = mapnik.Rule()\n",
|
||||||
|
"\n",
|
||||||
|
"line_symbolizer = mapnik.LineSymbolizer(mapnik.Color('green'),0.4)\n",
|
||||||
|
"rule.symbols.append(line_symbolizer)\n",
|
||||||
|
"\n",
|
||||||
|
"m.layers.append(layer)\n",
|
||||||
|
"style.rules.append(rule)\n",
|
||||||
|
"m.append_style('My Style', style)\n",
|
||||||
|
"layer.styles.append('My Style')\n",
|
||||||
|
"\n",
|
||||||
|
"m.layers.append(layer)\n",
|
||||||
|
"m.zoom_all()\n",
|
||||||
|
"show_mapnik(m)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,460 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"from matplotlib.patches import Polygon\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import fiona\n",
|
||||||
|
"from shapely.geometry import shape\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import os\n",
|
||||||
|
"from collections import defaultdict, OrderedDict \n",
|
||||||
|
"\n",
|
||||||
|
"## Fiona, ogr2ogr, pyplot demo\n",
|
||||||
|
"## Live 8.5 * darkblue-b\n",
|
||||||
|
"##"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"`Demo Area Analysis in IPython Notebook`\n",
|
||||||
|
"\n",
|
||||||
|
" * Use North Carolina sample data supplied on the OSGeo Live \n",
|
||||||
|
" * Extract an area of interest\n",
|
||||||
|
" * Extract a subset of a state soils record based on a bounding box (BBOX)\n",
|
||||||
|
" * Plot geometry graphically\n",
|
||||||
|
" * Buffer the geometry by a fixed amount; calculate the areas of intersection for soil types\n",
|
||||||
|
" * Produce a chart of the totals with pyplot"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"infile = '/usr/local/share/data/north_carolina/shape/urbanarea.shp'\n",
|
||||||
|
"infile_soils = '/usr/local/share/data/north_carolina/shape/soils_general.shp'\n",
|
||||||
|
"\n",
|
||||||
|
"out_farmville = '/tmp/farmville_shp'\n",
|
||||||
|
"out_farmville_soils = '/tmp/farmville_soil_shp'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## -- Call the gdal/ogr tool suite to extract and subset the original statewide dataset\n",
|
||||||
|
"## use a feature of IPython Notebook to pass python variables to a command line\n",
|
||||||
|
"\n",
|
||||||
|
"!rm -rf $out_farmville\n",
|
||||||
|
"!ogr2ogr -f 'ESRI Shapefile' $out_farmville $infile -where \"NAME='Farmville'\" -dim 2 -a_srs EPSG:3358\n",
|
||||||
|
"\n",
|
||||||
|
"##-- advanced example -- raleigh is multiple polygons\n",
|
||||||
|
"#!rm -rf /tmp/raleigh1_shp\n",
|
||||||
|
"#!ogr2ogr -f 'ESRI Shapefile' /tmp/raleigh1_shp $infile -where \"NAME='Raleigh'\" -dim 2 -a_srs EPSG:3358\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## -- use the IPython notebook to get help on py module Fiona\n",
|
||||||
|
"# fiona?\n",
|
||||||
|
"# fiona.open?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## open and inspect a Shapefile using Fiona\n",
|
||||||
|
"## a POLYGON record could be very long, so dont print the record w/ geometry\n",
|
||||||
|
"with fiona.open( out_farmville ) as f:\n",
|
||||||
|
" crs = f.crs\n",
|
||||||
|
" print 'CRS:',crs\n",
|
||||||
|
" rec = f.next()\n",
|
||||||
|
" #print rec\n",
|
||||||
|
" print 'SHAPELY REC:',rec.keys()\n",
|
||||||
|
" print 'SHAPEFILE FLDS: ',rec['properties'].keys()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with fiona.open( out_farmville,'r') as inp:\n",
|
||||||
|
" #print dir(inp) ## whats inside this object?\n",
|
||||||
|
" #print inp.bounds\n",
|
||||||
|
" \n",
|
||||||
|
" ## take the bounding box of the area of interest\n",
|
||||||
|
" ## add 300 meters on each side for aesthetics\n",
|
||||||
|
" ## (left, bottom, right, top)\n",
|
||||||
|
" left_bnds = inp.bounds[0] - 300\n",
|
||||||
|
" bottom_bnds = inp.bounds[1] - 300\n",
|
||||||
|
" right_bnds = inp.bounds[2] + 300\n",
|
||||||
|
" top_bnds = inp.bounds[3] + 300\n",
|
||||||
|
"\n",
|
||||||
|
"## echo one variable to sanity check\n",
|
||||||
|
"#left_bnds"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## ogr2ogr ... -clipsrc [xmin ymin xmax ymax] ...\n",
|
||||||
|
"\n",
|
||||||
|
"!rm -rf $out_farmville_soils\n",
|
||||||
|
"!ogr2ogr -f 'ESRI Shapefile' $out_farmville_soils $infile_soils -clipsrc $left_bnds $bottom_bnds $right_bnds $top_bnds -dim 2 -a_srs EPSG:3358\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## ----------------------------------------\n",
|
||||||
|
"## plot_multipolygon function\n",
|
||||||
|
"## Author: Kelsey Jordahl, Enthought\n",
|
||||||
|
"## Scipy 2013 geospatial tutorial\n",
|
||||||
|
"\n",
|
||||||
|
"def plot_polygon(ax, poly, color='black'):\n",
|
||||||
|
" a = np.asarray(poly.exterior)\n",
|
||||||
|
" ax.add_patch(Polygon(a, facecolor=color, alpha=0.5))\n",
|
||||||
|
" ax.plot(a[:, 0], a[:, 1], color=color)\n",
|
||||||
|
"\n",
|
||||||
|
"def plot_multipolygon(ax, geom, color='red'):\n",
|
||||||
|
" \"\"\" Can safely call with either Polygon or Multipolygon geometry\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" if geom.type == 'Polygon':\n",
|
||||||
|
" plot_polygon(ax, geom, color)\n",
|
||||||
|
" elif geom.type == 'MultiPolygon':\n",
|
||||||
|
" for poly in geom.geoms:\n",
|
||||||
|
" plot_polygon(ax, poly, color)\n",
|
||||||
|
"## ----------------------------------------"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"soil_colors = ['green', 'brown', '#ceFFde']\n",
|
||||||
|
"\n",
|
||||||
|
"dst_geoms = OrderedDict()\n",
|
||||||
|
"src_geoms = OrderedDict()\n",
|
||||||
|
"\n",
|
||||||
|
"cnt = 0\n",
|
||||||
|
"with fiona.open( out_farmville ) as f:\n",
|
||||||
|
" for rec in f:\n",
|
||||||
|
" src_geoms[cnt] = shape(rec['geometry'])\n",
|
||||||
|
" cnt += 1\n",
|
||||||
|
"\n",
|
||||||
|
"cnt = 0\n",
|
||||||
|
"with fiona.open( out_farmville_soils ) as f:\n",
|
||||||
|
" for rec in f:\n",
|
||||||
|
" ## check the geometry type if desired\n",
|
||||||
|
" ## intersections may result in POINT or LINESTRING\n",
|
||||||
|
" if rec['geometry']['type'] != 'Polygon':\n",
|
||||||
|
" continue\n",
|
||||||
|
" gsl = rec['properties']['GSL_NAME']\n",
|
||||||
|
" dst_geoms[gsl] = shape(rec['geometry'])\n",
|
||||||
|
" cnt += 1\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"fig, ax = plt.subplots(figsize=(11,11))\n",
|
||||||
|
"plt.title(\"Soil Class and Urban Area via N. Carolina Sample Dataset (CC-by-SA) OSGeo\")\n",
|
||||||
|
"\n",
|
||||||
|
"cnt = 0\n",
|
||||||
|
"for key in dst_geoms :\n",
|
||||||
|
" ## cnt mod (number of colors) is always a safe index into colors[]\n",
|
||||||
|
" color = soil_colors[ cnt%len(soil_colors) ]\n",
|
||||||
|
" plot_multipolygon(ax, dst_geoms[key], color=color)\n",
|
||||||
|
" cnt += 1\n",
|
||||||
|
" \n",
|
||||||
|
"cnt = 0\n",
|
||||||
|
"color = 'gray'\n",
|
||||||
|
"with fiona.open( out_farmville ) as f:\n",
|
||||||
|
" for rec in f:\n",
|
||||||
|
" plot_multipolygon(ax, src_geoms[cnt], color=color)\n",
|
||||||
|
" cnt += 1\n",
|
||||||
|
"\n",
|
||||||
|
"#ax.add_patch(Polygon( src_geoms[0].centroid, facecolor='black', alpha=0.5))\n",
|
||||||
|
"\n",
|
||||||
|
"labels = ax.get_xticklabels() \n",
|
||||||
|
"for label in labels: \n",
|
||||||
|
" label.set_rotation(90) \n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## show all the variables defined so far\n",
|
||||||
|
"%whos"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## what geomtries have been created ?\n",
|
||||||
|
"print 'src_geoms len: ',len(src_geoms)\n",
|
||||||
|
"print 'dst_geoms len: ',len(dst_geoms )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## buffer the urbanarea by N meters\n",
|
||||||
|
"## save the result in the source-geometry list\n",
|
||||||
|
"\n",
|
||||||
|
"src_geom_buffered = src_geoms[0].buffer(166)\n",
|
||||||
|
"src_geoms[1] = src_geom_buffered\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fig, ax = plt.subplots(figsize=(11,11))\n",
|
||||||
|
"\n",
|
||||||
|
"cnt = 0\n",
|
||||||
|
"for key in dst_geoms :\n",
|
||||||
|
" color = soil_colors[ cnt%len(soil_colors) ]\n",
|
||||||
|
" plot_multipolygon(ax, dst_geoms[key], color=color)\n",
|
||||||
|
" cnt += 1\n",
|
||||||
|
"\n",
|
||||||
|
"plot_multipolygon(ax, src_geoms[1], color='gray')\n",
|
||||||
|
"plot_multipolygon(ax, src_geoms[0], color='gray')\n",
|
||||||
|
" \n",
|
||||||
|
"labels = ax.get_xticklabels() \n",
|
||||||
|
"for label in labels: \n",
|
||||||
|
" label.set_rotation(90) \n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## take the intersection of each soil area against the buffered poly of interest\n",
|
||||||
|
"## store into a third convenient list\n",
|
||||||
|
"res_geoms = OrderedDict()\n",
|
||||||
|
"for key in dst_geoms:\n",
|
||||||
|
" res_geoms[key] = src_geom_buffered.intersection( dst_geoms[key] )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fig, ax = plt.subplots(figsize=(11,11))\n",
|
||||||
|
"\n",
|
||||||
|
"cnt = 0\n",
|
||||||
|
"for key in res_geoms :\n",
|
||||||
|
" color = soil_colors[ cnt%len(soil_colors) ]\n",
|
||||||
|
" plot_multipolygon(ax, res_geoms[key], color=color)\n",
|
||||||
|
" cnt += 1\n",
|
||||||
|
" \n",
|
||||||
|
"labels = ax.get_xticklabels() \n",
|
||||||
|
"for label in labels: \n",
|
||||||
|
" label.set_rotation(90) \n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## define a lookup table of mock attributes of each soil type\n",
|
||||||
|
"## (substitute a real data source here)\n",
|
||||||
|
"\n",
|
||||||
|
"soils_lkup = {\n",
|
||||||
|
" 'NC038' : { 'TEXTURE_CLAY':0.53, 'TEXTURE_SILT':0.33, 'TEXTURE_SAND':0.44 },\n",
|
||||||
|
" 'NC035' : { 'TEXTURE_CLAY':0.70, 'TEXTURE_SILT':0.33, 'TEXTURE_SAND':0.44 },\n",
|
||||||
|
" 'NC034' : { 'TEXTURE_CLAY':0.23, 'TEXTURE_SILT':0.74, 'TEXTURE_SAND':0.44 }\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"## get a rough total area for display purposes\n",
|
||||||
|
"sum_areas = 0.0\n",
|
||||||
|
"for key in res_geoms:\n",
|
||||||
|
" sum_areas += int(res_geoms[key].area)\n",
|
||||||
|
"\n",
|
||||||
|
"## record the area and percentage area in a convenient dictionary \n",
|
||||||
|
"tdd = {}\n",
|
||||||
|
"for key in res_geoms:\n",
|
||||||
|
" tdd[key] = soils_lkup[key]\n",
|
||||||
|
" tdd[key]['area'] = int(res_geoms[key].area)\n",
|
||||||
|
" tdd[key]['perc'] = int((res_geoms[key].area/sum_areas)*100)/100.0\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Now all attributes for visualization are available in a single dict\n",
|
||||||
|
"## in a larger system this could be delivered for serving graphical reports\n",
|
||||||
|
"tdd"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## What matplotlib pylot graphs are available ?\n",
|
||||||
|
"## http://matplotlib.org/api/pyplot_api.html\n",
|
||||||
|
"##\n",
|
||||||
|
"## Use IPython built-in help to discover pie chart attributes\n",
|
||||||
|
"\n",
|
||||||
|
"plt.pie?\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# The slices will be ordered and plotted counter-clockwise.\n",
|
||||||
|
"labels = [ 'NC034', 'NC035', 'NC038' ]\n",
|
||||||
|
"sizes = [ tdd['NC034']['perc']*100, tdd['NC035']['perc']*100, tdd['NC038']['perc']*100 ]\n",
|
||||||
|
"pie_colors = ['lightcoral', '#eeefee', 'yellowgreen']\n",
|
||||||
|
"# only \"explode\" the 2nd slice (i.e. 'NC035')\n",
|
||||||
|
"explode = (0, 0.1, 0)\n",
|
||||||
|
"\n",
|
||||||
|
"plt.pie(sizes, explode=explode, labels=labels, colors=pie_colors,\n",
|
||||||
|
" autopct='%1.1f%%', shadow=True, startangle=90)\n",
|
||||||
|
"# Set aspect ratio to be equal so that pie is drawn as a circle.\n",
|
||||||
|
"plt.axis('equal')\n",
|
||||||
|
"\n",
|
||||||
|
"plt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
State,Unemployment
|
||||||
|
AL,7.1
|
||||||
|
AK,6.8
|
||||||
|
AZ,8.1
|
||||||
|
AR,7.2
|
||||||
|
CA,10.1
|
||||||
|
CO,7.7
|
||||||
|
CT,8.4
|
||||||
|
DE,7.1
|
||||||
|
FL,8.2
|
||||||
|
GA,8.8
|
||||||
|
HI,5.4
|
||||||
|
ID,6.6
|
||||||
|
IL,8.8
|
||||||
|
IN,8.4
|
||||||
|
IA,5.1
|
||||||
|
KS,5.6
|
||||||
|
KY,8.1
|
||||||
|
LA,5.9
|
||||||
|
ME,7.2
|
||||||
|
MD,6.8
|
||||||
|
MA,6.7
|
||||||
|
MI,9.1
|
||||||
|
MN,5.6
|
||||||
|
MS,9.1
|
||||||
|
MO,6.7
|
||||||
|
MT,5.8
|
||||||
|
NE,3.9
|
||||||
|
NV,10.3
|
||||||
|
NH,5.7
|
||||||
|
NJ,9.6
|
||||||
|
NM,6.8
|
||||||
|
NY,8.4
|
||||||
|
NC,9.4
|
||||||
|
ND,3.2
|
||||||
|
OH,6.9
|
||||||
|
OK,5.2
|
||||||
|
OR,8.5
|
||||||
|
PA,8
|
||||||
|
RI,10.1
|
||||||
|
SC,8.8
|
||||||
|
SD,4.4
|
||||||
|
TN,7.8
|
||||||
|
TX,6.4
|
||||||
|
UT,5.5
|
||||||
|
VT,5
|
||||||
|
VA,5.8
|
||||||
|
WA,7.8
|
||||||
|
WV,7.5
|
||||||
|
WI,6.8
|
||||||
|
WY,5.1
|
|
|
@ -0,0 +1 @@
|
||||||
|
[{"WA": 7.8, "DE": 7.1, "WI": 6.8, "WV": 7.5, "HI": 5.4, "FL": 8.2, "WY": 5.1, "NH": 5.7, "NJ": 9.6, "NM": 6.8, "TX": 6.4, "LA": 5.9, "NC": 9.4, "ND": 3.2, "NE": 3.9, "TN": 7.8, "NY": 8.4, "PA": 8.0, "CA": 10.1, "NV": 10.3, "VA": 5.8, "CO": 7.7, "AK": 6.8, "AL": 7.1, "AR": 7.2, "VT": 5.0, "IL": 8.8, "GA": 8.8, "IN": 8.4, "IA": 5.1, "OK": 5.2, "AZ": 8.1, "ID": 6.6, "CT": 8.4, "ME": 7.2, "MD": 6.8, "MA": 6.7, "OH": 6.9, "UT": 5.5, "MO": 6.7, "MN": 5.6, "MI": 9.1, "RI": 10.1, "KS": 5.6, "MT": 5.8, "MS": 9.1, "SC": 8.8, "KY": 8.1, "OR": 8.5, "SD": 4.4}]
|
|
@ -0,0 +1,98 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import HTML\n",
|
||||||
|
"import folium\n",
|
||||||
|
"\n",
|
||||||
|
"def inline_map(map):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds the HTML source of the map directly into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" This method will not work if the map depends on any files (json data). Also this uses\n",
|
||||||
|
" the HTML5 srcdoc attribute, which may not be supported in all browsers.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map._build_map()\n",
|
||||||
|
" return HTML('<iframe srcdoc=\"{srcdoc}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(srcdoc=map.HTML.replace('\"', '"')))\n",
|
||||||
|
"\n",
|
||||||
|
"def embed_map(map, path=\"m213map.html\"):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds a linked iframe to the map into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" Note: this method will not capture the source of the map into the notebook.\n",
|
||||||
|
" This method should work for all maps (as long as they use relative urls).\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map.create_map(path=path)\n",
|
||||||
|
" return HTML('<iframe src=\"files/{path}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(path=path))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map = folium.Map(width=500,height=500,location=[44, -73], zoom_start=4)\n",
|
||||||
|
"\n",
|
||||||
|
"map.simple_marker([40.67, -73.94], popup='Add <b>popup</b> text here.',marker_color='green',marker_icon='ok-sign',clustered_marker=True)\n",
|
||||||
|
"map.simple_marker([44.67, -73.94], popup='Add <b>popup</b> text here.',marker_color='red',marker_icon='remove-sign',clustered_marker=True)\n",
|
||||||
|
"map.simple_marker([44.67, -71.94], popup='Add <b>popup</b> text here.',clustered_marker=True)\n",
|
||||||
|
"\n",
|
||||||
|
"map.circle_marker([44, -71], popup='', fill_color='#ff0000', radius=5000, line_color='#ff0000')\n",
|
||||||
|
"\n",
|
||||||
|
"points1 = [40,-71]\n",
|
||||||
|
"points2 = [42,-73]\n",
|
||||||
|
"map.line([points1, points2])\n",
|
||||||
|
"\n",
|
||||||
|
"inline_map(map)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,288 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import folium\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"#folium.initialize_notebook()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#Standard OSM\n",
|
||||||
|
"map_osm = folium.Map(location=[45.5236, -122.6750])\n",
|
||||||
|
"map_osm"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"stamen = folium.Map(location=[45.5236, -122.6750], tiles='Stamen Toner',\n",
|
||||||
|
" zoom_start=13)\n",
|
||||||
|
"stamen"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"mt_hood = folium.Map(location=[45.372, -121.6972], zoom_start=12,\n",
|
||||||
|
" tiles='Stamen Terrain')\n",
|
||||||
|
"mt_hood.simple_marker([45.3288, -121.6625], popup='Mt. Hood Meadows')\n",
|
||||||
|
"mt_hood.simple_marker([45.3311, -121.7113], popup='Timberline Lodge')\n",
|
||||||
|
"mt_hood"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"portland = folium.Map(location=[45.5236, -122.6750], tiles='Stamen Toner',\n",
|
||||||
|
" zoom_start=13)\n",
|
||||||
|
"portland.simple_marker(location=[45.5244, -122.6699], popup='The Waterfront')\n",
|
||||||
|
"portland.circle_marker(location=[45.5215, -122.6261], radius=500,\n",
|
||||||
|
" popup='Laurelhurst Park', line_color='#3186cc',\n",
|
||||||
|
" fill_color='#3186cc')\n",
|
||||||
|
"portland"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"latlng = folium.Map(location=[46.1991, -122.1889], tiles='Stamen Terrain',\n",
|
||||||
|
" zoom_start=13)\n",
|
||||||
|
"latlng.lat_lng_popover()\n",
|
||||||
|
"latlng"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"waypoints = folium.Map(location=[46.8527, -121.7649], tiles='Stamen Terrain',\n",
|
||||||
|
" zoom_start=13)\n",
|
||||||
|
"waypoints.simple_marker(location=[46.8354, -121.7325], popup='Camp Muir')\n",
|
||||||
|
"waypoints.click_for_marker(popup='Waypoint')\n",
|
||||||
|
"waypoints"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"polygons = folium.Map(location=[45.5236, -122.6750], zoom_start=13)\n",
|
||||||
|
"polygons.polygon_marker(location=[45.5012, -122.6655], popup='Ross Island Bridge',\n",
|
||||||
|
" fill_color='#132b5e', num_sides=3, radius=10)\n",
|
||||||
|
"polygons.polygon_marker(location=[45.5132, -122.6708], popup='Hawthorne Bridge',\n",
|
||||||
|
" fill_color='#45647d', num_sides=4, radius=10)\n",
|
||||||
|
"polygons.polygon_marker(location=[45.5275, -122.6692], popup='Steel Bridge',\n",
|
||||||
|
" fill_color='#769d96', num_sides=6, radius=10)\n",
|
||||||
|
"polygons.polygon_marker(location=[45.5318, -122.6745], popup='Broadway Bridge',\n",
|
||||||
|
" fill_color='#769d96', num_sides=8, radius=10)\n",
|
||||||
|
"polygons"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false,
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"state_geo = r'us-states.json'\n",
|
||||||
|
"state_unemployment = r'US_Unemployment_Oct2012.csv'\n",
|
||||||
|
"\n",
|
||||||
|
"state_data = pd.read_csv(state_unemployment)\n",
|
||||||
|
"\n",
|
||||||
|
"#Let Folium determine the scale\n",
|
||||||
|
"states = folium.Map(location=[48, -102], zoom_start=3)\n",
|
||||||
|
"states.geo_json(geo_path=state_geo, data=state_data,\n",
|
||||||
|
" columns=['State', 'Unemployment'],\n",
|
||||||
|
" key_on='feature.id',\n",
|
||||||
|
" fill_color='YlGn', fill_opacity=0.7, line_opacity=0.2,\n",
|
||||||
|
" legend_name='Unemployment Rate (%)')\n",
|
||||||
|
"states.create_map(path='us_state_map.html')\n",
|
||||||
|
"states"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"states2 = folium.Map(location=[48, -102], zoom_start=3)\n",
|
||||||
|
"states2.geo_json(geo_path=state_geo, data=state_data,\n",
|
||||||
|
" columns=['State', 'Unemployment'],\n",
|
||||||
|
" threshold_scale=[5, 6, 7, 8, 9, 10],\n",
|
||||||
|
" key_on='feature.id',\n",
|
||||||
|
" fill_color='BuPu', fill_opacity=0.7, line_opacity=0.5,\n",
|
||||||
|
" legend_name='Unemployment Rate (%)',\n",
|
||||||
|
" reset=True)\n",
|
||||||
|
"states2.create_map(path='us_state_map_2.html')\n",
|
||||||
|
"states2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"county_data = r'us_county_data.csv'\n",
|
||||||
|
"county_geo = r'us_counties_20m_topo.json'\n",
|
||||||
|
"\n",
|
||||||
|
"#Read into Dataframe, cast to string for consistency\n",
|
||||||
|
"df = pd.read_csv(county_data, na_values=[' '])\n",
|
||||||
|
"df['FIPS_Code'] = df['FIPS_Code'].astype(str)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def set_id(fips):\n",
|
||||||
|
" '''Modify FIPS code to match GeoJSON property'''\n",
|
||||||
|
" if fips == '0':\n",
|
||||||
|
" return None\n",
|
||||||
|
" elif len(fips) <= 4:\n",
|
||||||
|
" return ''.join(['0500000US0', fips])\n",
|
||||||
|
" else:\n",
|
||||||
|
" return ''.join(['0500000US', fips])\n",
|
||||||
|
"\n",
|
||||||
|
"#Apply set_id, drop NaN\n",
|
||||||
|
"df['GEO_ID'] = df['FIPS_Code'].apply(set_id)\n",
|
||||||
|
"df = df.dropna()\n",
|
||||||
|
"\n",
|
||||||
|
"#Number of employed with auto scale\n",
|
||||||
|
"map_1 = folium.Map(location=[48, -102], zoom_start=3)\n",
|
||||||
|
"map_1.geo_json(geo_path=county_geo, data_out='data1.json', data=df,\n",
|
||||||
|
" columns=['GEO_ID', 'Employed_2011'],\n",
|
||||||
|
" key_on='feature.id',\n",
|
||||||
|
" fill_color='YlOrRd', fill_opacity=0.7, line_opacity=0.3,\n",
|
||||||
|
" topojson='objects.us_counties_20m')\n",
|
||||||
|
"map_1.create_map(path='map_1.html')\n",
|
||||||
|
"map_1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map_2 = folium.Map(location=[40, -99], zoom_start=4)\n",
|
||||||
|
"map_2.geo_json(geo_path=county_geo, data_out='data2.json', data=df,\n",
|
||||||
|
" columns=['GEO_ID', 'Unemployment_rate_2011'],\n",
|
||||||
|
" key_on='feature.id',\n",
|
||||||
|
" threshold_scale=[0, 5, 7, 9, 11, 13],\n",
|
||||||
|
" fill_color='YlGnBu', line_opacity=0.3,\n",
|
||||||
|
" legend_name='Unemployment Rate 2011 (%)',\n",
|
||||||
|
" topojson='objects.us_counties_20m')\n",
|
||||||
|
"map_2.create_map(path='map_2.html')\n",
|
||||||
|
"map_2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map_3 = folium.Map(location=[40, -99], zoom_start=4)\n",
|
||||||
|
"map_3.geo_json(geo_path=county_geo, data_out='data3.json', data=df,\n",
|
||||||
|
" columns=['GEO_ID', 'Median_Household_Income_2011'],\n",
|
||||||
|
" key_on='feature.id',\n",
|
||||||
|
" fill_color='PuRd', line_opacity=0.3,\n",
|
||||||
|
" legend_name='Median Household Income 2011 ($)',\n",
|
||||||
|
" topojson='objects.us_counties_20m')\n",
|
||||||
|
"map_3.create_map(path='map_3.html')\n",
|
||||||
|
"map_3"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,100 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import HTML\n",
|
||||||
|
"import folium"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def inline_map(map):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds the HTML source of the map directly into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" This method will not work if the map depends on any files (json data). Also this uses\n",
|
||||||
|
" the HTML5 srcdoc attribute, which may not be supported in all browsers.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map._build_map()\n",
|
||||||
|
" return HTML('<iframe srcdoc=\"{srcdoc}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(srcdoc=map.HTML.replace('\"', '"')))\n",
|
||||||
|
"\n",
|
||||||
|
"def embed_map(map, path=\"m213map.html\"):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds a linked iframe to the map into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" Note: this method will not capture the source of the map into the notebook.\n",
|
||||||
|
" This method should work for all maps (as long as they use relative urls).\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map.create_map(path=path)\n",
|
||||||
|
" return HTML('<iframe src=\"files/{path}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(path=path))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map = folium.Map(width=500,height=500,location=[44, -73], zoom_start=4)\n",
|
||||||
|
"\n",
|
||||||
|
"map.simple_marker([40.67, -73.94], popup='Add <b>popup</b> text here.',marker_color='green',marker_icon='ok-sign')\n",
|
||||||
|
"map.simple_marker([44.67, -73.94], popup='Add <b>popup</b> text here.',marker_color='red',marker_icon='remove-sign')\n",
|
||||||
|
"map.simple_marker([44.67, -71.94], popup='Add <b>popup</b> text here.')\n",
|
||||||
|
"inline_map(map)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import folium\n",
|
||||||
|
"from IPython.display import HTML, Javascript, display"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def inline_map(m):\n",
|
||||||
|
" m._build_map()\n",
|
||||||
|
" srcdoc = m.HTML.replace('\"', '"')\n",
|
||||||
|
" embed = HTML('<iframe srcdoc=\"{srcdoc}\" '\n",
|
||||||
|
" 'style=\"width: 100%; height: 500px; '\n",
|
||||||
|
" 'border: none\"></iframe>'.format(srcdoc=srcdoc))\n",
|
||||||
|
" return embed"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#station = st_list[st_list.keys()[0]]\n",
|
||||||
|
"#map = folium.Map(location=[station[\"lat\"], station[\"lon\"]], zoom_start=4)\n",
|
||||||
|
"\n",
|
||||||
|
"map = folium.Map(location=[41, -71], zoom_start=4)\n",
|
||||||
|
"#map.line(get_coordinates(bounding_box, bounding_box_type), line_color='#FF0000', line_weight=5)\n",
|
||||||
|
"\n",
|
||||||
|
"#plot the obs station, \n",
|
||||||
|
"#for st in st_list: \n",
|
||||||
|
"map.simple_marker([41,-72], popup='something something...<br>',marker_color=\"green\",marker_icon=\"arrow-up\",icon_angle=0)\n",
|
||||||
|
"inline_map(map) "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,121 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import HTML\n",
|
||||||
|
"import folium"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def inline_map(map):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds the HTML source of the map directly into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" This method will not work if the map depends on any files (json data). Also this uses\n",
|
||||||
|
" the HTML5 srcdoc attribute, which may not be supported in all browsers.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map._build_map()\n",
|
||||||
|
" return HTML('<iframe srcdoc=\"{srcdoc}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(srcdoc=map.HTML.replace('\"', '"')))\n",
|
||||||
|
"\n",
|
||||||
|
"def embed_map(map, path=\"map.html\"):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds a linked iframe to the map into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" Note: this method will not capture the source of the map into the notebook.\n",
|
||||||
|
" This method should work for all maps (as long as they use relative urls).\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map.create_map(path=path)\n",
|
||||||
|
" return HTML('<iframe src=\"files/{path}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(path=path))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map = folium.Map(width=800,height=600,location=[44, -73], zoom_start=3)\n",
|
||||||
|
"\n",
|
||||||
|
"map.add_wms_layer(wms_name=\"Temperature\",\n",
|
||||||
|
" wms_url=\"http://gis.srh.noaa.gov/arcgis/services/NDFDTemps/MapServer/WMSServer\",\n",
|
||||||
|
" wms_format=\"image/png\",\n",
|
||||||
|
" wms_layers= 16\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
"map.add_tile_layer(tile_name='hfradar 1km',\n",
|
||||||
|
" tile_url='http://hfradar.ndbc.noaa.gov/tilesavg.php?s=10&e=100&x={x}&y={y}&z={z}&t=2014-8-18 14:00:00&rez=1')\n",
|
||||||
|
"map.add_tile_layer(tile_name='hfradar 2km',\n",
|
||||||
|
" tile_url='http://hfradar.ndbc.noaa.gov/tilesavg.php?s=10&e=100&x={x}&y={y}&z={z}&t=2014-8-18 14:00:00&rez=2')\n",
|
||||||
|
"map.add_tile_layer(tile_name='hfradar 6km',\n",
|
||||||
|
" tile_url='http://hfradar.ndbc.noaa.gov/tilesavg.php?s=10&e=100&x={x}&y={y}&z={z}&t=2014-8-18 14:00:00&rez=6')\n",
|
||||||
|
"\n",
|
||||||
|
"map.add_layers_to_map()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"inline_map(map) "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,165 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legend {
|
||||||
|
padding: 0px 0px;
|
||||||
|
font: 10px sans-serif;
|
||||||
|
background: white;
|
||||||
|
background: rgba(255,255,255,0.8);
|
||||||
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key path {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="map" style="width: 100%; height: 100%"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
queue()
|
||||||
|
.defer(d3.json, 'data1.json')
|
||||||
|
.defer(d3.json, 'us_counties_20m_topo.json')
|
||||||
|
.await(makeMap)
|
||||||
|
|
||||||
|
function onEachFeature(feature, layer) {
|
||||||
|
// does this feature have a property named popupContent?
|
||||||
|
if (feature.properties && feature.properties.popupContent) {
|
||||||
|
layer.bindPopup(feature.properties.popupContent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function makeMap(error, data_1,tjson_1) {
|
||||||
|
|
||||||
|
|
||||||
|
topo_1 = topojson.feature(tjson_1, tjson_1.objects.us_counties_20m);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function matchKey(datapoint, key_variable){
|
||||||
|
if (typeof key_variable[0][datapoint] === 'undefined') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return parseFloat(key_variable[0][datapoint]);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var color = d3.scale.threshold()
|
||||||
|
.domain([40.0, 10000.0, 30000.0, 70000.0, 100000.0])
|
||||||
|
.range(['#FFFFB2', '#FED976', '#FEB24C', '#FD8D3C', '#FC4E2A', '#E31A1C']);
|
||||||
|
|
||||||
|
|
||||||
|
var map = L.map('map').setView([48, -102], 3);
|
||||||
|
|
||||||
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||||
|
maxZoom: 18,
|
||||||
|
minZoom: 1,
|
||||||
|
attribution: 'Map data (c) <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
|
||||||
|
}).addTo(map);
|
||||||
|
|
||||||
|
|
||||||
|
function style_1(feature) {
|
||||||
|
return {
|
||||||
|
fillColor: color(matchKey(feature.id, data_1)),
|
||||||
|
weight: 1,
|
||||||
|
opacity: 0.3,
|
||||||
|
color: 'black',
|
||||||
|
fillOpacity: 0.7
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
gJson_layer_1 = L.geoJson(topo_1, {style: style_1,onEachFeature: onEachFeature}).addTo(map)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var legend = L.control({position: 'topright'});
|
||||||
|
|
||||||
|
legend.onAdd = function (map) {var div = L.DomUtil.create('div', 'legend'); return div};
|
||||||
|
|
||||||
|
legend.addTo(map);
|
||||||
|
|
||||||
|
var x = d3.scale.linear()
|
||||||
|
.domain([0, 110000])
|
||||||
|
.range([0, 400]);
|
||||||
|
|
||||||
|
var xAxis = d3.svg.axis()
|
||||||
|
.scale(x)
|
||||||
|
.orient("top")
|
||||||
|
.tickSize(1)
|
||||||
|
.tickValues([40.0, 10000.0, 30000.0, 70000.0, 100000.0]);
|
||||||
|
|
||||||
|
var svg = d3.select(".legend.leaflet-control").append("svg")
|
||||||
|
.attr("id", 'legend')
|
||||||
|
.attr("width", 450)
|
||||||
|
.attr("height", 40);
|
||||||
|
|
||||||
|
var g = svg.append("g")
|
||||||
|
.attr("class", "key")
|
||||||
|
.attr("transform", "translate(25,16)");
|
||||||
|
|
||||||
|
g.selectAll("rect")
|
||||||
|
.data(color.range().map(function(d, i) {
|
||||||
|
return {
|
||||||
|
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
|
||||||
|
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
|
||||||
|
z: d
|
||||||
|
};
|
||||||
|
}))
|
||||||
|
.enter().append("rect")
|
||||||
|
.attr("height", 10)
|
||||||
|
.attr("x", function(d) { return d.x0; })
|
||||||
|
.attr("width", function(d) { return d.x1 - d.x0; })
|
||||||
|
.style("fill", function(d) { return d.z; });
|
||||||
|
|
||||||
|
g.call(xAxis).append("text")
|
||||||
|
.attr("class", "caption")
|
||||||
|
.attr("y", 21)
|
||||||
|
.text('Employed_2011');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
|
@ -0,0 +1,165 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legend {
|
||||||
|
padding: 0px 0px;
|
||||||
|
font: 10px sans-serif;
|
||||||
|
background: white;
|
||||||
|
background: rgba(255,255,255,0.8);
|
||||||
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key path {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="map" style="width: 100%; height: 100%"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
queue()
|
||||||
|
.defer(d3.json, 'data2.json')
|
||||||
|
.defer(d3.json, 'us_counties_20m_topo.json')
|
||||||
|
.await(makeMap)
|
||||||
|
|
||||||
|
function onEachFeature(feature, layer) {
|
||||||
|
// does this feature have a property named popupContent?
|
||||||
|
if (feature.properties && feature.properties.popupContent) {
|
||||||
|
layer.bindPopup(feature.properties.popupContent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function makeMap(error, data_1,tjson_1) {
|
||||||
|
|
||||||
|
|
||||||
|
topo_1 = topojson.feature(tjson_1, tjson_1.objects.us_counties_20m);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function matchKey(datapoint, key_variable){
|
||||||
|
if (typeof key_variable[0][datapoint] === 'undefined') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return parseFloat(key_variable[0][datapoint]);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var color = d3.scale.threshold()
|
||||||
|
.domain([0, 5, 7, 9, 11, 13])
|
||||||
|
.range(['#FFFFCC', '#C7E9B4', '#7FCDBB', '#41B6C4', '#1D91C0', '#225EA8', '#0C2C84']);
|
||||||
|
|
||||||
|
|
||||||
|
var map = L.map('map').setView([40, -99], 4);
|
||||||
|
|
||||||
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||||
|
maxZoom: 18,
|
||||||
|
minZoom: 1,
|
||||||
|
attribution: 'Map data (c) <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
|
||||||
|
}).addTo(map);
|
||||||
|
|
||||||
|
|
||||||
|
function style_1(feature) {
|
||||||
|
return {
|
||||||
|
fillColor: color(matchKey(feature.id, data_1)),
|
||||||
|
weight: 1,
|
||||||
|
opacity: 0.3,
|
||||||
|
color: 'black',
|
||||||
|
fillOpacity: 0.6
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
gJson_layer_1 = L.geoJson(topo_1, {style: style_1,onEachFeature: onEachFeature}).addTo(map)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var legend = L.control({position: 'topright'});
|
||||||
|
|
||||||
|
legend.onAdd = function (map) {var div = L.DomUtil.create('div', 'legend'); return div};
|
||||||
|
|
||||||
|
legend.addTo(map);
|
||||||
|
|
||||||
|
var x = d3.scale.linear()
|
||||||
|
.domain([0, 14])
|
||||||
|
.range([0, 400]);
|
||||||
|
|
||||||
|
var xAxis = d3.svg.axis()
|
||||||
|
.scale(x)
|
||||||
|
.orient("top")
|
||||||
|
.tickSize(1)
|
||||||
|
.tickValues([0, 5, 7, 9, 11, 13]);
|
||||||
|
|
||||||
|
var svg = d3.select(".legend.leaflet-control").append("svg")
|
||||||
|
.attr("id", 'legend')
|
||||||
|
.attr("width", 450)
|
||||||
|
.attr("height", 40);
|
||||||
|
|
||||||
|
var g = svg.append("g")
|
||||||
|
.attr("class", "key")
|
||||||
|
.attr("transform", "translate(25,16)");
|
||||||
|
|
||||||
|
g.selectAll("rect")
|
||||||
|
.data(color.range().map(function(d, i) {
|
||||||
|
return {
|
||||||
|
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
|
||||||
|
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
|
||||||
|
z: d
|
||||||
|
};
|
||||||
|
}))
|
||||||
|
.enter().append("rect")
|
||||||
|
.attr("height", 10)
|
||||||
|
.attr("x", function(d) { return d.x0; })
|
||||||
|
.attr("width", function(d) { return d.x1 - d.x0; })
|
||||||
|
.style("fill", function(d) { return d.z; });
|
||||||
|
|
||||||
|
g.call(xAxis).append("text")
|
||||||
|
.attr("class", "caption")
|
||||||
|
.attr("y", 21)
|
||||||
|
.text('Unemployment Rate 2011 (%)');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
|
@ -0,0 +1,165 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legend {
|
||||||
|
padding: 0px 0px;
|
||||||
|
font: 10px sans-serif;
|
||||||
|
background: white;
|
||||||
|
background: rgba(255,255,255,0.8);
|
||||||
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key path {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="map" style="width: 100%; height: 100%"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
queue()
|
||||||
|
.defer(d3.json, 'data3.json')
|
||||||
|
.defer(d3.json, 'us_counties_20m_topo.json')
|
||||||
|
.await(makeMap)
|
||||||
|
|
||||||
|
function onEachFeature(feature, layer) {
|
||||||
|
// does this feature have a property named popupContent?
|
||||||
|
if (feature.properties && feature.properties.popupContent) {
|
||||||
|
layer.bindPopup(feature.properties.popupContent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function makeMap(error, data_1,tjson_1) {
|
||||||
|
|
||||||
|
|
||||||
|
topo_1 = topojson.feature(tjson_1, tjson_1.objects.us_counties_20m);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function matchKey(datapoint, key_variable){
|
||||||
|
if (typeof key_variable[0][datapoint] === 'undefined') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return parseFloat(key_variable[0][datapoint]);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var color = d3.scale.threshold()
|
||||||
|
.domain([20000.0, 40000.0, 50000.0, 50000.0, 60000.0])
|
||||||
|
.range(['#F1EEF6', '#D4B9DA', '#C994C7', '#DF65B0', '#E7298A', '#CE1256']);
|
||||||
|
|
||||||
|
|
||||||
|
var map = L.map('map').setView([40, -99], 4);
|
||||||
|
|
||||||
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||||
|
maxZoom: 18,
|
||||||
|
minZoom: 1,
|
||||||
|
attribution: 'Map data (c) <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
|
||||||
|
}).addTo(map);
|
||||||
|
|
||||||
|
|
||||||
|
function style_1(feature) {
|
||||||
|
return {
|
||||||
|
fillColor: color(matchKey(feature.id, data_1)),
|
||||||
|
weight: 1,
|
||||||
|
opacity: 0.3,
|
||||||
|
color: 'black',
|
||||||
|
fillOpacity: 0.6
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
gJson_layer_1 = L.geoJson(topo_1, {style: style_1,onEachFeature: onEachFeature}).addTo(map)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var legend = L.control({position: 'topright'});
|
||||||
|
|
||||||
|
legend.onAdd = function (map) {var div = L.DomUtil.create('div', 'legend'); return div};
|
||||||
|
|
||||||
|
legend.addTo(map);
|
||||||
|
|
||||||
|
var x = d3.scale.linear()
|
||||||
|
.domain([0, 66000])
|
||||||
|
.range([0, 400]);
|
||||||
|
|
||||||
|
var xAxis = d3.svg.axis()
|
||||||
|
.scale(x)
|
||||||
|
.orient("top")
|
||||||
|
.tickSize(1)
|
||||||
|
.tickValues([20000.0, 40000.0, 50000.0, 50000.0, 60000.0]);
|
||||||
|
|
||||||
|
var svg = d3.select(".legend.leaflet-control").append("svg")
|
||||||
|
.attr("id", 'legend')
|
||||||
|
.attr("width", 450)
|
||||||
|
.attr("height", 40);
|
||||||
|
|
||||||
|
var g = svg.append("g")
|
||||||
|
.attr("class", "key")
|
||||||
|
.attr("transform", "translate(25,16)");
|
||||||
|
|
||||||
|
g.selectAll("rect")
|
||||||
|
.data(color.range().map(function(d, i) {
|
||||||
|
return {
|
||||||
|
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
|
||||||
|
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
|
||||||
|
z: d
|
||||||
|
};
|
||||||
|
}))
|
||||||
|
.enter().append("rect")
|
||||||
|
.attr("height", 10)
|
||||||
|
.attr("x", function(d) { return d.x0; })
|
||||||
|
.attr("width", function(d) { return d.x1 - d.x0; })
|
||||||
|
.style("fill", function(d) { return d.z; });
|
||||||
|
|
||||||
|
g.call(xAxis).append("text")
|
||||||
|
.attr("class", "caption")
|
||||||
|
.attr("y", 21)
|
||||||
|
.text('Median Household Income 2011 ($)');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
|
@ -0,0 +1,163 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legend {
|
||||||
|
padding: 0px 0px;
|
||||||
|
font: 10px sans-serif;
|
||||||
|
background: white;
|
||||||
|
background: rgba(255,255,255,0.8);
|
||||||
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key path {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="map" style="width: 100%; height: 100%"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
queue()
|
||||||
|
.defer(d3.json, 'data.json')
|
||||||
|
.defer(d3.json, 'us-states.json')
|
||||||
|
.await(makeMap)
|
||||||
|
|
||||||
|
function onEachFeature(feature, layer) {
|
||||||
|
// does this feature have a property named popupContent?
|
||||||
|
if (feature.properties && feature.properties.popupContent) {
|
||||||
|
layer.bindPopup(feature.properties.popupContent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function makeMap(error, data_1,gjson_1) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function matchKey(datapoint, key_variable){
|
||||||
|
if (typeof key_variable[0][datapoint] === 'undefined') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return parseFloat(key_variable[0][datapoint]);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var color = d3.scale.threshold()
|
||||||
|
.domain([3.0, 7.0, 8.0, 9.0, 9.0])
|
||||||
|
.range(['#FFFFCC', '#D9F0A3', '#ADDD8E', '#78C679', '#41AB5D', '#238443']);
|
||||||
|
|
||||||
|
|
||||||
|
var map = L.map('map').setView([48, -102], 3);
|
||||||
|
|
||||||
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||||
|
maxZoom: 18,
|
||||||
|
minZoom: 1,
|
||||||
|
attribution: 'Map data (c) <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
|
||||||
|
}).addTo(map);
|
||||||
|
|
||||||
|
|
||||||
|
function style_1(feature) {
|
||||||
|
return {
|
||||||
|
fillColor: color(matchKey(feature.id, data_1)),
|
||||||
|
weight: 1,
|
||||||
|
opacity: 0.2,
|
||||||
|
color: 'black',
|
||||||
|
fillOpacity: 0.7
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
gJson_layer_1 = L.geoJson(gjson_1, {style: style_1,onEachFeature: onEachFeature}).addTo(map)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var legend = L.control({position: 'topright'});
|
||||||
|
|
||||||
|
legend.onAdd = function (map) {var div = L.DomUtil.create('div', 'legend'); return div};
|
||||||
|
|
||||||
|
legend.addTo(map);
|
||||||
|
|
||||||
|
var x = d3.scale.linear()
|
||||||
|
.domain([0, 9])
|
||||||
|
.range([0, 400]);
|
||||||
|
|
||||||
|
var xAxis = d3.svg.axis()
|
||||||
|
.scale(x)
|
||||||
|
.orient("top")
|
||||||
|
.tickSize(1)
|
||||||
|
.tickValues([3.0, 7.0, 8.0, 9.0, 9.0]);
|
||||||
|
|
||||||
|
var svg = d3.select(".legend.leaflet-control").append("svg")
|
||||||
|
.attr("id", 'legend')
|
||||||
|
.attr("width", 450)
|
||||||
|
.attr("height", 40);
|
||||||
|
|
||||||
|
var g = svg.append("g")
|
||||||
|
.attr("class", "key")
|
||||||
|
.attr("transform", "translate(25,16)");
|
||||||
|
|
||||||
|
g.selectAll("rect")
|
||||||
|
.data(color.range().map(function(d, i) {
|
||||||
|
return {
|
||||||
|
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
|
||||||
|
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
|
||||||
|
z: d
|
||||||
|
};
|
||||||
|
}))
|
||||||
|
.enter().append("rect")
|
||||||
|
.attr("height", 10)
|
||||||
|
.attr("x", function(d) { return d.x0; })
|
||||||
|
.attr("width", function(d) { return d.x1 - d.x0; })
|
||||||
|
.style("fill", function(d) { return d.z; });
|
||||||
|
|
||||||
|
g.call(xAxis).append("text")
|
||||||
|
.attr("class", "caption")
|
||||||
|
.attr("y", 21)
|
||||||
|
.text('Unemployment Rate (%)');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
|
@ -0,0 +1,163 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legend {
|
||||||
|
padding: 0px 0px;
|
||||||
|
font: 10px sans-serif;
|
||||||
|
background: white;
|
||||||
|
background: rgba(255,255,255,0.8);
|
||||||
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key path {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="map" style="width: 100%; height: 100%"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
queue()
|
||||||
|
.defer(d3.json, 'data.json')
|
||||||
|
.defer(d3.json, 'us-states.json')
|
||||||
|
.await(makeMap)
|
||||||
|
|
||||||
|
function onEachFeature(feature, layer) {
|
||||||
|
// does this feature have a property named popupContent?
|
||||||
|
if (feature.properties && feature.properties.popupContent) {
|
||||||
|
layer.bindPopup(feature.properties.popupContent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function makeMap(error, data_1,gjson_1) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function matchKey(datapoint, key_variable){
|
||||||
|
if (typeof key_variable[0][datapoint] === 'undefined') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return parseFloat(key_variable[0][datapoint]);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var color = d3.scale.threshold()
|
||||||
|
.domain([5, 6, 7, 8, 9, 10])
|
||||||
|
.range(['#EDF8FB', '#BFD3E6', '#9EBCDA', '#8C96C6', '#8C6BB1', '#88419D', '#6E016B']);
|
||||||
|
|
||||||
|
|
||||||
|
var map = L.map('map').setView([48, -102], 3);
|
||||||
|
|
||||||
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||||
|
maxZoom: 18,
|
||||||
|
minZoom: 1,
|
||||||
|
attribution: 'Map data (c) <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
|
||||||
|
}).addTo(map);
|
||||||
|
|
||||||
|
|
||||||
|
function style_1(feature) {
|
||||||
|
return {
|
||||||
|
fillColor: color(matchKey(feature.id, data_1)),
|
||||||
|
weight: 1,
|
||||||
|
opacity: 0.5,
|
||||||
|
color: 'black',
|
||||||
|
fillOpacity: 0.7
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
gJson_layer_1 = L.geoJson(gjson_1, {style: style_1,onEachFeature: onEachFeature}).addTo(map)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var legend = L.control({position: 'topright'});
|
||||||
|
|
||||||
|
legend.onAdd = function (map) {var div = L.DomUtil.create('div', 'legend'); return div};
|
||||||
|
|
||||||
|
legend.addTo(map);
|
||||||
|
|
||||||
|
var x = d3.scale.linear()
|
||||||
|
.domain([0, 11])
|
||||||
|
.range([0, 400]);
|
||||||
|
|
||||||
|
var xAxis = d3.svg.axis()
|
||||||
|
.scale(x)
|
||||||
|
.orient("top")
|
||||||
|
.tickSize(1)
|
||||||
|
.tickValues([5, 6, 7, 8, 9, 10]);
|
||||||
|
|
||||||
|
var svg = d3.select(".legend.leaflet-control").append("svg")
|
||||||
|
.attr("id", 'legend')
|
||||||
|
.attr("width", 450)
|
||||||
|
.attr("height", 40);
|
||||||
|
|
||||||
|
var g = svg.append("g")
|
||||||
|
.attr("class", "key")
|
||||||
|
.attr("transform", "translate(25,16)");
|
||||||
|
|
||||||
|
g.selectAll("rect")
|
||||||
|
.data(color.range().map(function(d, i) {
|
||||||
|
return {
|
||||||
|
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
|
||||||
|
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
|
||||||
|
z: d
|
||||||
|
};
|
||||||
|
}))
|
||||||
|
.enter().append("rect")
|
||||||
|
.attr("height", 10)
|
||||||
|
.attr("x", function(d) { return d.x0; })
|
||||||
|
.attr("width", function(d) { return d.x1 - d.x0; })
|
||||||
|
.style("fill", function(d) { return d.z; });
|
||||||
|
|
||||||
|
g.call(xAxis).append("text")
|
||||||
|
.attr("class", "caption")
|
||||||
|
.attr("y", 21)
|
||||||
|
.text('Unemployment Rate (%)');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
|
@ -0,0 +1,74 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import cartopy\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"import cartopy.feature as cfeature\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"## Natural Earth 2 cache test\n",
|
||||||
|
"## Live 8.5 * darkblue-b\n",
|
||||||
|
"##"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ax = plt.axes(projection=ccrs.PlateCarree() ) \n",
|
||||||
|
"\n",
|
||||||
|
"ax.add_feature(cfeature.LAND)\n",
|
||||||
|
"ax.add_feature(cfeature.OCEAN)\n",
|
||||||
|
"ax.add_feature(cfeature.COASTLINE)\n",
|
||||||
|
"ax.add_feature(cfeature.BORDERS, linestyle=':')\n",
|
||||||
|
"ax.add_feature(cfeature.LAKES, alpha=0.5)\n",
|
||||||
|
"ax.add_feature(cfeature.RIVERS)\n",
|
||||||
|
"\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,127 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import cartopy.crs as ccrs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Regridding vectors with quiver\n",
|
||||||
|
"------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
"This example demonstrates the regridding functionality in quiver (there exists\n",
|
||||||
|
"equivalent functionality in :meth:`cartopy.mpl.geoaxes.GeoAxes.barbs`).\n",
|
||||||
|
"\n",
|
||||||
|
"Regridding can be an effective way of visualising a vector field, particularly\n",
|
||||||
|
"if the data is dense or warped.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def sample_data(shape=(20, 30)):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Returns ``(x, y, u, v, crs)`` of some vector data\n",
|
||||||
|
" computed mathematically. The returned CRS will be a North Polar\n",
|
||||||
|
" Stereographic projection, meaning that the vectors will be unevenly\n",
|
||||||
|
" spaced in a PlateCarree projection.\n",
|
||||||
|
"\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" crs = ccrs.NorthPolarStereo()\n",
|
||||||
|
" scale = 1e7\n",
|
||||||
|
" x = np.linspace(-scale, scale, shape[1])\n",
|
||||||
|
" y = np.linspace(-scale, scale, shape[0])\n",
|
||||||
|
"\n",
|
||||||
|
" x2d, y2d = np.meshgrid(x, y)\n",
|
||||||
|
" u = 10 * np.cos(2 * x2d / scale + 3 * y2d / scale)\n",
|
||||||
|
" v = 20 * np.cos(6 * x2d / scale)\n",
|
||||||
|
"\n",
|
||||||
|
" return x, y, u, v, crs\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def main():\n",
|
||||||
|
" plt.figure(figsize=(8, 10))\n",
|
||||||
|
"\n",
|
||||||
|
" x, y, u, v, vector_crs = sample_data(shape=(50, 50))\n",
|
||||||
|
" ax1 = plt.subplot(2, 1, 1, projection=ccrs.PlateCarree())\n",
|
||||||
|
" ax1.coastlines()\n",
|
||||||
|
" ax1.set_extent([-45, 55, 20, 80], ccrs.PlateCarree())\n",
|
||||||
|
" ax1.quiver(x, y, u, v, transform=vector_crs)\n",
|
||||||
|
"\n",
|
||||||
|
" ax2 = plt.subplot(2, 1, 2, projection=ccrs.PlateCarree())\n",
|
||||||
|
" plt.title('The same vector field regridded')\n",
|
||||||
|
" ax2.coastlines()\n",
|
||||||
|
" ax2.set_extent([-45, 55, 20, 80], ccrs.PlateCarree())\n",
|
||||||
|
" ax2.quiver(x, y, u, v, transform=vector_crs, regrid_shape=20)\n",
|
||||||
|
"\n",
|
||||||
|
" plt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,307 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import cartopy\n",
|
||||||
|
"import cartopy.crs as ccrs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def sample_data(shape=(20, 30)):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Returns ``(x, y, u, v, crs)`` of some vector data\n",
|
||||||
|
" computed mathematically. The returned crs will be a rotated\n",
|
||||||
|
" pole CRS, meaning that the vectors will be unevenly spaced in\n",
|
||||||
|
" regular PlateCarree space.\n",
|
||||||
|
"\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" crs = ccrs.RotatedPole(pole_longitude=177.5, pole_latitude=37.5)\n",
|
||||||
|
"\n",
|
||||||
|
" x = np.linspace(311.9, 391.1, shape[1])\n",
|
||||||
|
" y = np.linspace(-23.6, 24.8, shape[0])\n",
|
||||||
|
"\n",
|
||||||
|
" x2d, y2d = np.meshgrid(x, y)\n",
|
||||||
|
" u = 10 * (2 * np.cos(2 * np.deg2rad(x2d) + 3 * np.deg2rad(y2d + 30)) ** 2)\n",
|
||||||
|
" v = 20 * np.cos(6 * np.deg2rad(x2d))\n",
|
||||||
|
"\n",
|
||||||
|
" return x, y, u, v, crs\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" ax = plt.axes(projection=ccrs.Orthographic(-10, 45))\n",
|
||||||
|
"\n",
|
||||||
|
" ax.add_feature(cartopy.feature.OCEAN, zorder=0)\n",
|
||||||
|
" ax.add_feature(cartopy.feature.LAND, zorder=0, edgecolor='black')\n",
|
||||||
|
"\n",
|
||||||
|
" ax.set_global()\n",
|
||||||
|
" ax.gridlines()\n",
|
||||||
|
"\n",
|
||||||
|
" x, y, u, v, vector_crs = sample_data()\n",
|
||||||
|
" ax.quiver(x, y, u, v, transform=vector_crs)\n",
|
||||||
|
"\n",
|
||||||
|
" plt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"`cartopy.geoaxes.quiver`\n",
|
||||||
|
"\n",
|
||||||
|
" **Cartopy matplotlib.quiver PolyCollection.**\n",
|
||||||
|
"\n",
|
||||||
|
"Plot a 2-D field of arrows on a projected plane.\n",
|
||||||
|
"\n",
|
||||||
|
" Extra Kwargs:\n",
|
||||||
|
"\n",
|
||||||
|
" * transform: :class:`cartopy.crs.Projection` or matplotlib transform\n",
|
||||||
|
" The coordinate system in which the vectors are defined.\n",
|
||||||
|
"\n",
|
||||||
|
" * regrid_shape: int or 2-tuple of ints\n",
|
||||||
|
" If given, specifies that the points where the arrows are\n",
|
||||||
|
" located will be interpolated onto a regular grid in\n",
|
||||||
|
" projection space. If a single integer is given then that\n",
|
||||||
|
" will be used as the minimum grid length dimension, while the\n",
|
||||||
|
" other dimension will be scaled up according to the target\n",
|
||||||
|
" extent's aspect ratio. If a pair of ints are given they\n",
|
||||||
|
" determine the grid length in the x and y directions\n",
|
||||||
|
" respectively.\n",
|
||||||
|
"\n",
|
||||||
|
" * target_extent: 4-tuple\n",
|
||||||
|
" If given, specifies the extent in the target CRS that the\n",
|
||||||
|
" regular grid defined by *regrid_shape* will have. Defaults\n",
|
||||||
|
" to the current extent of the map projection.\n",
|
||||||
|
"\n",
|
||||||
|
" See :func:`matplotlib.pyplot.quiver` for details on arguments\n",
|
||||||
|
" and other keyword arguments.\n",
|
||||||
|
"\n",
|
||||||
|
" .. note::\n",
|
||||||
|
"\n",
|
||||||
|
" The vector components must be defined as grid eastward and\n",
|
||||||
|
" grid northward.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"`matplotlib.quiver`\n",
|
||||||
|
"\n",
|
||||||
|
" **Specialized PolyCollection for arrows.**\n",
|
||||||
|
"\n",
|
||||||
|
"Plot a 2-D field of barbs.\n",
|
||||||
|
"\n",
|
||||||
|
"Call signatures::\n",
|
||||||
|
"\n",
|
||||||
|
" barb(U, V, **kw)\n",
|
||||||
|
" barb(U, V, C, **kw)\n",
|
||||||
|
" barb(X, Y, U, V, **kw)\n",
|
||||||
|
" barb(X, Y, U, V, C, **kw)\n",
|
||||||
|
"\n",
|
||||||
|
"*Arguments:*\n",
|
||||||
|
"\n",
|
||||||
|
" *X*, *Y*:\n",
|
||||||
|
" The x and y coordinates of the barb locations\n",
|
||||||
|
" (default is head of barb; see *pivot* kwarg)\n",
|
||||||
|
"\n",
|
||||||
|
" *U*, *V*:\n",
|
||||||
|
" Give the x and y components of the barb shaft\n",
|
||||||
|
"\n",
|
||||||
|
" *C*:\n",
|
||||||
|
" An optional array used to map colors to the barbs\n",
|
||||||
|
"\n",
|
||||||
|
"All arguments may be 1-D or 2-D arrays or sequences. If *X* and *Y*\n",
|
||||||
|
"are absent, they will be generated as a uniform grid. If *U* and *V*\n",
|
||||||
|
"are 2-D arrays but *X* and *Y* are 1-D, and if ``len(X)`` and ``len(Y)``\n",
|
||||||
|
"match the column and row dimensions of *U*, then *X* and *Y* will be\n",
|
||||||
|
"expanded with :func:`numpy.meshgrid`.\n",
|
||||||
|
"\n",
|
||||||
|
" *U*, *V*, *C* may be masked arrays, but masked *X*, *Y* are not\n",
|
||||||
|
" supported at present.\n",
|
||||||
|
"\n",
|
||||||
|
"*Keyword arguments:*\n",
|
||||||
|
"\n",
|
||||||
|
" *length*:\n",
|
||||||
|
" Length of the barb in points; the other parts of the barb\n",
|
||||||
|
" are scaled against this.\n",
|
||||||
|
" Default is 9\n",
|
||||||
|
"\n",
|
||||||
|
" *pivot*: [ 'tip' | 'middle' ]\n",
|
||||||
|
" The part of the arrow that is at the grid point; the arrow rotates\n",
|
||||||
|
" about this point, hence the name *pivot*. Default is 'tip'\n",
|
||||||
|
"\n",
|
||||||
|
" *barbcolor*: [ color | color sequence ]\n",
|
||||||
|
" Specifies the color all parts of the barb except any flags. This\n",
|
||||||
|
" parameter is analagous to the *edgecolor* parameter for polygons,\n",
|
||||||
|
" which can be used instead. However this parameter will override\n",
|
||||||
|
" facecolor.\n",
|
||||||
|
"\n",
|
||||||
|
" *flagcolor*: [ color | color sequence ]\n",
|
||||||
|
" Specifies the color of any flags on the barb. This parameter is\n",
|
||||||
|
" analagous to the *facecolor* parameter for polygons, which can be\n",
|
||||||
|
" used instead. However this parameter will override facecolor. If\n",
|
||||||
|
" this is not set (and *C* has not either) then *flagcolor* will be\n",
|
||||||
|
" set to match *barbcolor* so that the barb has a uniform color. If\n",
|
||||||
|
" *C* has been set, *flagcolor* has no effect.\n",
|
||||||
|
"\n",
|
||||||
|
" *sizes*:\n",
|
||||||
|
" A dictionary of coefficients specifying the ratio of a given\n",
|
||||||
|
" feature to the length of the barb. Only those values one wishes to\n",
|
||||||
|
" override need to be included. These features include:\n",
|
||||||
|
"\n",
|
||||||
|
" - 'spacing' - space between features (flags, full/half barbs)\n",
|
||||||
|
"\n",
|
||||||
|
" - 'height' - height (distance from shaft to top) of a flag or\n",
|
||||||
|
" full barb\n",
|
||||||
|
"\n",
|
||||||
|
" - 'width' - width of a flag, twice the width of a full barb\n",
|
||||||
|
"\n",
|
||||||
|
" - 'emptybarb' - radius of the circle used for low magnitudes\n",
|
||||||
|
"\n",
|
||||||
|
" *fill_empty*:\n",
|
||||||
|
" A flag on whether the empty barbs (circles) that are drawn should\n",
|
||||||
|
" be filled with the flag color. If they are not filled, they will\n",
|
||||||
|
" be drawn such that no color is applied to the center. Default is\n",
|
||||||
|
" False\n",
|
||||||
|
"\n",
|
||||||
|
" *rounding*:\n",
|
||||||
|
" A flag to indicate whether the vector magnitude should be rounded\n",
|
||||||
|
" when allocating barb components. If True, the magnitude is\n",
|
||||||
|
" rounded to the nearest multiple of the half-barb increment. If\n",
|
||||||
|
" False, the magnitude is simply truncated to the next lowest\n",
|
||||||
|
" multiple. Default is True\n",
|
||||||
|
"\n",
|
||||||
|
" *barb_increments*:\n",
|
||||||
|
" A dictionary of increments specifying values to associate with\n",
|
||||||
|
" different parts of the barb. Only those values one wishes to\n",
|
||||||
|
" override need to be included.\n",
|
||||||
|
"\n",
|
||||||
|
" - 'half' - half barbs (Default is 5)\n",
|
||||||
|
"\n",
|
||||||
|
" - 'full' - full barbs (Default is 10)\n",
|
||||||
|
"\n",
|
||||||
|
" - 'flag' - flags (default is 50)\n",
|
||||||
|
"\n",
|
||||||
|
" *flip_barb*:\n",
|
||||||
|
" Either a single boolean flag or an array of booleans. Single\n",
|
||||||
|
" boolean indicates whether the lines and flags should point\n",
|
||||||
|
" opposite to normal for all barbs. An array (which should be the\n",
|
||||||
|
" same size as the other data arrays) indicates whether to flip for\n",
|
||||||
|
" each individual barb. Normal behavior is for the barbs and lines\n",
|
||||||
|
" to point right (comes from wind barbs having these features point\n",
|
||||||
|
" towards low pressure in the Northern Hemisphere.) Default is\n",
|
||||||
|
" False\n",
|
||||||
|
"\n",
|
||||||
|
"Barbs are traditionally used in meteorology as a way to plot the speed\n",
|
||||||
|
"and direction of wind observations, but can technically be used to\n",
|
||||||
|
"plot any two dimensional vector quantity. As opposed to arrows, which\n",
|
||||||
|
"give vector magnitude by the length of the arrow, the barbs give more\n",
|
||||||
|
"quantitative information about the vector magnitude by putting slanted\n",
|
||||||
|
"lines or a triangle for various increments in magnitude, as show\n",
|
||||||
|
"schematically below::\n",
|
||||||
|
"\n",
|
||||||
|
" : /\\ \\\\\n",
|
||||||
|
" : / \\ \\\\\n",
|
||||||
|
" : / \\ \\ \\\\\n",
|
||||||
|
" : / \\ \\ \\\\\n",
|
||||||
|
" : ------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
".. note the double \\\\ at the end of each line to make the figure\n",
|
||||||
|
".. render correctly\n",
|
||||||
|
"\n",
|
||||||
|
"The largest increment is given by a triangle (or \"flag\"). After those\n",
|
||||||
|
"come full lines (barbs). The smallest increment is a half line. There\n",
|
||||||
|
"is only, of course, ever at most 1 half line. If the magnitude is\n",
|
||||||
|
"small and only needs a single half-line and no full lines or\n",
|
||||||
|
"triangles, the half-line is offset from the end of the barb so that it\n",
|
||||||
|
"can be easily distinguished from barbs with a single full line. The\n",
|
||||||
|
"magnitude for the barb shown above would nominally be 65, using the\n",
|
||||||
|
"standard increments of 50, 10, and 5.\n",
|
||||||
|
"\n",
|
||||||
|
"linewidths and edgecolors can be used to customize the barb.\n",
|
||||||
|
"Additional :class:`~matplotlib.collections.PolyCollection` keyword\n",
|
||||||
|
"arguments:\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print 'matplotlib.__version__ => ' + matplotlib.__version__\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,197 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"from osgeo import gdal\n",
|
||||||
|
"from osgeo import gdal_array\n",
|
||||||
|
"\n",
|
||||||
|
"import rasterio\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"\n",
|
||||||
|
"## DEM plot via cartopy, rasterio, pyplot imshow\n",
|
||||||
|
"## Live 8.5 * darkblue-b\n",
|
||||||
|
"##"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"=="
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Rasterio\n",
|
||||||
|
"## Clean and fast\n",
|
||||||
|
"## geospatial raster I/O for Python programmers who use Numpy\n",
|
||||||
|
"\n",
|
||||||
|
"with rasterio.open('sample_data/SanMateo_CA.tif') as src:\n",
|
||||||
|
" data = src.read()\n",
|
||||||
|
" data = np.transpose( data, [1,2,0])\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"## show selected data attributes\n",
|
||||||
|
"## -------------------------------------\n",
|
||||||
|
"# type(data) numpy.ma.core.MaskedArray\n",
|
||||||
|
"# data.ndim 3\n",
|
||||||
|
"# data.shape (1080, 864, 1)\n",
|
||||||
|
"# data.dtype dtype('float32')\n",
|
||||||
|
"\n",
|
||||||
|
"## NOTE: when using rasterio and matplotlib only,\n",
|
||||||
|
"## the column order for the trivial case of lat/lon/measure\n",
|
||||||
|
"## is NOT handled automatically.. \n",
|
||||||
|
"## column reordering is REQUIRED np.transpose()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Rasterio supplies a simple dictionary of important GeoTIFF metadata\n",
|
||||||
|
"src.meta"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## numpy ndarray indexing example\n",
|
||||||
|
"## show the measure values for a 2x2 area\n",
|
||||||
|
"## no-data options are available using a masked array\n",
|
||||||
|
"## http://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html#what-is-a-masked-array\n",
|
||||||
|
"\n",
|
||||||
|
"data[0:2,0:2,0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## pyplot Image-Show \n",
|
||||||
|
"## http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow\n",
|
||||||
|
"##\n",
|
||||||
|
"## Example of using matplotlib to directly display a GeoTIFF\n",
|
||||||
|
"##\n",
|
||||||
|
"## Cartopy supplies a library of mapping transformations\n",
|
||||||
|
"## use an idiom to calculate the correct display bounds\n",
|
||||||
|
"##\n",
|
||||||
|
"## data[:,:,0] refers to a numpy ndarray: all-x, all-y, 0th measure\n",
|
||||||
|
"##\n",
|
||||||
|
"\n",
|
||||||
|
"xmin = src.transform[0]\n",
|
||||||
|
"xmax = src.transform[0] + src.transform[1]*src.width\n",
|
||||||
|
"ymin = src.transform[3] + src.transform[5]*src.height\n",
|
||||||
|
"ymax = src.transform[3]\n",
|
||||||
|
"\n",
|
||||||
|
"## Mercator etc..\n",
|
||||||
|
"crs = ccrs.PlateCarree()\n",
|
||||||
|
"\n",
|
||||||
|
"ax = plt.axes(projection=crs)\n",
|
||||||
|
"plt.imshow( data[:,:,0], origin='upper', \n",
|
||||||
|
" extent=[xmin, xmax, ymin, ymax], \n",
|
||||||
|
" cmap=plt.get_cmap('gist_earth'),\n",
|
||||||
|
" transform=crs, interpolation='nearest')\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"=="
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Show the same content, but with a colorbar legend for the data\n",
|
||||||
|
"##\n",
|
||||||
|
"plt.figure(figsize=(15, 10))\n",
|
||||||
|
"ax = plt.subplot(111, projection=ccrs.PlateCarree())\n",
|
||||||
|
"\n",
|
||||||
|
"#elev, crs, extent = srtm_composite(12, 47, 1, 1)\n",
|
||||||
|
"plt.imshow( data[:,:,0], transform=ccrs.PlateCarree(),\n",
|
||||||
|
" cmap='gist_earth')\n",
|
||||||
|
"cb = plt.colorbar(orientation='vertical')\n",
|
||||||
|
"cb.set_label('Altitude')\n",
|
||||||
|
"plt.title(\"DEM\")\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%whos"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,183 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.coord_systems\n",
|
||||||
|
"import iris.cube\n",
|
||||||
|
"\n",
|
||||||
|
"import iris.plot as iplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cube_path = 'sample_data/rotated_pole.nc'\n",
|
||||||
|
"cube = iris.load_cube(cube_path)\n",
|
||||||
|
"\n",
|
||||||
|
"cube.coord('grid_latitude').guess_bounds()\n",
|
||||||
|
"cube.coord('grid_longitude').guess_bounds()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print cube.summary()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Basic Plots\n",
|
||||||
|
"iplt.contourf(cube)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"iplt.pcolor(cube)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## subplots\n",
|
||||||
|
"\n",
|
||||||
|
"plt.subplot(221)\n",
|
||||||
|
"plt.title('Default')\n",
|
||||||
|
"iplt.contourf(cube)\n",
|
||||||
|
"plt.gca().coastlines()\n",
|
||||||
|
"\n",
|
||||||
|
"# Second sub-plot\n",
|
||||||
|
"plt.subplot(222, projection=ccrs.Mollweide(central_longitude=120))\n",
|
||||||
|
"plt.title('Molleweide')\n",
|
||||||
|
"iplt.contourf(cube)\n",
|
||||||
|
"plt.gca().coastlines()\n",
|
||||||
|
"\n",
|
||||||
|
"# Third sub-plot (the projection part is redundant, but a useful\n",
|
||||||
|
"# test none-the-less)\n",
|
||||||
|
"ax = plt.subplot(223, projection=iplt.default_projection(cube))\n",
|
||||||
|
"plt.title('Native')\n",
|
||||||
|
"iplt.contour(cube)\n",
|
||||||
|
"ax.coastlines()\n",
|
||||||
|
"\n",
|
||||||
|
"# Fourth sub-plot\n",
|
||||||
|
"ax = plt.subplot(2, 2, 4, projection=ccrs.PlateCarree())\n",
|
||||||
|
"plt.title('PlateCarree')\n",
|
||||||
|
"iplt.contourf(cube)\n",
|
||||||
|
"ax.coastlines()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.axes(projection=ccrs.PlateCarree(central_longitude=40))\n",
|
||||||
|
"plt.gca().coastlines()\n",
|
||||||
|
"iplt.pcolormesh(cube)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"iplt.outline(cube)\n",
|
||||||
|
"plt.gca().coastlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"iplt.points(cube)\n",
|
||||||
|
"plt.gca().coastlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,166 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Deriving Exner Pressure and Air Temperature\n",
|
||||||
|
"===========================================\n",
|
||||||
|
"\n",
|
||||||
|
"This example shows some processing of cubes in order to derive further related\n",
|
||||||
|
"cubes; in this case the derived cubes are Exner pressure and air temperature\n",
|
||||||
|
"which are calculated by combining air pressure, air potential temperature and\n",
|
||||||
|
"specific humidity. Finally, the two new cubes are presented side-by-side in a\n",
|
||||||
|
"plot.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.ticker\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.coords as coords\n",
|
||||||
|
"import iris.iterate\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def limit_colorbar_ticks(contour_object):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Takes a contour object which has an associated colorbar and limits the\n",
|
||||||
|
" number of ticks on the colorbar to 4.\n",
|
||||||
|
"\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" # Under Matplotlib v1.2.x the colorbar attribute of a contour object is\n",
|
||||||
|
" # a tuple containing the colorbar and an axes object, whereas under\n",
|
||||||
|
" # Matplotlib v1.3.x it is simply the colorbar.\n",
|
||||||
|
" try:\n",
|
||||||
|
" colorbar = contour_object.colorbar[0]\n",
|
||||||
|
" except (AttributeError, TypeError):\n",
|
||||||
|
" colorbar = contour_object.colorbar\n",
|
||||||
|
"\n",
|
||||||
|
" colorbar.locator = matplotlib.ticker.MaxNLocator(4)\n",
|
||||||
|
" colorbar.update_ticks()\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" #fname = iris.sample_data_path('colpex.pp')\n",
|
||||||
|
" fname = 'sample_data/colpex.pp'\n",
|
||||||
|
"\n",
|
||||||
|
" # The list of phenomena of interest\n",
|
||||||
|
" phenomena = ['air_potential_temperature', 'air_pressure']\n",
|
||||||
|
"\n",
|
||||||
|
" # Define the constraint on standard name and model level\n",
|
||||||
|
" constraints = [iris.Constraint(phenom, model_level_number=1) for\n",
|
||||||
|
" phenom in phenomena]\n",
|
||||||
|
"\n",
|
||||||
|
" air_potential_temperature, air_pressure = iris.load_cubes(fname,\n",
|
||||||
|
" constraints)\n",
|
||||||
|
"\n",
|
||||||
|
" # Define a coordinate which represents 1000 hPa\n",
|
||||||
|
" p0 = coords.AuxCoord(1000, long_name='P0', units='hPa')\n",
|
||||||
|
" # Convert reference pressure 'p0' into the same units as 'air_pressure'\n",
|
||||||
|
" p0.convert_units(air_pressure.units)\n",
|
||||||
|
"\n",
|
||||||
|
" # Calculate Exner pressure\n",
|
||||||
|
" exner_pressure = (air_pressure / p0) ** (287.05 / 1005.0)\n",
|
||||||
|
" # Set the name (the unit is scalar)\n",
|
||||||
|
" exner_pressure.rename('exner_pressure')\n",
|
||||||
|
"\n",
|
||||||
|
" # Calculate air_temp\n",
|
||||||
|
" air_temperature = exner_pressure * air_potential_temperature\n",
|
||||||
|
" # Set the name (the unit is K)\n",
|
||||||
|
" air_temperature.rename('air_temperature')\n",
|
||||||
|
"\n",
|
||||||
|
" # Now create an iterator which will give us lat lon slices of\n",
|
||||||
|
" # exner pressure and air temperature in the form\n",
|
||||||
|
" # (exner_slice, air_temp_slice).\n",
|
||||||
|
" lat_lon_slice_pairs = iris.iterate.izip(exner_pressure,\n",
|
||||||
|
" air_temperature,\n",
|
||||||
|
" coords=['grid_latitude',\n",
|
||||||
|
" 'grid_longitude'])\n",
|
||||||
|
"\n",
|
||||||
|
" # For the purposes of this example, we only want to demonstrate the first\n",
|
||||||
|
" # plot.\n",
|
||||||
|
" lat_lon_slice_pairs = [next(lat_lon_slice_pairs)]\n",
|
||||||
|
"\n",
|
||||||
|
" plt.figure(figsize=(8, 4))\n",
|
||||||
|
" for exner_slice, air_temp_slice in lat_lon_slice_pairs:\n",
|
||||||
|
" plt.subplot(121)\n",
|
||||||
|
" cont = qplt.contourf(exner_slice)\n",
|
||||||
|
"\n",
|
||||||
|
" # The default colorbar has a few too many ticks on it, causing text to\n",
|
||||||
|
" # overlap. Therefore, limit the number of ticks.\n",
|
||||||
|
" limit_colorbar_ticks(cont)\n",
|
||||||
|
"\n",
|
||||||
|
" plt.subplot(122)\n",
|
||||||
|
" cont = qplt.contourf(air_temp_slice)\n",
|
||||||
|
" limit_colorbar_ticks(cont)\n",
|
||||||
|
" iplt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,210 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Seasonal ensemble model plots\n",
|
||||||
|
"=============================\n",
|
||||||
|
"\n",
|
||||||
|
"This example demonstrates the loading of a lagged ensemble dataset from the GloSea4 model, which is then used to\n",
|
||||||
|
"produce two types of plot:\n",
|
||||||
|
"\n",
|
||||||
|
" * The first shows the \"postage stamp\" style image with an array of 14 images, one for each ensemble member with\n",
|
||||||
|
" a shared colorbar. (The missing image in this example represents ensemble member number 6 which was a failed run)\n",
|
||||||
|
"\n",
|
||||||
|
" * The second plot shows the data limited to a region of interest, in this case a region defined for forecasting\n",
|
||||||
|
" ENSO (El Nino-Southern Oscillation), which, for the purposes of this example, has had the ensemble mean subtracted\n",
|
||||||
|
" from each ensemble member to give an anomaly surface temperature. In practice a better approach would be to take the\n",
|
||||||
|
" climatological mean, calibrated to the model, from each ensemble member.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def realization_metadata(cube, field, fname):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" A function which modifies the cube's metadata to add a \"realization\" (ensemble member) coordinate from the filename if one\n",
|
||||||
|
" doesn't already exist in the cube.\n",
|
||||||
|
"\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" # add an ensemble member coordinate if one doesn't already exist\n",
|
||||||
|
" if not cube.coords('realization'):\n",
|
||||||
|
" # the ensemble member is encoded in the filename as *_???.pp where ??? is the ensemble member\n",
|
||||||
|
" realization_number = fname[-6:-3]\n",
|
||||||
|
"\n",
|
||||||
|
" import iris.coords\n",
|
||||||
|
" realization_coord = iris.coords.AuxCoord(np.int32(realization_number), 'realization')\n",
|
||||||
|
" cube.add_aux_coord(realization_coord)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" # extract surface temperature cubes which have an ensemble member coordinate, adding appropriate lagged ensemble metadata\n",
|
||||||
|
" #dpath = iris.sample_data_path('GloSea4', 'ensemble_???.pp')\n",
|
||||||
|
" dpath = 'sample_data/GloSea4/ensemble_???.pp'\n",
|
||||||
|
" surface_temp = iris.load_cube( dpath,\n",
|
||||||
|
" iris.Constraint('surface_temperature', realization=lambda value: True),\n",
|
||||||
|
" callback=realization_metadata,\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" # ----------------------------------------------------------------------------------------------------------------\n",
|
||||||
|
" # Plot #1: Ensemble postage stamps\n",
|
||||||
|
" # ----------------------------------------------------------------------------------------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
" # for the purposes of this example, take the last time element of the cube\n",
|
||||||
|
" last_timestep = surface_temp[:, -1, :, :]\n",
|
||||||
|
"\n",
|
||||||
|
" # Make 50 evenly spaced levels which span the dataset\n",
|
||||||
|
" contour_levels = np.linspace(np.min(last_timestep.data), np.max(last_timestep.data), 50)\n",
|
||||||
|
"\n",
|
||||||
|
" # Create a wider than normal figure to support our many plots\n",
|
||||||
|
" plt.figure(figsize=(12, 6), dpi=100)\n",
|
||||||
|
"\n",
|
||||||
|
" # Also manually adjust the spacings which are used when creating subplots\n",
|
||||||
|
" plt.gcf().subplots_adjust(hspace=0.05, wspace=0.05, top=0.95, bottom=0.05, left=0.075, right=0.925)\n",
|
||||||
|
"\n",
|
||||||
|
" # iterate over all possible latitude longitude slices\n",
|
||||||
|
" for cube in last_timestep.slices(['latitude', 'longitude']):\n",
|
||||||
|
"\n",
|
||||||
|
" # get the ensemble member number from the ensemble coordinate\n",
|
||||||
|
" ens_member = cube.coord('realization').points[0]\n",
|
||||||
|
"\n",
|
||||||
|
" # plot the data in a 4x4 grid, with each plot's position in the grid being determined by ensemble member number\n",
|
||||||
|
" # the special case for the 13th ensemble member is to have the plot at the bottom right\n",
|
||||||
|
" if ens_member == 13:\n",
|
||||||
|
" plt.subplot(4, 4, 16)\n",
|
||||||
|
" else:\n",
|
||||||
|
" plt.subplot(4, 4, ens_member+1)\n",
|
||||||
|
"\n",
|
||||||
|
" cf = iplt.contourf(cube, contour_levels)\n",
|
||||||
|
"\n",
|
||||||
|
" # add coastlines\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
"\n",
|
||||||
|
" # make an axes to put the shared colorbar in\n",
|
||||||
|
" colorbar_axes = plt.gcf().add_axes([0.35, 0.1, 0.3, 0.05])\n",
|
||||||
|
" colorbar = plt.colorbar(cf, colorbar_axes, orientation='horizontal')\n",
|
||||||
|
" colorbar.set_label('%s' % last_timestep.units)\n",
|
||||||
|
"\n",
|
||||||
|
" # limit the colorbar to 8 tick marks\n",
|
||||||
|
" import matplotlib.ticker\n",
|
||||||
|
" colorbar.locator = matplotlib.ticker.MaxNLocator(8)\n",
|
||||||
|
" colorbar.update_ticks()\n",
|
||||||
|
"\n",
|
||||||
|
" # get the time for the entire plot\n",
|
||||||
|
" time_coord = last_timestep.coord('time')\n",
|
||||||
|
" time = time_coord.units.num2date(time_coord.bounds[0, 0])\n",
|
||||||
|
"\n",
|
||||||
|
" # set a global title for the postage stamps with the date formated by \"monthname year\"\n",
|
||||||
|
" plt.suptitle('Surface temperature ensemble forecasts for %s' % time.strftime('%B %Y'))\n",
|
||||||
|
"\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n",
|
||||||
|
" # ----------------------------------------------------------------------------------------------------------------\n",
|
||||||
|
" # Plot #2: ENSO plumes\n",
|
||||||
|
" # ----------------------------------------------------------------------------------------------------------------\n",
|
||||||
|
"\n",
|
||||||
|
" # Nino 3.4 lies between: 170W and 120W, 5N and 5S, so define a constraint which matches this\n",
|
||||||
|
" nino_3_4_constraint = iris.Constraint(longitude=lambda v: -170+360 <= v <= -120+360, latitude=lambda v: -5 <= v <= 5)\n",
|
||||||
|
"\n",
|
||||||
|
" nino_cube = surface_temp.extract(nino_3_4_constraint)\n",
|
||||||
|
"\n",
|
||||||
|
" # Subsetting a circular longitude coordinate always results in a circular coordinate, so set the coordinate to be non-circular\n",
|
||||||
|
" nino_cube.coord('longitude').circular = False\n",
|
||||||
|
"\n",
|
||||||
|
" # Calculate the horizontal mean for the nino region\n",
|
||||||
|
" mean = nino_cube.collapsed(['latitude', 'longitude'], iris.analysis.MEAN)\n",
|
||||||
|
"\n",
|
||||||
|
" # Calculate the ensemble mean of the horizontal mean. To do this, remove the \"forecast_period\" and\n",
|
||||||
|
" # \"forecast_reference_time\" coordinates which span both \"relalization\" and \"time\".\n",
|
||||||
|
" mean.remove_coord(\"forecast_reference_time\")\n",
|
||||||
|
" mean.remove_coord(\"forecast_period\")\n",
|
||||||
|
" ensemble_mean = mean.collapsed('realization', iris.analysis.MEAN)\n",
|
||||||
|
"\n",
|
||||||
|
" # take the ensemble mean from each ensemble member\n",
|
||||||
|
" mean -= ensemble_mean.data\n",
|
||||||
|
"\n",
|
||||||
|
" plt.figure()\n",
|
||||||
|
"\n",
|
||||||
|
" for ensemble_member in mean.slices(['time']):\n",
|
||||||
|
" # draw each ensemble member as a dashed line in black\n",
|
||||||
|
" iplt.plot(ensemble_member, '--k')\n",
|
||||||
|
"\n",
|
||||||
|
" plt.title('Mean temperature anomaly for ENSO 3.4 region')\n",
|
||||||
|
" plt.xlabel('Time')\n",
|
||||||
|
" plt.ylabel('Temperature anomaly / K')\n",
|
||||||
|
"\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,116 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.dates as mdates\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt\n",
|
||||||
|
"import iris.unit"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Hovmoller diagram of monthly surface temperature\n",
|
||||||
|
"================================================\n",
|
||||||
|
"\n",
|
||||||
|
"This example demonstrates the creation of a Hovmoller diagram with fine control over plot ticks and labels.\n",
|
||||||
|
"The data comes from the Met Office OSTIA project and has been pre-processed to calculate the monthly mean sea\n",
|
||||||
|
"surface temperature.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" #fname = iris.sample_data_path('ostia_monthly.nc')\n",
|
||||||
|
" fname = 'sample_data/ostia_monthly.nc'\n",
|
||||||
|
"\n",
|
||||||
|
" # load a single cube of surface temperature between +/- 5 latitude\n",
|
||||||
|
" cube = iris.load_cube(fname, iris.Constraint('surface_temperature', latitude=lambda v: -5 < v < 5))\n",
|
||||||
|
"\n",
|
||||||
|
" # Take the mean over latitude\n",
|
||||||
|
" cube = cube.collapsed('latitude', iris.analysis.MEAN)\n",
|
||||||
|
"\n",
|
||||||
|
" # Now that we have our data in a nice way, lets create the plot\n",
|
||||||
|
" # contour with 20 levels\n",
|
||||||
|
" qplt.contourf(cube, 20)\n",
|
||||||
|
"\n",
|
||||||
|
" # Put a custom label on the y axis\n",
|
||||||
|
" plt.ylabel('Time / years')\n",
|
||||||
|
"\n",
|
||||||
|
" # Stop matplotlib providing clever axes range padding\n",
|
||||||
|
" plt.axis('tight')\n",
|
||||||
|
"\n",
|
||||||
|
" # As we are plotting annual variability, put years as the y ticks\n",
|
||||||
|
" plt.gca().yaxis.set_major_locator(mdates.YearLocator())\n",
|
||||||
|
"\n",
|
||||||
|
" # And format the ticks to just show the year\n",
|
||||||
|
" plt.gca().yaxis.set_major_formatter(mdates.DateFormatter('%Y'))\n",
|
||||||
|
"\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,138 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.analysis.cartography\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Tri-Polar Grid Projected Plotting\n",
|
||||||
|
"=================================\n",
|
||||||
|
"\n",
|
||||||
|
"This example demonstrates cell plots of data on the semi-structured ORCA2 model\n",
|
||||||
|
"grid.\n",
|
||||||
|
"\n",
|
||||||
|
"First, the data is projected into the PlateCarree coordinate reference system.\n",
|
||||||
|
"\n",
|
||||||
|
"Second four pcolormesh plots are created from this projected dataset,\n",
|
||||||
|
"using different projections for the output image.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def main():\n",
|
||||||
|
" # Load data\n",
|
||||||
|
" #filepath = iris.sample_data_path('orca2_votemper.nc')\n",
|
||||||
|
" filepath = 'sample_data/orca2_votemper.nc'\n",
|
||||||
|
" cube = iris.load_cube(filepath)\n",
|
||||||
|
"\n",
|
||||||
|
" # Choose plot projections\n",
|
||||||
|
" projections = {}\n",
|
||||||
|
" projections['Mollweide'] = ccrs.Mollweide()\n",
|
||||||
|
" projections['PlateCarree'] = ccrs.PlateCarree()\n",
|
||||||
|
" projections['NorthPolarStereo'] = ccrs.NorthPolarStereo()\n",
|
||||||
|
" projections['Orthographic'] = ccrs.Orthographic(central_longitude=-90,\n",
|
||||||
|
" central_latitude=45)\n",
|
||||||
|
"\n",
|
||||||
|
" pcarree = projections['PlateCarree']\n",
|
||||||
|
" # Transform cube to target projection\n",
|
||||||
|
" new_cube, extent = iris.analysis.cartography.project(cube, pcarree,\n",
|
||||||
|
" nx=400, ny=200)\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot data in each projection\n",
|
||||||
|
" for name in sorted(projections):\n",
|
||||||
|
" fig = plt.figure()\n",
|
||||||
|
" fig.suptitle('ORCA2 Data Projected to {}'.format(name))\n",
|
||||||
|
" # Set up axes and title\n",
|
||||||
|
" ax = plt.subplot(projection=projections[name])\n",
|
||||||
|
" # Set limits\n",
|
||||||
|
" ax.set_global()\n",
|
||||||
|
" # plot with Iris quickplot pcolormesh\n",
|
||||||
|
" qplt.pcolormesh(new_cube)\n",
|
||||||
|
" # Draw coastlines\n",
|
||||||
|
" ax.coastlines()\n",
|
||||||
|
"\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,165 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt\n",
|
||||||
|
"from iris.util import rolling_window\n",
|
||||||
|
"from iris.analysis import Aggregator"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Calculating a custom statistic\n",
|
||||||
|
"==============================\n",
|
||||||
|
"\n",
|
||||||
|
"This example shows how to define and use a custom\n",
|
||||||
|
":class:`iris.analysis.Aggregator`, that provides a new statistical operator for\n",
|
||||||
|
"use with cube aggregation functions such as :meth:`~iris.cube.Cube.collapsed`,\n",
|
||||||
|
":meth:`~iris.cube.Cube.aggregated_by` or\n",
|
||||||
|
":meth:`~iris.cube.Cube.rolling_window`.\n",
|
||||||
|
"\n",
|
||||||
|
"In this case, we have a 240-year sequence of yearly average surface temperature\n",
|
||||||
|
"over North America, and we want to calculate in how many years these exceed a\n",
|
||||||
|
"certain temperature over a spell of 5 years or more.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"# Define a function to perform the custom statistical operation.\n",
|
||||||
|
"# Note: in order to meet the requirements of iris.analysis.Aggregator, it must\n",
|
||||||
|
"# do the calculation over an arbitrary (given) data axis.\n",
|
||||||
|
"def count_spells(data, threshold, axis, spell_length):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Function to calculate the number of points in a sequence where the value\n",
|
||||||
|
" has exceeded a threshold value for at least a certain number of timepoints.\n",
|
||||||
|
"\n",
|
||||||
|
" Generalised to operate on multiple time sequences arranged on a specific\n",
|
||||||
|
" axis of a multidimensional array.\n",
|
||||||
|
"\n",
|
||||||
|
" Args:\n",
|
||||||
|
"\n",
|
||||||
|
" * data (array):\n",
|
||||||
|
" raw data to be compared with value threshold.\n",
|
||||||
|
"\n",
|
||||||
|
" * threshold (float):\n",
|
||||||
|
" threshold point for 'significant' datapoints.\n",
|
||||||
|
"\n",
|
||||||
|
" * axis (int):\n",
|
||||||
|
" number of the array dimension mapping the time sequences.\n",
|
||||||
|
" (Can also be negative, e.g. '-1' means last dimension)\n",
|
||||||
|
"\n",
|
||||||
|
" * spell_length (int):\n",
|
||||||
|
" number of consecutive times at which value > threshold to \"count\".\n",
|
||||||
|
"\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" if axis < 0:\n",
|
||||||
|
" # just cope with negative axis numbers\n",
|
||||||
|
" axis += data.ndim\n",
|
||||||
|
" # Threshold the data to find the 'significant' points.\n",
|
||||||
|
" data_hits = data > threshold\n",
|
||||||
|
" # Make an array with data values \"windowed\" along the time axis.\n",
|
||||||
|
" hit_windows = rolling_window(data_hits, window=spell_length, axis=axis)\n",
|
||||||
|
" # Find the windows \"full of True-s\" (along the added 'window axis').\n",
|
||||||
|
" full_windows = np.all(hit_windows, axis=axis+1)\n",
|
||||||
|
" # Count points fulfilling the condition (along the time axis).\n",
|
||||||
|
" spell_point_counts = np.sum(full_windows, axis=axis, dtype=int)\n",
|
||||||
|
" return spell_point_counts\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" # Load the whole time-sequence as a single cube.\n",
|
||||||
|
" #file_path = iris.sample_data_path('E1_north_america.nc')\n",
|
||||||
|
" file_path = 'sample_data/E1_north_america.nc'\n",
|
||||||
|
" cube = iris.load_cube(file_path)\n",
|
||||||
|
"\n",
|
||||||
|
" # Make an aggregator from the user function.\n",
|
||||||
|
" SPELL_COUNT = Aggregator('spell_count',\n",
|
||||||
|
" count_spells,\n",
|
||||||
|
" units_func=lambda units: 1)\n",
|
||||||
|
"\n",
|
||||||
|
" # Define the parameters of the test.\n",
|
||||||
|
" threshold_temperature = 280.0\n",
|
||||||
|
" spell_years = 5\n",
|
||||||
|
"\n",
|
||||||
|
" # Calculate the statistic.\n",
|
||||||
|
" warm_periods = cube.collapsed('time', SPELL_COUNT,\n",
|
||||||
|
" threshold=threshold_temperature,\n",
|
||||||
|
" spell_length=spell_years)\n",
|
||||||
|
" warm_periods.rename('Number of 5-year warm spells in 240 years')\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot the results.\n",
|
||||||
|
" qplt.contourf(warm_periods, cmap='RdYlBu_r')\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" iplt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,175 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"import matplotlib.colors as mcols\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.coord_categorisation\n",
|
||||||
|
"import iris.plot as iplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Colouring anomaly data with logarithmic scaling\n",
|
||||||
|
"===============================================\n",
|
||||||
|
"\n",
|
||||||
|
"In this example, we need to plot anomaly data where the values have a\n",
|
||||||
|
"\"logarithmic\" significance -- i.e. we want to give approximately equal ranges\n",
|
||||||
|
"of colour between data values of, say, 1 and 10 as between 10 and 100.\n",
|
||||||
|
"\n",
|
||||||
|
"As the data range also contains zero, that obviously does not suit a simple\n",
|
||||||
|
"logarithmic interpretation. However, values of less than a certain absolute\n",
|
||||||
|
"magnitude may be considered \"not significant\", so we put these into a separate\n",
|
||||||
|
"\"zero band\" which is plotted in white.\n",
|
||||||
|
"\n",
|
||||||
|
"To do this, we create a custom value mapping function (normalization) using\n",
|
||||||
|
"the matplotlib Norm class `matplotlib.colours.SymLogNorm\n",
|
||||||
|
"<http://matplotlib.org/api/colors_api.html#matplotlib.colors.SymLogNorm>`_.\n",
|
||||||
|
"We use this to make a cell-filled pseudocolour plot with a colorbar.\n",
|
||||||
|
"\n",
|
||||||
|
"NOTE: By \"pseudocolour\", we mean that each data point is drawn as a \"cell\"\n",
|
||||||
|
"region on the plot, coloured according to its data value.\n",
|
||||||
|
"This is provided in Iris by the functions :meth:`iris.plot.pcolor` and\n",
|
||||||
|
":meth:`iris.plot.pcolormesh`, which call the underlying matplotlib\n",
|
||||||
|
"functions of the same names (i.e. `matplotlib.pyplot.pcolor\n",
|
||||||
|
"<http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pcolor>`_\n",
|
||||||
|
"and `matplotlib.pyplot.pcolormesh\n",
|
||||||
|
"<http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pcolormesh>`_).\n",
|
||||||
|
"See also: http://en.wikipedia.org/wiki/False_color#Pseudocolor.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def main():\n",
|
||||||
|
" # Load a sample air temperatures sequence.\n",
|
||||||
|
" #file_path = iris.sample_data_path('E1_north_america.nc')\n",
|
||||||
|
" file_path = 'sample_data/E1_north_america.nc'\n",
|
||||||
|
" temperatures = iris.load_cube(file_path)\n",
|
||||||
|
"\n",
|
||||||
|
" # Create a year-number coordinate from the time information.\n",
|
||||||
|
" iris.coord_categorisation.add_year(temperatures, 'time')\n",
|
||||||
|
"\n",
|
||||||
|
" # Create a sample anomaly field for one chosen year, by extracting that\n",
|
||||||
|
" # year and subtracting the time mean.\n",
|
||||||
|
" sample_year = 1989\n",
|
||||||
|
" year_temperature = temperatures.extract(iris.Constraint(year=sample_year))\n",
|
||||||
|
" time_mean = temperatures.collapsed('time', iris.analysis.MEAN)\n",
|
||||||
|
" anomaly = year_temperature - time_mean\n",
|
||||||
|
"\n",
|
||||||
|
" # Construct a plot title string explaining which years are involved.\n",
|
||||||
|
" years = temperatures.coord('year').points\n",
|
||||||
|
" plot_title = 'Temperature anomaly'\n",
|
||||||
|
" plot_title += '\\n{} differences from {}-{} average.'.format(\n",
|
||||||
|
" sample_year, years[0], years[-1])\n",
|
||||||
|
"\n",
|
||||||
|
" # Define scaling levels for the logarithmic colouring.\n",
|
||||||
|
" minimum_log_level = 0.1\n",
|
||||||
|
" maximum_scale_level = 3.0\n",
|
||||||
|
"\n",
|
||||||
|
" # Use a standard colour map which varies blue-white-red.\n",
|
||||||
|
" # For suitable options, see the 'Diverging colormaps' section in:\n",
|
||||||
|
" # http://matplotlib.org/examples/color/colormaps_reference.html\n",
|
||||||
|
" anom_cmap = 'bwr'\n",
|
||||||
|
"\n",
|
||||||
|
" # Create a 'logarithmic' data normalization.\n",
|
||||||
|
" anom_norm = mcols.SymLogNorm(linthresh=minimum_log_level,\n",
|
||||||
|
" linscale=0,\n",
|
||||||
|
" vmin=-maximum_scale_level,\n",
|
||||||
|
" vmax=maximum_scale_level)\n",
|
||||||
|
" # Setting \"linthresh=minimum_log_level\" makes its non-logarithmic\n",
|
||||||
|
" # data range equal to our 'zero band'.\n",
|
||||||
|
" # Setting \"linscale=0\" maps the whole zero band to the middle colour value\n",
|
||||||
|
" # (i.e. 0.5), which is the neutral point of a \"diverging\" style colormap.\n",
|
||||||
|
"\n",
|
||||||
|
" # Create an Axes, specifying the map projection.\n",
|
||||||
|
" plt.axes(projection=ccrs.LambertConformal())\n",
|
||||||
|
"\n",
|
||||||
|
" # Make a pseudocolour plot using this colour scheme.\n",
|
||||||
|
" mesh = iplt.pcolormesh(anomaly, cmap=anom_cmap, norm=anom_norm)\n",
|
||||||
|
"\n",
|
||||||
|
" # Add a colourbar, with extensions to show handling of out-of-range values.\n",
|
||||||
|
" bar = plt.colorbar(mesh, orientation='horizontal', extend='both')\n",
|
||||||
|
"\n",
|
||||||
|
" # Set some suitable fixed \"logarithmic\" colourbar tick positions.\n",
|
||||||
|
" tick_levels = [-3, -1, -0.3, 0.0, 0.3, 1, 3]\n",
|
||||||
|
" bar.set_ticks(tick_levels)\n",
|
||||||
|
"\n",
|
||||||
|
" # Modify the tick labels so that the centre one shows \"+/-<minumum-level>\".\n",
|
||||||
|
" tick_levels[3] = r'$\\pm${:g}'.format(minimum_log_level)\n",
|
||||||
|
" bar.set_ticklabels(tick_levels)\n",
|
||||||
|
"\n",
|
||||||
|
" # Label the colourbar to show the units.\n",
|
||||||
|
" bar.set_label('[{}, log scale]'.format(anomaly.units))\n",
|
||||||
|
"\n",
|
||||||
|
" # Add coastlines and a title.\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" plt.title(plot_title)\n",
|
||||||
|
"\n",
|
||||||
|
" # Display the result.\n",
|
||||||
|
" iplt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.quickplot as qplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Fitting a polynomial\n",
|
||||||
|
"====================\n",
|
||||||
|
"\n",
|
||||||
|
"This example demonstrates computing a polynomial fit to 1D data from an Iris\n",
|
||||||
|
"cube, adding the fit to the cube's metadata, and plotting both the 1D data and\n",
|
||||||
|
"the fit.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" #fname = iris.sample_data_path('A1B_north_america.nc')\n",
|
||||||
|
" fname = 'sample_data/A1B_north_america.nc'\n",
|
||||||
|
" cube = iris.load_cube(fname)\n",
|
||||||
|
"\n",
|
||||||
|
" # Extract a single time series at a latitude and longitude point.\n",
|
||||||
|
" location = next(cube.slices(['time']))\n",
|
||||||
|
"\n",
|
||||||
|
" # Calculate a polynomial fit to the data at this time series.\n",
|
||||||
|
" x_points = location.coord('time').points\n",
|
||||||
|
" y_points = location.data\n",
|
||||||
|
" degree = 2\n",
|
||||||
|
"\n",
|
||||||
|
" p = np.polyfit(x_points, y_points, degree)\n",
|
||||||
|
" y_fitted = np.polyval(p, x_points)\n",
|
||||||
|
"\n",
|
||||||
|
" # Add the polynomial fit values to the time series to take\n",
|
||||||
|
" # full advantage of Iris plotting functionality.\n",
|
||||||
|
" long_name = 'degree_{}_polynomial_fit_of_{}'.format(degree, cube.name())\n",
|
||||||
|
" fit = iris.coords.AuxCoord(y_fitted, long_name=long_name,\n",
|
||||||
|
" units=location.units)\n",
|
||||||
|
" location.add_aux_coord(fit, 0)\n",
|
||||||
|
"\n",
|
||||||
|
" qplt.plot(location.coord('time'), location, label='data')\n",
|
||||||
|
" qplt.plot(location.coord('time'),\n",
|
||||||
|
" location.coord(long_name),\n",
|
||||||
|
" 'g-', label='polynomial fit')\n",
|
||||||
|
" plt.legend(loc='best')\n",
|
||||||
|
" plt.title('Trend of US air temperature over time')\n",
|
||||||
|
"\n",
|
||||||
|
" qplt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,228 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Plotting in different projections\n",
|
||||||
|
"=================================\n",
|
||||||
|
"\n",
|
||||||
|
"This example shows how to overlay data and graphics in different projections,\n",
|
||||||
|
"demonstrating various features of Iris, Cartopy and matplotlib.\n",
|
||||||
|
"\n",
|
||||||
|
"We wish to overlay two datasets, defined on different rotated-pole grids.\n",
|
||||||
|
"To display both together, we make a pseudocoloured plot of the first, overlaid\n",
|
||||||
|
"with contour lines from the second.\n",
|
||||||
|
"We also add some lines and text annotations drawn in various projections.\n",
|
||||||
|
"\n",
|
||||||
|
"We plot these over a specified region, in two different map projections.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"# Define a Cartopy 'ordinary' lat-lon coordinate reference system.\n",
|
||||||
|
"crs_latlon = ccrs.PlateCarree()\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def make_plot(projection_name, projection_crs):\n",
|
||||||
|
"\n",
|
||||||
|
" # Create a matplotlib Figure.\n",
|
||||||
|
" fig = plt.figure()\n",
|
||||||
|
"\n",
|
||||||
|
" # Add a matplotlib Axes, specifying the required display projection.\n",
|
||||||
|
" # NOTE: specifying 'projection' (a \"cartopy.crs.Projection\") makes the\n",
|
||||||
|
" # resulting Axes a \"cartopy.mpl.geoaxes.GeoAxes\", which supports plotting\n",
|
||||||
|
" # in different coordinate systems.\n",
|
||||||
|
" ax = plt.axes(projection=projection_crs)\n",
|
||||||
|
"\n",
|
||||||
|
" # Set display limits to include a set region of latitude * longitude.\n",
|
||||||
|
" # (Note: Cartopy-specific).\n",
|
||||||
|
" ax.set_extent((-80.0, 20.0, 10.0, 80.0), crs=crs_latlon)\n",
|
||||||
|
"\n",
|
||||||
|
" # Add coastlines and meridians/parallels (Cartopy-specific).\n",
|
||||||
|
" ax.coastlines(linewidth=0.75, color='navy')\n",
|
||||||
|
" ax.gridlines(crs=crs_latlon, linestyle='-')\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot the first dataset as a pseudocolour filled plot.\n",
|
||||||
|
" #maindata_filepath = iris.sample_data_path('rotated_pole.nc')\n",
|
||||||
|
" maindata_filepath = 'sample_data/rotated_pole.nc'\n",
|
||||||
|
" main_data = iris.load_cube(maindata_filepath)\n",
|
||||||
|
" # NOTE: iplt.pcolormesh calls \"pyplot.pcolormesh\", passing in a coordinate\n",
|
||||||
|
" # system with the 'transform' keyword: This enables the Axes (a cartopy\n",
|
||||||
|
" # GeoAxes) to reproject the plot into the display projection.\n",
|
||||||
|
" iplt.pcolormesh(main_data, cmap='RdBu_r')\n",
|
||||||
|
"\n",
|
||||||
|
" # Overplot the other dataset (which has a different grid), as contours.\n",
|
||||||
|
" #overlay_filepath = iris.sample_data_path('space_weather.nc')\n",
|
||||||
|
" overlay_filepath = 'sample_data/space_weather.nc'\n",
|
||||||
|
" overlay_data = iris.load_cube(overlay_filepath, 'total electron content')\n",
|
||||||
|
" # NOTE: as above, \"iris.plot.contour\" calls \"pyplot.contour\" with a\n",
|
||||||
|
" # 'transform' keyword, enabling Cartopy reprojection.\n",
|
||||||
|
" iplt.contour(overlay_data, 20,\n",
|
||||||
|
" linewidths=2.0, colors='darkgreen', linestyles='-')\n",
|
||||||
|
"\n",
|
||||||
|
" # Draw a margin line, some way in from the border of the 'main' data...\n",
|
||||||
|
" # First calculate rectangle corners, 7% in from each corner of the data.\n",
|
||||||
|
" x_coord, y_coord = main_data.coord(axis='x'), main_data.coord(axis='y')\n",
|
||||||
|
" x_start, x_end = np.min(x_coord.points), np.max(x_coord.points)\n",
|
||||||
|
" y_start, y_end = np.min(y_coord.points), np.max(y_coord.points)\n",
|
||||||
|
" margin = 0.07\n",
|
||||||
|
" margin_fractions = np.array([margin, 1.0 - margin])\n",
|
||||||
|
" x_lower, x_upper = x_start + (x_end - x_start) * margin_fractions\n",
|
||||||
|
" y_lower, y_upper = y_start + (y_end - y_start) * margin_fractions\n",
|
||||||
|
" box_x_points = x_lower + (x_upper - x_lower) * np.array([0, 1, 1, 0, 0])\n",
|
||||||
|
" box_y_points = y_lower + (y_upper - y_lower) * np.array([0, 0, 1, 1, 0])\n",
|
||||||
|
" # Get the Iris coordinate sytem of the X coordinate (Y should be the same).\n",
|
||||||
|
" cs_data1 = x_coord.coord_system\n",
|
||||||
|
" # Construct an equivalent Cartopy coordinate reference system (\"crs\").\n",
|
||||||
|
" crs_data1 = cs_data1.as_cartopy_crs()\n",
|
||||||
|
" # Draw the rectangle in this crs, with matplotlib \"pyplot.plot\".\n",
|
||||||
|
" # NOTE: the 'transform' keyword specifies a non-display coordinate system\n",
|
||||||
|
" # for the plot points (as used by the \"iris.plot\" functions).\n",
|
||||||
|
" plt.plot(box_x_points, box_y_points, transform=crs_data1,\n",
|
||||||
|
" linewidth=2.0, color='white', linestyle='--')\n",
|
||||||
|
"\n",
|
||||||
|
" # Mark some particular places with a small circle and a name label...\n",
|
||||||
|
" # Define some test points with latitude and longitude coordinates.\n",
|
||||||
|
" city_data = [('London', 51.5072, 0.1275),\n",
|
||||||
|
" ('Halifax, NS', 44.67, -63.61),\n",
|
||||||
|
" ('Reykjavik', 64.1333, -21.9333)]\n",
|
||||||
|
" # Place a single marker point and a text annotation at each place.\n",
|
||||||
|
" for name, lat, lon in city_data:\n",
|
||||||
|
" plt.plot(lon, lat, marker='o', markersize=7.0, markeredgewidth=2.5,\n",
|
||||||
|
" markerfacecolor='black', markeredgecolor='white',\n",
|
||||||
|
" transform=crs_latlon)\n",
|
||||||
|
" # NOTE: the \"plt.annotate call\" does not have a \"transform=\" keyword,\n",
|
||||||
|
" # so for this one we transform the coordinates with a Cartopy call.\n",
|
||||||
|
" at_x, at_y = ax.projection.transform_point(lon, lat,\n",
|
||||||
|
" src_crs=crs_latlon)\n",
|
||||||
|
" plt.annotate(\n",
|
||||||
|
" name, xy=(at_x, at_y), xytext=(30, 20), textcoords='offset points',\n",
|
||||||
|
" color='black', backgroundcolor='white', size='large',\n",
|
||||||
|
" arrowprops=dict(arrowstyle='->', color='white', linewidth=2.5))\n",
|
||||||
|
"\n",
|
||||||
|
" # Add a title, and display.\n",
|
||||||
|
" plt.title('A pseudocolour plot on the {} projection,\\n'\n",
|
||||||
|
" 'with overlaid contours.'.format(projection_name))\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" # Demonstrate with two different display projections.\n",
|
||||||
|
" make_plot('Equidistant Cylindrical', ccrs.PlateCarree())\n",
|
||||||
|
" make_plot('North Polar Stereographic', ccrs.NorthPolarStereo())\n",
|
||||||
|
" make_plot('LambertConformal', ccrs.LambertConformal())\n",
|
||||||
|
" make_plot('Robinson', ccrs.Robinson())\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ccrs.Projection?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"* AlbersEqualArea\n",
|
||||||
|
"* AzimuthalEquidistant\n",
|
||||||
|
"* EuroPP\n",
|
||||||
|
"* Gnomonic\n",
|
||||||
|
"* GOOGLE_MERCATOR\n",
|
||||||
|
"* LambertConformal\n",
|
||||||
|
"* LambertCylindrical\n",
|
||||||
|
"* Mercator\n",
|
||||||
|
"* Miller\n",
|
||||||
|
"* Mollweide\n",
|
||||||
|
"* Orthographic\n",
|
||||||
|
"* OSGB\n",
|
||||||
|
"* Robinson\n",
|
||||||
|
"* RotatedGeodetic\n",
|
||||||
|
"* RotatedPole\n",
|
||||||
|
"* Stereographic\n",
|
||||||
|
"* TransverseMercator\n",
|
||||||
|
"* UTM\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,146 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.quickplot as qplt\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"## IRIS Quickplot example -- Contour, fill, pcolormesh\n",
|
||||||
|
"## Live 8.5 * darkblue-b\n",
|
||||||
|
"##"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## demo data of Sea Surface Temperatures as netCDF\n",
|
||||||
|
"file_path = '/usr/lib/python2.7/dist-packages/cartopy/data/netcdf/'\n",
|
||||||
|
"filename = 'HadISST1_SST_update.nc'\n",
|
||||||
|
"cubes = iris.load(file_path+filename)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## IRIS format - show a text description of the 0th cube\n",
|
||||||
|
"\n",
|
||||||
|
"print 'Cubes defined: ' + str(len(cubes))\n",
|
||||||
|
"print cubes[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## store the 0th time slice of all lat,lon data points\n",
|
||||||
|
"sst0 = cubes[0][0,...]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## IRIS Quickplot contour\n",
|
||||||
|
"## http://scitools.org.uk/iris/docs/v1.6/iris/iris/quickplot.html#iris.quickplot.contour\n",
|
||||||
|
"contour = qplt.contour(sst0)\n",
|
||||||
|
"\n",
|
||||||
|
"# Add coastlines to the map created by contour.\n",
|
||||||
|
"plt.gca().coastlines()\n",
|
||||||
|
"\n",
|
||||||
|
"# Add contour labels based on the contour we have just created.\n",
|
||||||
|
"plt.clabel(contour)\n",
|
||||||
|
"\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Filled polygons, values legend, continental interior legend\n",
|
||||||
|
"## http://scitools.org.uk/iris/docs/v1.6/iris/iris/quickplot.html#iris.quickplot.contour\n",
|
||||||
|
"\n",
|
||||||
|
"qplt.contourf(sst0)\n",
|
||||||
|
"plt.gca().stock_img()\n",
|
||||||
|
"plt.gca().coastlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Pseudocolor Mesh\n",
|
||||||
|
"## http://scitools.org.uk/iris/docs/v1.6/iris/iris/quickplot.html#iris.quickplot.pcolormesh\n",
|
||||||
|
"\n",
|
||||||
|
"qplt.pcolormesh(sst0)\n",
|
||||||
|
"plt.gca().stock_img()\n",
|
||||||
|
"plt.gca().coastlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,119 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt\n",
|
||||||
|
"\n",
|
||||||
|
"## netCDF via IRIS example\n",
|
||||||
|
"## Live 8.5 * darkblue-b\n",
|
||||||
|
"##"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Convert a DEM GeoTIFF to netCDF with gdal\n",
|
||||||
|
"!rm 'sample_data/SanMateo_CA.cf'\n",
|
||||||
|
"!gdal_translate -of netcdf sample_data/SanMateo_CA.tif sample_data/SanMateo_CA.cf\n",
|
||||||
|
"\n",
|
||||||
|
"## previously extracted from a large USGS DEM via\n",
|
||||||
|
"## gdal_translate -projwin -122.44 37.7 -122.2 37.4 -of GTiff imgn38w123_1.img SanMateo_CA.tif"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## Open the netCDF with IRIS\n",
|
||||||
|
"res = iris.load('sample_data/SanMateo_CA.cf')\n",
|
||||||
|
"\n",
|
||||||
|
"print ' TYPE: ', type(res)\n",
|
||||||
|
"print 'CUBES: ', res\n",
|
||||||
|
"print ' REPR: ', res[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## color visualization of elevation data using IRIS Quickplot\n",
|
||||||
|
"iplt.pcolormesh(res[0], cmap=plt.get_cmap('gist_earth'))\n",
|
||||||
|
"iplt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"contour = qplt.contour(res[0])\n",
|
||||||
|
"\n",
|
||||||
|
"## use the center point for a quick demo\n",
|
||||||
|
"lon = -122.3200000\n",
|
||||||
|
"lat = 37.5500000\n",
|
||||||
|
"\n",
|
||||||
|
"plt.scatter(lon, lat, marker=(5, 1), color='red', s=200)\n",
|
||||||
|
"plt.title(\"Welcome to San Mateo\")\n",
|
||||||
|
"\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,111 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy.ma as ma\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Ionosphere space weather\n",
|
||||||
|
"========================\n",
|
||||||
|
"\n",
|
||||||
|
"This space weather example plots a filled contour of rotated pole point\n",
|
||||||
|
"data with a shaded relief image underlay. The plot shows aggregated\n",
|
||||||
|
"vertical electron content in the ionosphere.\n",
|
||||||
|
"\n",
|
||||||
|
"The plot exhibits an interesting outline effect due to excluding data\n",
|
||||||
|
"values below a certain threshold.\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" # Load the \"total electron content\" cube.\n",
|
||||||
|
" #filename = iris.sample_data_path('space_weather.nc')\n",
|
||||||
|
" filename = 'sample_data/space_weather.nc'\n",
|
||||||
|
" cube = iris.load_cube(filename, 'total electron content')\n",
|
||||||
|
"\n",
|
||||||
|
" # Explicitly mask negative electron content.\n",
|
||||||
|
" cube.data = ma.masked_less(cube.data, 0)\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot the cube using one hundred colour levels.\n",
|
||||||
|
" qplt.contourf(cube, 100)\n",
|
||||||
|
" plt.title('Total Electron Content')\n",
|
||||||
|
" plt.xlabel('longitude / degrees')\n",
|
||||||
|
" plt.ylabel('latitude / degrees')\n",
|
||||||
|
" plt.gca().stock_img()\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" iplt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,144 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Quickplot of a 2d cube on a map\n",
|
||||||
|
"===============================\n",
|
||||||
|
"\n",
|
||||||
|
"This example demonstrates a contour plot of global air temperature.\n",
|
||||||
|
"The plot title and the labels for the axes are automatically derived from the metadata.\n",
|
||||||
|
"(added example of to_xml -ed)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" #fname = iris.sample_data_path('air_temp.pp')\n",
|
||||||
|
" fname = 'sample_data/air_temp.pp'\n",
|
||||||
|
" temperature = iris.load_cube(fname)\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot #1: contourf with axes longitude from -180 to 180\n",
|
||||||
|
" fig = plt.figure(figsize=(12, 5))\n",
|
||||||
|
" plt.subplot(121)\n",
|
||||||
|
" qplt.contourf(temperature, 15)\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot #2: contourf with axes longitude from 0 to 360\n",
|
||||||
|
" proj = ccrs.PlateCarree(central_longitude=-180.0)\n",
|
||||||
|
" ax = plt.subplot(122, projection=proj)\n",
|
||||||
|
" qplt.contourf(temperature, 15)\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" iplt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#fname = iris.sample_data_path('air_temp.pp')\n",
|
||||||
|
"fname = 'sample_data/air_temp.pp'\n",
|
||||||
|
"t0 = iris.load_cube(fname)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false,
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"t0.xml()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"whos"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,134 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import cartopy.crs as ccrs\n",
|
||||||
|
"\n",
|
||||||
|
"import iris\n",
|
||||||
|
"import iris.plot as iplt\n",
|
||||||
|
"import iris.quickplot as qplt\n",
|
||||||
|
"import iris.analysis.cartography"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Rotated pole mapping\n",
|
||||||
|
"=====================\n",
|
||||||
|
"\n",
|
||||||
|
"This example uses several visualisation methods to achieve an array of\n",
|
||||||
|
"differing images, including:\n",
|
||||||
|
"\n",
|
||||||
|
" * Visualisation of point based data\n",
|
||||||
|
" * Contouring of point based data\n",
|
||||||
|
" * Block plot of contiguous bounded data\n",
|
||||||
|
" * Non native projection and a Natural Earth shaded relief image underlay\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"### http://scitools.org.uk/iris/docs/v1.9.0/html/gallery.html"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" #fname = iris.sample_data_path('rotated_pole.nc')\n",
|
||||||
|
" fname = 'sample_data/rotated_pole.nc'\n",
|
||||||
|
" air_pressure = iris.load_cube(fname)\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot #1: Point plot showing data values & a colorbar\n",
|
||||||
|
" plt.figure()\n",
|
||||||
|
" points = qplt.points(air_pressure, c=air_pressure.data)\n",
|
||||||
|
" cb = plt.colorbar(points, orientation='horizontal')\n",
|
||||||
|
" cb.set_label(air_pressure.units)\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot #2: Contourf of the point based data\n",
|
||||||
|
" plt.figure()\n",
|
||||||
|
" qplt.contourf(air_pressure, 15)\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot #3: Contourf overlayed by coloured point data\n",
|
||||||
|
" plt.figure()\n",
|
||||||
|
" qplt.contourf(air_pressure)\n",
|
||||||
|
" iplt.points(air_pressure, c=air_pressure.data)\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" iplt.show()\n",
|
||||||
|
"\n",
|
||||||
|
" # For the purposes of this example, add some bounds to the latitude\n",
|
||||||
|
" # and longitude\n",
|
||||||
|
" air_pressure.coord('grid_latitude').guess_bounds()\n",
|
||||||
|
" air_pressure.coord('grid_longitude').guess_bounds()\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot #4: Block plot\n",
|
||||||
|
" plt.figure()\n",
|
||||||
|
" plt.axes(projection=ccrs.PlateCarree())\n",
|
||||||
|
" iplt.pcolormesh(air_pressure)\n",
|
||||||
|
" plt.gca().stock_img()\n",
|
||||||
|
" plt.gca().coastlines()\n",
|
||||||
|
" iplt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"main()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,132 @@
|
||||||
|
|
||||||
|
# Spatial Data Analysis with PySAL and GeoDaSpace
|
||||||
|
|
||||||
|
**[Sergio Rey] and [Luc Anselin]**
|
||||||
|
|
||||||
|
**November 11, 2015**
|
||||||
|
|
||||||
|
**NARSC 2015, Portland, Oregon**
|
||||||
|
|
||||||
|
## Tutorial Description
|
||||||
|
|
||||||
|
A unique feature of this tutorial is the use of Python based software tools for spatial data analysis. Python is an object oriented scripting language that is gaining rapid adoption in the scientific computing and data science communities. To facilitate its adoption within the GIScience community, Rey and Anselin have collaborated on the creation of [PySAL]: Python Library for Spatial Analysis. Since its initial release in July 2010, PySAL has been downloaded over 75,000 times and is now included in well- known open source scientific data analysis distributions, such as Anaconda. This two-part tutorial will introduce participants to the latest version of PySAL as well as to GeoDaSpace, a GUI application based on PySAL designed for spatial econometric analysis. The first component introduces the basic organization and philosophy of PySAL, with a special focus on exploratory spatial data analysis. In the second part of the tutorial the focus moves to practical spatial regression analysis using GeoDaSpace and the PySAL spreg module.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- Previous experience with Python programming is recommended
|
||||||
|
- Participants should bring their own laptops to the workshop
|
||||||
|
- Software should be installed prior to traveling to the workshop (instructions below)
|
||||||
|
|
||||||
|
### Software Requirements
|
||||||
|
**Please note that PySAL is pre-installed here -- feb2016
|
||||||
|
|
||||||
|
|
||||||
|
For the workshop we will require the following packages be installed
|
||||||
|
|
||||||
|
- PySAL 1.10.0
|
||||||
|
- SciPy
|
||||||
|
- Numpy
|
||||||
|
- iPython
|
||||||
|
- ipywidgets
|
||||||
|
- jupyter
|
||||||
|
- matplotlib
|
||||||
|
- pandas
|
||||||
|
- folium
|
||||||
|
- [GeoDaSpace][GeoDaSpace]
|
||||||
|
|
||||||
|
There are a number of ways to install PySAL and these dependencies. For the workshop, if you do not yet have the dependencies installed we suggest using one of two scientific Python distributions (below). These have the advantages of including most of the dependencies for PySAL as well as PySAL itself. Moreover, both allow for updating PySAL to the most recent release (1.10 released July 31, 2015) which is more current that what is listed in either distribution. Both of these distributions also allow for installation of our final dependency, folium.
|
||||||
|
|
||||||
|
#### PySAL via Anaconda Python Distribution
|
||||||
|
|
||||||
|
1. Install [Anaconda Python Distribution][Anaconda]
|
||||||
|
2. Open a terminal (Mac or Linux) or Powershell (Windows)
|
||||||
|
2. `pip install -U pysal`
|
||||||
|
3. `pip install -U folium`
|
||||||
|
|
||||||
|
#### Creating a Custom Conda Environment
|
||||||
|
|
||||||
|
If you already have Anaconda installed and you did not want to modify your default environemnt, you can create a *custom conda environment* for this session using the following commands:
|
||||||
|
|
||||||
|
1. `conda create -n pysal110 scipy matplotlib jupyter ipython pandas ipywidgets`
|
||||||
|
2. `source activate pysal110`
|
||||||
|
4. `pip install -U pysal`
|
||||||
|
5. `pip install -U folium`
|
||||||
|
|
||||||
|
When you are done working in this environment, you can get back to your default environment with:
|
||||||
|
|
||||||
|
`source deactivate`
|
||||||
|
|
||||||
|
|
||||||
|
#### PySAL via Enthought Canopy
|
||||||
|
|
||||||
|
Note that the Academic version of Canopy comes with PySAL version 1.7. For this workshop we will be using PySAL 1.10. Upgrading in Canopy can be done as follows:
|
||||||
|
|
||||||
|
1. Install [Canopy][Canopy]
|
||||||
|
2. Run Canopy
|
||||||
|
3. From the menu select `Tools Canopy Terminal`
|
||||||
|
4. `pip install -U pysal`
|
||||||
|
5. `pip install -U folium`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### Testing Your Installation
|
||||||
|
|
||||||
|
Once you have installed all the dependencies, you can check to confirm everything is ready to go.
|
||||||
|
|
||||||
|
For Anaconda:
|
||||||
|
|
||||||
|
1. Open a terminal (Mac or Linux) or Powershell (Windows)
|
||||||
|
2. `ipython notebook`
|
||||||
|
3. In the browser click `New Notebook`
|
||||||
|
3. In the first cell in the notebook enter
|
||||||
|
`import pysal`
|
||||||
|
`pysal.version`
|
||||||
|
Then `<Shift-Enter>` (i.e., hit the Shift then the Enter Key)
|
||||||
|
4. In the second cell in the notebook enter
|
||||||
|
`import folium`
|
||||||
|
Then `<Shift-Enter>`
|
||||||
|
5. In the third cell enter
|
||||||
|
`!which python`
|
||||||
|
Then `<Shift-Enter>`
|
||||||
|
|
||||||
|
Your screen should look something like:
|
||||||
|
![Anaconda setup](esda/figures/anaconda.png)
|
||||||
|
|
||||||
|
|
||||||
|
For Enthought Canopy:
|
||||||
|
|
||||||
|
2. Run Canopy
|
||||||
|
3. From the menu select `Tools Canopy Terminal`
|
||||||
|
2. `ipython notebook`
|
||||||
|
3. In the browser click `New Notebook`
|
||||||
|
3. In the first cell in the notebook enter
|
||||||
|
`import pysal`
|
||||||
|
`pysal.version`
|
||||||
|
Then `<Shift-Enter>` (i.e., hit the Shift then the Enter Key)
|
||||||
|
4. In the second cell in the notebook enter
|
||||||
|
`import folium`
|
||||||
|
Then `<Shift-Enter>`
|
||||||
|
5. In the third cell enter
|
||||||
|
`!which python`
|
||||||
|
Then `<Shift-Enter>`
|
||||||
|
|
||||||
|
|
||||||
|
Your screen should look something like:
|
||||||
|
![Enthought setup](esda/figures/enthought.png)
|
||||||
|
|
||||||
|
|
||||||
|
#### Issues
|
||||||
|
|
||||||
|
If you run into any problems, double check that you have installed both the upgraded version of PySAL and folium (see above). If problems persist, please contact me <sjsrey@gmail.com>.
|
||||||
|
|
||||||
|
|
||||||
|
[PySAL]: http://pysal.org
|
||||||
|
[GeoDaSpace]: https://geodacenter.asu.edu/software/downloads/geodaspace
|
||||||
|
[Anaconda]: http://continuum.io/downloads.html
|
||||||
|
[Canopy]: https://www.enthought.com/store
|
||||||
|
[VirtualBox]: https://www.virtualbox.org/wiki/Downloads
|
||||||
|
[VirtualBox 4.3.12]: http://download.virtualbox.org/virtualbox/4.3.12/VirtualBox-4.3.12-93733-Win.exe
|
||||||
|
[Vagrant]: http://www.vagrantup.com/downloads.html
|
||||||
|
[Vagrantfile]: Vagrantfile
|
||||||
|
[Sergio Rey]: https://geoplan.asu.edu/people/sergio-j-rey
|
||||||
|
[Luc Anselin]: https://geoplan.asu.edu/people/luc-anselin
|
|
@ -0,0 +1,240 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Spatial Data Analysis with PySAL and GeoDaSpace\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Authors: Sergio Rey and Luc Anselin\n",
|
||||||
|
"\n",
|
||||||
|
"Date: 2015-11-11 \n",
|
||||||
|
"\n",
|
||||||
|
"Location: NARSC 2015, Portland, Oregon"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Introduction\n",
|
||||||
|
"\n",
|
||||||
|
"A unique feature of this tutorial is the use of Python based software tools for spatial data analysis. Python is an object oriented scripting language that is gaining rapid adoption in the scientific computing and data science communities. To facilitate its adoption within the GIScience community, Rey and Anselin have collaborated on the creation of PySAL: Python Library for Spatial Analysis. Since its initial release in July 2010, PySAL has been downloaded over 75,000 times and is now included in well- known open source scientific data analysis distributions, such as Anaconda. This two-part tutorial will introduce participants to the latest version of PySAL as well as to GeoDaSpace, a GUI application based on PySAL designed for spatial econometric analysis. The first component introduces the basic organization and philosophy of PySAL, with a special focus on exploratory spatial data analysis. In the second part of the tutorial the focus moves to practical spatial regression analysis using GeoDaSpace and the PySAL spreg module."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Outline\n",
|
||||||
|
"\n",
|
||||||
|
"This is a full-day tutorial broken into two components.\n",
|
||||||
|
"\n",
|
||||||
|
"### Part I: Exploratory Spatial Data Analysis with PySAL\n",
|
||||||
|
"\n",
|
||||||
|
"- PySAL introduction\n",
|
||||||
|
"- Notebooks and installation\n",
|
||||||
|
"- Visualization\n",
|
||||||
|
"- Spatial data processing\n",
|
||||||
|
"- Spatial weights\n",
|
||||||
|
"- GeoVisualization\n",
|
||||||
|
"- Global spatial autocorrelation\n",
|
||||||
|
"- Local spatial autocorrelation\n",
|
||||||
|
"\n",
|
||||||
|
"### Part II: Spatial Regression Analysis with GeoDaSpace\n",
|
||||||
|
"\n",
|
||||||
|
"- GeoDaSpace introduction\n",
|
||||||
|
"- Basic regression\n",
|
||||||
|
"- Spatial diagnostics\n",
|
||||||
|
"- Nonspatial endogeneity\n",
|
||||||
|
"- Spatial lag model\n",
|
||||||
|
"- Spatial error model\n",
|
||||||
|
"- Spatial heterogeneity"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# PySAL: Python Spatial Analysis Library\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"## Origins and Objectives\n",
|
||||||
|
"\n",
|
||||||
|
"- Leverage Existing Tools Development\n",
|
||||||
|
"\n",
|
||||||
|
" - GeoDa/PySpace\n",
|
||||||
|
" - STARS\n",
|
||||||
|
"\n",
|
||||||
|
"- Develop Core Library\n",
|
||||||
|
"\n",
|
||||||
|
" - spatial data *analytical* functions\n",
|
||||||
|
" - enhanced specialization and modularity\n",
|
||||||
|
" - fill *void* in geospatial Python libraries\n",
|
||||||
|
" \n",
|
||||||
|
"- Flexible Delivery Mechanisms\n",
|
||||||
|
"\n",
|
||||||
|
" - interactive shell\n",
|
||||||
|
" - GUI\n",
|
||||||
|
" - Toolkits\n",
|
||||||
|
" - webservices\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Components\n",
|
||||||
|
"![PySAL](figures/pysalgraph.png)\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Delivery Mechanisms\n",
|
||||||
|
"\n",
|
||||||
|
"### Interactive Shell: iPython Notebook\n",
|
||||||
|
"![pysalipnb](figures/pysalnb.png)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"### Toolkits: ArcGIS + spreg\n",
|
||||||
|
"![arctool](figures/10_arctool.png)\n",
|
||||||
|
"### Toolkits: QGIS + pysal\n",
|
||||||
|
"![arctool](figures/qgis.png)\n",
|
||||||
|
"### Webservices: cgPySAL\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"![cgpysal](figures/cgpysal.png)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"### GUI: Crime Analytics for Space-Time (CAST)\n",
|
||||||
|
"\n",
|
||||||
|
"![cast](figures/cast.png)\n",
|
||||||
|
"\n",
|
||||||
|
"### GUI: GeoDaSpace (PySAL-spreg)\n",
|
||||||
|
"![gsgui](figures/gsgui.png)\n",
|
||||||
|
"\n",
|
||||||
|
"![gsout](figures/gsoutput.png)\n",
|
||||||
|
"\n",
|
||||||
|
"![weightsviewer](figures/weightsviewer.png)\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Team\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"| Serge Rey | Luc Anselin |\n",
|
||||||
|
"|-----------------|------------------|\n",
|
||||||
|
"| Charles Schnidt | David Folch |\n",
|
||||||
|
"| Myunghwa Hwang | Dani Arribas |\n",
|
||||||
|
"| Phil Stephens | Julia Koschinsky |\n",
|
||||||
|
"| Pedro Amaral | Nick Malizia |\n",
|
||||||
|
"| Xing Kang | Xun Li |\n",
|
||||||
|
"| Mark McCann | Ran Wei |\n",
|
||||||
|
"| Nancy Lozano | Jing Yao |\n",
|
||||||
|
"| Jay Laura | Levi Wolf |\n",
|
||||||
|
"| Sizhe Wang | Wei Kang |\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"and, contributions from *many* others!\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"## Releases\n",
|
||||||
|
"\n",
|
||||||
|
"### Six-Month Release Cycle\n",
|
||||||
|
" - 1.0 July 2010\n",
|
||||||
|
" - 1.1 January 2011\n",
|
||||||
|
" - 1.2 July 2011\n",
|
||||||
|
" - 1.3 January 2012\n",
|
||||||
|
" - 1.4 July 2012\n",
|
||||||
|
" - 1.5 January 2013\n",
|
||||||
|
" - 1.6 July 2013\n",
|
||||||
|
" - 1.7 January 2014\n",
|
||||||
|
" - **1.8 July 2014**\n",
|
||||||
|
" \n",
|
||||||
|
"### Scientific Python Distributions\n",
|
||||||
|
"\n",
|
||||||
|
"#### Anaconda Python Distribution\n",
|
||||||
|
"![anaconda](figures/anaconda_pysal.png)\n",
|
||||||
|
"\n",
|
||||||
|
"#### Enthought Canopy \n",
|
||||||
|
"\n",
|
||||||
|
"![anaconda](figures/enthought_pysal.png)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"## Acknowledgements\n",
|
||||||
|
"\n",
|
||||||
|
"![funding](figures/ack.png)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
" \n",
|
||||||
|
"## Development\n",
|
||||||
|
"\n",
|
||||||
|
"[![PySAL.org](figures/pysal_org.png)](http://pysal.org)\n",
|
||||||
|
"\n",
|
||||||
|
"## References\n",
|
||||||
|
"\n",
|
||||||
|
"1. S. J. Rey. Python Spatial Analysis Library (PySAL): An update and illustration. In C. Brunsdon and A. SIngleton, editors, Geocomputation. Sage, In Press 2014.\n",
|
||||||
|
"2. S. J. Rey and L. Anselin. PySAL: A Python library of spatial analytical methods. In M. M. Fischer and A. Getis, editors, Handbook of Applied Spatial Analysis, pages 175–193. Springer, Berlin, 2010.\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,400 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# PySAL"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Documentation"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import HTML\n",
|
||||||
|
"HTML('<iframe src=http://pysal.org/?useformat=mobile width=800 height=550></iframe>')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Source Code"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Source is at http://github.com/pysal"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Introduction to IPython Notebook\n",
|
||||||
|
"\n",
|
||||||
|
"We will cover the basic operations/interface of the notebook which we will, in turn, use for the remainder of the workshop"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## What is IPython Notebook (now called Jupyter)\n",
|
||||||
|
"\n",
|
||||||
|
"- web-based interface for working with Python (and other languages)\n",
|
||||||
|
"- Similar in spirit to a scientific notebook\n",
|
||||||
|
" - record experiments\n",
|
||||||
|
" - document\n",
|
||||||
|
" - share\n",
|
||||||
|
"- literate programming\n",
|
||||||
|
"- simple JSON format\n",
|
||||||
|
" - web citizen\n",
|
||||||
|
" - git friendly"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Starting the notebook"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"From a shell or terminal we can start the notebook with:\n",
|
||||||
|
"```\n",
|
||||||
|
"ipython notebook\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"This brings up the **dashboard** which will list any notebooks encountered in the current working directory.\n",
|
||||||
|
"\n",
|
||||||
|
"You can either open an existing notebook or create a new one from the dashboard."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import Image\n",
|
||||||
|
"def show_image(img):\n",
|
||||||
|
" i = Image(filename = img)\n",
|
||||||
|
" return i"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"show_image('images/dashboard.png')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Notebook Inteface"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Menu"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"show_image('images/menu.png')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Help System"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"show_image('images/help.png')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Keyboard Shortcuts\n",
|
||||||
|
"\n",
|
||||||
|
"A list of keyboard shortcuts can be revealed by entering `h`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"show_image('images/shortcuts.png')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Note there are two *modes* for the notebook, *Edit Mode* and *Command mode*. The latter is used to *manipulate* notebook cells, while the former is used to *edit* the content of a cell.\n",
|
||||||
|
"\n",
|
||||||
|
"- To enter *Command Mode* press `ESC`.\n",
|
||||||
|
"- To enter *Edit Mode* press `Enter` on a cell.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Navigation\n",
|
||||||
|
"\n",
|
||||||
|
"When in command mode, you can use keyboard shortcuts to move from cell to cell\n",
|
||||||
|
"\n",
|
||||||
|
"- `k` moves the cursor up one cell\n",
|
||||||
|
"- `j` moves the cursor down one cell\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Cell Types"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Code Cells"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x = range(10) # Shift-Enter to execute and create a new cell"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x = range(10)\n",
|
||||||
|
"y = [ xi*3 for xi in x] # Ctrl-Return to execute but stay in the current cell"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Comments\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Text Cells"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"In command mode, `t` will set the cell to text. After that, pressing `Enter` will let you edit the text."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Markdown Cells"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"In command mode, ```m``` gives us a [Markdown] cell.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"[Markdown]: http://daringfireball.net/projects/markdown/\n",
|
||||||
|
"\n",
|
||||||
|
"We can then use Markdown syntax to do things like lists:\n",
|
||||||
|
"\n",
|
||||||
|
"- first\n",
|
||||||
|
"- second\n",
|
||||||
|
"- third\n",
|
||||||
|
" - nested one\n",
|
||||||
|
" - nested two\n",
|
||||||
|
"- fourth\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"As well as **LaTeX**.\n",
|
||||||
|
"\n",
|
||||||
|
"This is an in-line equation $\\hat{\\beta} = (X'X)^{-1}X'y$ for the ordinary least squares estimator.\n",
|
||||||
|
"\n",
|
||||||
|
"A display equation is done with\n",
|
||||||
|
"\n",
|
||||||
|
"$$ y = \\rho W y + X\\beta + \\epsilon$$\n",
|
||||||
|
"\n",
|
||||||
|
"**NOTE**: LaTeX will only render if mathjax is available via a network connection or if it has been installed locally."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Kernel"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Stoping and Restarting"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"`00` (enter the zero key twice) will restart the kernel."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Exporting and Saving"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"You can save in several formats via the **File** menu entry"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"show_image('images/save.png')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Saving\n",
|
||||||
|
"\n",
|
||||||
|
"In command mode, `s` will save the notebook, and mark a checkpoint. You can use checkpoints to rollback to a previous version if needed."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,588 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Spatial Data Processing with PySAL\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook will cover basic spatial data processing and using PySAL with the Notebook\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Common Imports"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# our convention is to alias PySAL, NumPy and Pandas\n",
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import pandas as pd"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# check the versions\n",
|
||||||
|
"ps.version"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"np.version.short_version"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"pd.__version__"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Reading a CSV File"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"!head data/mexico.csv"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"f = ps.open(\"data/mexico.csv\")\n",
|
||||||
|
"vnames = [\"pcgdp%d\"%decade for decade in range(1940, 2010, 10)]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"vnames"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"Y = np.transpose(np.array([f.by_col[v] for v in vnames]))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"Y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"state = f.by_col['State']"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"f.close() # done with the file"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"state"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Reading a Shapefile"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# us counties example"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"### First the attributes from the dbf"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dbf = ps.open('data/NAT.dbf')\n",
|
||||||
|
"header = dbf.header"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"header"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Read all the numeric variables into a big array\n",
|
||||||
|
"# find the first offset we need\n",
|
||||||
|
"start_col = header.index(\"SOUTH\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"start_col"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"vars = header[8:]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"vars"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"nat_array = np.array([np.array(dbf.by_col(var)) for var in vars])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"nat_array.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"nat_array = nat_array.T"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"nat_array.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dbf.close() # done with the dbf file\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Now the geometries "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shp_file = ps.open(\"data/NAT.shp\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shp_file.header"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"len(shp_file)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shapes = [ shp_file.next() for i in xrange(len(shp_file)) ]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"type(shapes)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"len(shapes)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"type(shapes)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"s0 = shapes[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"s0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dir(s0)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shp_file.close()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Reading a GeoJSON File"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import json"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"f = open('data/nat.json')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fj = json.load(f)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"f.close()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fj.keys()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"features = []\n",
|
||||||
|
"for feature in fj['features']:\n",
|
||||||
|
" features.append(feature)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"features[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,473 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## X,Y Plots"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"I = np.exp(-d)\n",
|
||||||
|
"plt.plot(d,I)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"I = np.exp(-d)\n",
|
||||||
|
"plt.ylabel('I')\n",
|
||||||
|
"plt.xlabel('d')\n",
|
||||||
|
"plt.plot(d,I)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"I = np.exp(-d)\n",
|
||||||
|
"plt.ylabel(r'$I= \\exp(-d)$')\n",
|
||||||
|
"plt.xlabel('d')\n",
|
||||||
|
"plt.plot(d,I)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"type(d)\n",
|
||||||
|
"d[0:10]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"I = d**(-2)\n",
|
||||||
|
"plt.ylabel(r'$I= 1 / d^2$')\n",
|
||||||
|
"plt.xlabel('d')\n",
|
||||||
|
"plt.plot(d,I)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"Iid2 = d**(-2)\n",
|
||||||
|
"plt.plot(d,Iid2)\n",
|
||||||
|
"plt.ylabel(r'$I$')\n",
|
||||||
|
"Inex = np.exp(-d)\n",
|
||||||
|
"plt.plot(d,Inex)\n",
|
||||||
|
"plt.xlabel('d')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"Iid2 = d**(-2)\n",
|
||||||
|
"plt.plot(d,Iid2, label = r'$I = d^{-2}$')\n",
|
||||||
|
"plt.ylabel(r'$I$')\n",
|
||||||
|
"Inex = np.exp(-d)\n",
|
||||||
|
"plt.plot(d,Inex, label = r'$I = \\exp(-d)$')\n",
|
||||||
|
"plt.xlabel('d')\n",
|
||||||
|
"plt.legend()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.legend?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"Iid2 = d**(-2)\n",
|
||||||
|
"plt.plot(d,Iid2, 'r-', label = r'$I = d^{-2}$')\n",
|
||||||
|
"plt.ylabel(r'$I$')\n",
|
||||||
|
"Inex = np.exp(-d)\n",
|
||||||
|
"plt.plot(d,Inex, 'g--',label = r'$I = \\exp(-d)$')\n",
|
||||||
|
"plt.xlabel('d')\n",
|
||||||
|
"plt.legend()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"d = np.linspace(1,10,200)\n",
|
||||||
|
"Iid = d**(-1)\n",
|
||||||
|
"plt.plot(d,Iid, 'b-.', label = r'$I = d^{-1}$')\n",
|
||||||
|
"Iid2 = d**(-2)\n",
|
||||||
|
"plt.plot(d,Iid2, 'r-', label = r'$I = d^{-2}$')\n",
|
||||||
|
"plt.ylabel(r'$I$')\n",
|
||||||
|
"Inex = np.exp(-d)\n",
|
||||||
|
"plt.plot(d,Inex, 'g--',label = r'$I = \\exp(-d)$')\n",
|
||||||
|
"plt.xlabel('d')\n",
|
||||||
|
"plt.legend()\n",
|
||||||
|
"plt.title('Distance Decay')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Scatter Plot"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x = np.arange(100)\n",
|
||||||
|
"y = 0.5 + np.random.normal(size=(100,))\n",
|
||||||
|
"plt.scatter(x,y)\n",
|
||||||
|
"plt.xlabel('x')\n",
|
||||||
|
"plt.ylabel('y')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.scatter(x,y)\n",
|
||||||
|
"plt.vlines(x.mean(),y.min(),y.max())\n",
|
||||||
|
"plt.hlines(x.min(),x.max(),y.mean())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Distributions and Densities"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.mlab as mlab\n",
|
||||||
|
"\n",
|
||||||
|
"x = np.random.multivariate_normal([100,70],[[2.,1.7],[1.7,2.]], (50))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(x[:,0], x[:,1], \"o\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x0h = plt.hist(x[:,0])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x1h = plt.hist(x[:,1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"n,bins,bars = plt.hist(x[:,1])\n",
|
||||||
|
"\n",
|
||||||
|
"y = mlab.normpdf(bins, np.mean(x[:,1]), np.std(x[:,1]))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(bins,y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(bins,y)\n",
|
||||||
|
"plt.hist(x[:,1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(bins,y)\n",
|
||||||
|
"plt.hist(x[:,1],normed=1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Saving to a file"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(bins,y)\n",
|
||||||
|
"plt.hist(x[:,1],normed=1)\n",
|
||||||
|
"plt.savefig(\"hist.png\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"!ls *.png"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from scipy.stats import gaussian_kde"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = x[:,1]\n",
|
||||||
|
"density = gaussian_kde(y)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"xs = np.linspace(y.min(),y.max(),30)\n",
|
||||||
|
"plt.plot(xs, density(xs))\n",
|
||||||
|
"plt.xlabel('y')\n",
|
||||||
|
"plt.ylabel('f(y)')\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"xs = np.linspace(y.min()*.8,y.max()*1.2,30)\n",
|
||||||
|
"plt.plot(xs, density(xs))\n",
|
||||||
|
"plt.xlabel('y')\n",
|
||||||
|
"plt.ylabel('f(y)')\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"There is a lot more to matplotlib. One can visit the [gallery](http://matplotlib.org/gallery.html) and pull examples in to get a sense of what is possible, and how to adapt examples for your own purposes.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"For now we move on to generating choropleth maps."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,495 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Choropleth Mapping With Folium and PySAL"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"____\n",
|
||||||
|
"## Author\n",
|
||||||
|
"\n",
|
||||||
|
"Serge Rey [sjsrey@gmail.com](sjsrey@gmail)\n",
|
||||||
|
"\n",
|
||||||
|
"## Requirements\n",
|
||||||
|
"\n",
|
||||||
|
"Since we will be pulling in tiles for basemaps, we need internet connectivity for what follows to work.\n",
|
||||||
|
"____\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import HTML\n",
|
||||||
|
"\n",
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"#import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import folium\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Help functions\n",
|
||||||
|
"In order to have folium maps appear in-line we will need two utility functions.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def inline_map(map):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds the HTML source of the map directly into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" This method will not work if the map depends on any files (json data). Also this uses\n",
|
||||||
|
" the HTML5 srcdoc attribute, which may not be supported in all browsers.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map._build_map()\n",
|
||||||
|
" return HTML('<iframe srcdoc=\"{srcdoc}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(srcdoc=map.HTML.replace('\"', '"')))\n",
|
||||||
|
"\n",
|
||||||
|
"def embed_map(map, path=\"map.html\"):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds a linked iframe to the map into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" Note: this method will not capture the source of the map into the notebook.\n",
|
||||||
|
" This method should work for all maps (as long as they use relative urls).\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map.create_map(path=path)\n",
|
||||||
|
" return HTML('<iframe src=\"files/{path}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(path=path))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The source for these functions is an iPython notebook by [Blake Burkhart](http://nbviewer.ipython.org/gist/bburky/7763555/folium-ipython.ipynb)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## OpenStreet Map Tile with Folium"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map = folium.Map(location=[45.52, -122.68], zoom_start=11)\n",
|
||||||
|
"map.simple_marker([45.52, -122.68], popup='Welcome to PySAL @<b>NARSC 2015</b>!')\n",
|
||||||
|
"inline_map(map)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Base layer for south"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=4)\n",
|
||||||
|
"map_osm.geo_json(geo_path = 'data/south.json')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Change the zoom level"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5)\n",
|
||||||
|
"map_osm.geo_json(geo_path = 'data/south.json')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Binding Attribute Data to the Map"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import json\n",
|
||||||
|
"\n",
|
||||||
|
"f = open(r'data/south.json')\n",
|
||||||
|
"q = json.load(f)\n",
|
||||||
|
"f.close()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"q.keys()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"features = q['features']\n",
|
||||||
|
"len(features)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"feature_0 = features[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"feature_0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"indices = []\n",
|
||||||
|
"values = []\n",
|
||||||
|
"for feature in features:\n",
|
||||||
|
" indices.append(str(feature['properties']['FIPS']))\n",
|
||||||
|
" values.append(feature['properties']['HR80'])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df = pd.DataFrame({'HR80': values,\n",
|
||||||
|
" 'FIPS': indices} )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','HR80'],\n",
|
||||||
|
" fill_color='YlGnBu', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" legend_name='Homicide Rate HR80')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Using PySAL Map Classification Schemes"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.array(df.HR80.tolist())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ps.version"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Maximum Breaks"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"max_breaks = ps.Maximum_Breaks(y, 5)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"bins = max_breaks.bins.tolist()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','HR80'],\n",
|
||||||
|
" fill_color='YlGnBu', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" threshold_scale=bins[:-1],\n",
|
||||||
|
" legend_name='Homicide Rate HR80 (Maximum Breaks)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Quantiles"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"q5 = ps.Quantiles(y, 5).bins.tolist()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','HR80'],\n",
|
||||||
|
" fill_color='YlGnBu', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" threshold_scale=q5[:-1],\n",
|
||||||
|
" legend_name='Homicide Rate HR80 (Quintiles)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"q6 = ps.Quantiles(y, 6).bins.tolist()\n",
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','HR80'],\n",
|
||||||
|
" fill_color='YlGnBu', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" threshold_scale=q6[:-1],\n",
|
||||||
|
" legend_name='Homicide Rate HR80 (Quantiles k=6)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Fisher-Jenks"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fj= ps.Fisher_Jenks(y, 5).bins.tolist()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','HR80'],\n",
|
||||||
|
" fill_color='YlGnBu', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" threshold_scale=fj[:-1],\n",
|
||||||
|
" legend_name='Homicide Rate HR80 (Fisher Jenks)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ps.esda.mapclassify.Fisher_Jenks_Sampled?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,296 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import folium\n",
|
||||||
|
"import random as RD\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"from pysal.contrib.viz import mapping as maps\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Guide for the `mapping` module in `PySAL`\n",
|
||||||
|
"\n",
|
||||||
|
"Contributors:\n",
|
||||||
|
"\n",
|
||||||
|
"* Dani Arribas-Bel `<daniel.arribas.bel@gmail.com>`\n",
|
||||||
|
"* Serge Rey `<sjsrey@gmail.com>`\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"This document describes the main structure, components and usage of the mapping module in `PySAL`. The is organized around three main layers:\n",
|
||||||
|
"\n",
|
||||||
|
"* A lower-level layer that reads polygon, line and point shapefiles and returns a Matplotlib collection.\n",
|
||||||
|
"* A medium-level layer that performs some usual transformations on a Matplotlib object (e.g. color code polygons according to a vector of values).\n",
|
||||||
|
"* A higher-level layer intended for end-users for particularly useful cases and style preferences pre-defined (e.g. Create a choropleth)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Lower-level component\n",
|
||||||
|
"\n",
|
||||||
|
"This includes basic functionality to read spatial data from a file (currently only shapefiles supported) and produce rudimentary Matplotlib objects. The main methods are:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* `map_poly_shape`: to read in polygon shapefiles"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* `map_line_shape`: to read in line shapefiles"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* `map_point_shape`: to read in point shapefiles"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"These methods all support an option to subset the observations to be plotted (very useful when missing values are present). They can also be overlaid and combined by using the `setup_ax` function. the resulting object is very basic but also very flexible so, for minds used to matplotlib this should be good news as it allows to modify pretty much any property and attribute."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Example"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shp_link = ps.examples.get_path('columbus.shp')\n",
|
||||||
|
"shp = ps.open(shp_link)\n",
|
||||||
|
"some = [bool(RD.getrandbits(1)) for i in ps.open(shp_link)]\n",
|
||||||
|
"\n",
|
||||||
|
"fig = plt.figure()\n",
|
||||||
|
"\n",
|
||||||
|
"base = maps.map_poly_shp(shp)\n",
|
||||||
|
"base.set_facecolor('none')\n",
|
||||||
|
"base.set_linewidth(0.75)\n",
|
||||||
|
"base.set_edgecolor('0.8')\n",
|
||||||
|
"some = maps.map_poly_shp(shp, which=some)\n",
|
||||||
|
"some.set_alpha(0.5)\n",
|
||||||
|
"some.set_linewidth(0.)\n",
|
||||||
|
"cents = np.array([poly.centroid for poly in ps.open(shp_link)])\n",
|
||||||
|
"pts = plt.scatter(cents[:, 0], cents[:, 1])\n",
|
||||||
|
"pts.set_color('red')\n",
|
||||||
|
"\n",
|
||||||
|
"ax = maps.setup_ax([base, some, pts])\n",
|
||||||
|
"fig.add_axes(ax)\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"some"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Medium-level component\n",
|
||||||
|
"\n",
|
||||||
|
"This layer comprises functions that perform usual transformations on matplotlib objects, such as color coding objects (points, polygons, etc.) according to a series of values. This includes the following methods:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* `base_choropleth_classless`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* `base_choropleth_unique`\n",
|
||||||
|
"\n",
|
||||||
|
"### Example"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"net_link = ps.examples.get_path('eberly_net.shp')\n",
|
||||||
|
"net = ps.open(net_link)\n",
|
||||||
|
"values = np.array(ps.open(net_link.replace('.shp', '.dbf')).by_col('TNODE'))\n",
|
||||||
|
"\n",
|
||||||
|
"pts_link = ps.examples.get_path('eberly_net_pts_onnetwork.shp')\n",
|
||||||
|
"pts = ps.open(pts_link)\n",
|
||||||
|
"\n",
|
||||||
|
"fig = plt.figure()\n",
|
||||||
|
"\n",
|
||||||
|
"netm = maps.map_line_shp(net)\n",
|
||||||
|
"netc = maps.base_choropleth_unique(netm, values)\n",
|
||||||
|
"\n",
|
||||||
|
"ptsm = maps.map_point_shp(pts)\n",
|
||||||
|
"ptsm = maps.base_choropleth_classif(ptsm, values)\n",
|
||||||
|
"ptsm.set_alpha(0.5)\n",
|
||||||
|
"ptsm.set_linewidth(0.)\n",
|
||||||
|
"\n",
|
||||||
|
"ax = maps.setup_ax([netc, ptsm])\n",
|
||||||
|
"fig.add_axes(ax)\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"* `base_choropleth_classif`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Higher-level component\n",
|
||||||
|
"\n",
|
||||||
|
"This currently includes the following end-user functions:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* `plot_poly_lines`: very quick shapefile plotting."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"maps.plot_poly_lines(ps.examples.get_path('columbus.shp'))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* `plot_choropleth`: for quick plotting of several types of chocopleths."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shp_link = ps.examples.get_path('columbus.shp')\n",
|
||||||
|
"values = np.array(ps.open(ps.examples.get_path('columbus.dbf')).by_col('HOVAL'))\n",
|
||||||
|
"\n",
|
||||||
|
"types = ['classless', 'unique_values', 'quantiles', 'equal_interval']\n",
|
||||||
|
"for typ in types:\n",
|
||||||
|
" maps.plot_choropleth(shp_link, values, typ, title=typ)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# To-Do list\n",
|
||||||
|
"\n",
|
||||||
|
"General concepts and specific ideas to implement over time, with enough description so they can be brought to life.\n",
|
||||||
|
"\n",
|
||||||
|
"* Support for points in medium and higher layer\n",
|
||||||
|
"* LISA cluster maps"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Caution note on plotting points\n",
|
||||||
|
"\n",
|
||||||
|
"Support for points (dots) is still not quite polished. Ideally, one would like to create a `PathCollection` from scratch so it is analogue to the creation of a `PolyCollection` or `LineCollection`. However, for the time being, we are relying on the wrapper `plt.scatter`, which makes it harder to extract the collection and plug it in a different figure. For that reason, it is recommended that, for the time being, one creates the line and/or polygon map as shown in this notebook and then grabs the output axis and uses `ax.scatter` to overlay the points.\n",
|
||||||
|
"\n",
|
||||||
|
"**NOTE**: the `PathCollection` created by `plt.scatter` is detailed on line 3142 of [`_axes.py`](https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py). Maybe we can take some inspiration from there to create our own `PathCollection` for points so they live at the same level as polygons."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,951 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"#import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Regular Lattice Weights "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w = ps.lat2W()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.neighbors"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.weights"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"$s_0 = \\sum_i \\sum_j w_{i,j}$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.s0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"np.sum( [ c[0]*c[1] for c in w.histogram ] )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.pct_nonzero"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"$pctnonzero = s_0 / n^2$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.s0 / w.n**2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Queen Contiguity"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wq = ps.lat2W(rook=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wq.s0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wq.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"np.sum( [ c[0] * c[1] for c in wq.histogram ] )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wq.pct_nonzero"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"144. / wq.n**2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Bishop"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wb = ps.w_difference(wq,w, constrained = False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wb.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wb.s0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wb[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wq[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ps.w_difference?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wb.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Spatial Lag"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"$l_i = \\sum_{j} w_{i,j} y_j$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.arange(w.n)\n",
|
||||||
|
"y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wy = ps.lag_spatial(w,y)\n",
|
||||||
|
"wy"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y[[1,5]]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y *= 10"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wy = ps.lag_spatial(w,y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wy"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y[[1,5]]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y[[1,5]].sum()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.transform = 'R'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"${w}_{i,j}^* = w_{i,j} / \\sum_j w_{i,j}$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wry = ps.lag_spatial(w,y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wry"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w[24]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y[[19,23]].mean()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Weights From External Files"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## shapefiles"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ls data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wsouth = ps.rook_from_shapefile(\"data/south.shp\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wsouth.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wsouth.pct_nonzero"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wsq = ps.queen_from_shapefile(\"data/south.shp\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wsq.pct_nonzero"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wsouth.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wsq.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wh = np.array(wsouth.histogram)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.bar(wh[:,0],wh[:,1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"whq = np.array(wsq.histogram)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.bar(whq[:,0],whq[:,1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Distance Based Weights"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x,y = np.indices((5,5))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x.shape = (25,1)\n",
|
||||||
|
"y.shape = (25,1)\n",
|
||||||
|
"data = np.hstack([x,y])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### knn weights"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wknn3 = ps.knnW_from_array(data, k = 4)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wknn3[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wknn3.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Distance Bands"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wdb = ps.threshold_binaryW_from_array(data,2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wdb[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wdb.histogram"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wdb.cardinalities"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Writing to external files"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w = ps.rook_from_shapefile('data/NAT.shp')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"gal = ps.open('data/nat_copy.gal', 'w')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"gal.write(w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"gal.close()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"gal = ps.open('data/nat_copy.gal', 'r')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w1 = gal.read()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w1.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.pct_nonzero == w1.pct_nonzero"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,381 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import HTML\n",
|
||||||
|
"\n",
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import folium\n",
|
||||||
|
"import random as RD\n",
|
||||||
|
"\n",
|
||||||
|
"from pysal.contrib.viz import mapping as maps\n",
|
||||||
|
"from matplotlib.collections import LineCollection\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"shp = ps.open(ps.examples.get_path(\"taz.shp\"))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dbf = ps.open(ps.examples.get_path(\"taz.dbf\"))\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dbf.header"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fig = plt.figure(figsize=(9,9))\n",
|
||||||
|
"base = maps.map_poly_shp(shp)\n",
|
||||||
|
"base.set_linewidth(0.75)\n",
|
||||||
|
"base.set_facecolor('none')\n",
|
||||||
|
"base.set_edgecolor('0.8')\n",
|
||||||
|
"ax = maps.setup_ax([base])\n",
|
||||||
|
"fig.add_axes(ax)\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## County as unique values"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cnty = np.array(dbf.by_col(\"CNTY\"))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"fig = plt.figure(figsize=(9,9))\n",
|
||||||
|
"base = maps.map_poly_shp(shp)\n",
|
||||||
|
"base.set_linewidth(0.75)\n",
|
||||||
|
"base.set_facecolor('none')\n",
|
||||||
|
"base.set_edgecolor('0.8')\n",
|
||||||
|
"counties = maps.base_choropleth_unique(maps.map_poly_shp(shp), cnty)\n",
|
||||||
|
"counties.set_linewidth(0)\n",
|
||||||
|
"counties.set_alpha(.5)\n",
|
||||||
|
"ax = maps.setup_ax([base, counties])\n",
|
||||||
|
"fig.add_axes(ax)\n",
|
||||||
|
"plt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cents = np.array([poly.centroid for poly in shp])\n",
|
||||||
|
"cents[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wrook = ps.rook_from_shapefile(ps.examples.get_path(\"taz.shp\"))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w = wrook\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cents.min(axis=0)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def w2line_graph(w, centroids):\n",
|
||||||
|
" \n",
|
||||||
|
" segments = []\n",
|
||||||
|
" for i in w.id2i:\n",
|
||||||
|
" origin = cents[i]\n",
|
||||||
|
" for j in w.neighbors[i]:\n",
|
||||||
|
" dest = cents[j]\n",
|
||||||
|
" ij = [i,j]\n",
|
||||||
|
" ij.sort()\n",
|
||||||
|
" segments.append([origin, dest])\n",
|
||||||
|
"\n",
|
||||||
|
" return segments \n",
|
||||||
|
"\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"segs = w2line_graph(wrook, cents)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## FIXME\n",
|
||||||
|
"if False:\n",
|
||||||
|
" fig = plt.figure(figsize=(9,9))\n",
|
||||||
|
" base = maps.map_poly_shp(shp)\n",
|
||||||
|
" base.set_linewidth(0.75)\n",
|
||||||
|
" base.set_facecolor('none')\n",
|
||||||
|
" base.set_edgecolor('0.8')\n",
|
||||||
|
" segs2 = LineCollection(segs)\n",
|
||||||
|
" maps._add_axes2col(segs2, shp.bbox)\n",
|
||||||
|
" segs2.set_linewidth(0.20)\n",
|
||||||
|
" ax = maps.setup_ax([base, segs])\n",
|
||||||
|
" fig.add_axes(ax)\n",
|
||||||
|
" plt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Intersection weights"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wb = ps.regime_weights(np.array(dbf.by_col(\"CNTY\")))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wb.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wint = ps.weights.Wsets.w_intersection(wb, wrook)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"segs = w2line_graph(wint, cents)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## FIXME\n",
|
||||||
|
"if False:\n",
|
||||||
|
" fig = plt.figure(figsize=(9,9))\n",
|
||||||
|
" base = maps.map_poly_shp(shp)\n",
|
||||||
|
" base.set_linewidth(0.75)\n",
|
||||||
|
" base.set_facecolor('none')\n",
|
||||||
|
" base.set_edgecolor('0.8')\n",
|
||||||
|
" segs = LineCollection(segs)\n",
|
||||||
|
" maps._add_axes2col(segs, shp.bbox)\n",
|
||||||
|
" segs.set_linewidth(0.20)\n",
|
||||||
|
" ax = maps.setup_ax([base, segs])\n",
|
||||||
|
" fig.add_axes(ax)\n",
|
||||||
|
" plt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"segments = w2line_graph(wint, cents)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fig = plt.figure(figsize=(9,9))\n",
|
||||||
|
"base = maps.map_poly_shp(shp)\n",
|
||||||
|
"base.set_linewidth(0.75)\n",
|
||||||
|
"base.set_facecolor('none')\n",
|
||||||
|
"base.set_edgecolor('0.8')\n",
|
||||||
|
"counties = maps.base_choropleth_unique(maps.map_poly_shp(shp), cnty)\n",
|
||||||
|
"counties.set_linewidth(0)\n",
|
||||||
|
"counties.set_alpha(.5)\n",
|
||||||
|
"segs = LineCollection(segments)\n",
|
||||||
|
"maps._add_axes2col(segs, shp.bbox)\n",
|
||||||
|
"segs.set_linewidth(0.20)\n",
|
||||||
|
"segs.set_color('0.1')\n",
|
||||||
|
"ax = maps.setup_ax([base, counties, segs])\n",
|
||||||
|
"fig.add_axes(ax)\n",
|
||||||
|
"plt.show()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,769 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.arange(25)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y.shape = (5,5)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.matshow(y,cmap=plt.cm.gray_r)\n",
|
||||||
|
"plt.colorbar()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.zeros((5,5))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Perfect negative autocorrelation"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.zeros((25,))\n",
|
||||||
|
"ids = range(0,25,2)\n",
|
||||||
|
"y[ids] = 1\n",
|
||||||
|
"y.shape = (5,5)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.matshow(y, cmap=plt.cm.gray)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y.shape = (25,)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Join Counts"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"w = ps.lat2W(5,5)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"$bb_i = \\sum_j y_i w_{i,j} y_j = y_i \\sum_j w_{i,j} y_j$ "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"bbi = y * ps.lag_spatial(w,y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"bbi"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"$ww_i = \\sum_j (1-y_i) w_{i,j} (1-y_j) = (1-y_i) \\sum_j w_{i,j} (1-y_j)$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"yc = 1 - y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wwi = yc * ps.lag_spatial(w,yc)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wwi"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"$bw_i = \\sum_{j} w_{i,j} - bb_i - ww_i$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"$2 bw = \\sum_i bw_i = \\sum_i ( \\sum_j w_{i,j} - bb_i - ww_i ) = S_0 - bb - ww$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.s0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"bw = (w.s0 - bbi.sum() - wwi.sum())/2."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"bw"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc = ps.Join_Counts(y, w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc.bw"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc.p_sim_bb"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc.p_sim_bw"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from scipy.stats import gaussian_kde"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"density = gaussian_kde(jc.sim_bw)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"xs = np.linspace(0,50,200)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(xs, density(xs))\n",
|
||||||
|
"plt.axvline(x=jc.bw, color='r')\n",
|
||||||
|
"plt.xlabel('bw')\n",
|
||||||
|
"plt.ylabel('f(bw)')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"density = gaussian_kde(jc.sim_bb)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(xs, density(xs))\n",
|
||||||
|
"plt.axvline(x=jc.bb, color='r')\n",
|
||||||
|
"plt.xlabel('bb')\n",
|
||||||
|
"plt.ylabel('f(bb)')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc.bb"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.s0/2\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Random pattern"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from scipy.stats import bernoulli"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = bernoulli.rvs(0.5, size=25)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc_random = ps.Join_Counts(y,w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y.shape = (5,5)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.matshow(y,cmap=plt.cm.gray_r)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc_random.p_sim_bb"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"density = gaussian_kde(jc_random.sim_bb)\n",
|
||||||
|
"xs = np.linspace(0,30,200)\n",
|
||||||
|
"plt.plot(xs, density(xs))\n",
|
||||||
|
"plt.axvline(x=jc_random.bb, color='r')\n",
|
||||||
|
"plt.xlabel('bb')\n",
|
||||||
|
"plt.ylabel('f(bb)')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc_random.p_sim_bw"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc_random.ww"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc_random.bw"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"jc_random.bb"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Continuous Variable"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.arange(w.n)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"yc = y.copy()\n",
|
||||||
|
"yc.shape = (5,5)\n",
|
||||||
|
"plt.matshow(yc,cmap=plt.cm.gray_r)\n",
|
||||||
|
"plt.colorbar()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"##Moran's I\n",
|
||||||
|
"\n",
|
||||||
|
"$$I = \\frac{n}{S_0} \\frac{\\sum_i \\sum_j z_i w_{i,j} z_j}{\\sum_i z_iz_i}$$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"mi = ps.Moran(y,w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"mi.I"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"mi.EI"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"mi.p_norm"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"mi.p_sim"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"mi.sim"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"density = gaussian_kde(mi.sim)\n",
|
||||||
|
"xs = np.linspace(mi.sim.min(),mi.sim.max(),200)\n",
|
||||||
|
"plt.plot(xs, density(xs))\n",
|
||||||
|
"plt.axvline(x=mi.I, color='r')\n",
|
||||||
|
"plt.xlabel('I')\n",
|
||||||
|
"plt.ylabel('f(I)')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Moran Scatter Plot"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.transform = \"R\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wy = ps.lag_spatial(w, y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.scatter(y,wy)\n",
|
||||||
|
"plt.vlines(y.mean(),wy.min(),wy.max())\n",
|
||||||
|
"plt.hlines(wy.mean(),y.min(),y.max())\n",
|
||||||
|
"plt.xlabel('y')\n",
|
||||||
|
"plt.ylabel('wy')\n",
|
||||||
|
"plt.title('Moran Scatter Plot')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Getis Ord $G$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"g = ps.G(y,w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"g.p_sim"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ps.G?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,182 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"from scipy.linalg import inv"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def draw_map(lamb):\n",
|
||||||
|
" s = 20\n",
|
||||||
|
" n = s**2\n",
|
||||||
|
" w = ps.lat2W(s, s, rook=False)\n",
|
||||||
|
" w.transform = 'R'\n",
|
||||||
|
" e = np.random.random((n, 1))\n",
|
||||||
|
" u = inv(np.eye(n) - lamb * w.full()[0])\n",
|
||||||
|
" u = np.dot(u, e)\n",
|
||||||
|
" ul = ps.lag_spatial(w, u)\n",
|
||||||
|
" u = (u - u.mean()) / np.std(u)\n",
|
||||||
|
" ul = (ul - ul.mean()) / np.std(ul)\n",
|
||||||
|
" gu = u.reshape((s, s))\n",
|
||||||
|
" # Figure\n",
|
||||||
|
" f = plt.figure(figsize=(9, 4))\n",
|
||||||
|
" ax1 = f.add_subplot(121)\n",
|
||||||
|
" ax1.matshow(gu, cmap=plt.cm.YlGn)\n",
|
||||||
|
" ax1.set_frame_on(False)\n",
|
||||||
|
" ax1.axes.get_xaxis().set_visible(False)\n",
|
||||||
|
" ax1.axes.get_yaxis().set_visible(False)\n",
|
||||||
|
" #---\n",
|
||||||
|
" ax2 = f.add_subplot(122)\n",
|
||||||
|
" sc = ax2.scatter(u, ul, linewidth=0)\n",
|
||||||
|
" ols = ps.spreg.OLS(ul, u)\n",
|
||||||
|
" tag = \"b = %.3f\"%ols.betas[1][0]\n",
|
||||||
|
" ax2.plot(u, ols.predy, c='red', label=tag)\n",
|
||||||
|
" ax2.axvline(0, c='0.5')\n",
|
||||||
|
" ax2.axhline(0, c='0.5')\n",
|
||||||
|
" ax2.legend()\n",
|
||||||
|
" plt.xlabel('u')\n",
|
||||||
|
" plt.ylabel('Wu')\n",
|
||||||
|
" plt.suptitle(\"$\\lambda$ = %.2f\"%lamb)\n",
|
||||||
|
" plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Interactive spatial autocorrelation"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This notebook illustrates the concept of spatial autocorrelation using the new interactivity in IPython. The data generating process (DGP) considered here is the following:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"1. $u = \\lambda Wu + \\epsilon$\n",
|
||||||
|
"\n",
|
||||||
|
"1. $u - \\lambda Wu = \\epsilon$\n",
|
||||||
|
"\n",
|
||||||
|
"1. $u (I - \\lambda W) = \\epsilon$\n",
|
||||||
|
"\n",
|
||||||
|
"1. $u = (I - \\lambda W)^{-1} \\epsilon$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Where `u` is a vector spatially autocorrelated, `W` is a spatial weights matrix as you could created with `PySAL`, and $\\epsilon$ is an i.i.d. random vector."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"To implement the previous DGP, the simple method `draw_map` (actual code pasted at the bottom of the notebook, so make sure to run that cell beforehand) creates a random vector with degree of spatial autocorrelation $\\lambda$ and allocates it to a lattice geography, where every pixel is assumed to be an area with a value. Right next to it, the function also displays the Moran's scatter plot. Both map and plot depend on the $\\lambda$ parameter that controls the degree of spatial autocorrelation.\n",
|
||||||
|
"\n",
|
||||||
|
"Here's a static version of the function:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"draw_map(0.95)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now, we can make the $\\lambda$ value change in an interactive way:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from ipywidgets import interact\n",
|
||||||
|
"interact(draw_map, lamb=(-0.9, 0.9))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Actual plotting function"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,977 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from IPython.display import HTML\n",
|
||||||
|
"\n",
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import folium\n",
|
||||||
|
"import random as RD\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"import folium\n",
|
||||||
|
"def inline_map(map):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds the HTML source of the map directly into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" This method will not work if the map depends on any files (json data). Also this uses\n",
|
||||||
|
" the HTML5 srcdoc attribute, which may not be supported in all browsers.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map._build_map()\n",
|
||||||
|
" return HTML('<iframe srcdoc=\"{srcdoc}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(srcdoc=map.HTML.replace('\"', '"')))\n",
|
||||||
|
"\n",
|
||||||
|
"def embed_map(map, path=\"map.html\"):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Embeds a linked iframe to the map into the IPython notebook.\n",
|
||||||
|
" \n",
|
||||||
|
" Note: this method will not capture the source of the map into the notebook.\n",
|
||||||
|
" This method should work for all maps (as long as they use relative urls).\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" map.create_map(path=path)\n",
|
||||||
|
" return HTML('<iframe src=\"files/{path}\" style=\"width: 100%; height: 510px; border: none\"></iframe>'.format(path=path))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Base layer for south"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import folium\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5)\n",
|
||||||
|
"map_osm.geo_json(geo_path = 'data/south.json')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Binding Attribute Data to the Map"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import json\n",
|
||||||
|
"\n",
|
||||||
|
"f = open(r'data/south.json')\n",
|
||||||
|
"q = json.load(f)\n",
|
||||||
|
"f.close()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"q.keys()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"features = q['features']\n",
|
||||||
|
"len(features)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"indices = []\n",
|
||||||
|
"values = []\n",
|
||||||
|
"for feature in features:\n",
|
||||||
|
" indices.append(str(feature['properties']['FIPS']))\n",
|
||||||
|
" values.append(feature['properties']['HR80'])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df = pd.DataFrame({'HR80': values,\n",
|
||||||
|
" 'FIPS': indices} )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.array(df.HR80.tolist())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"w = ps.queen_from_shapefile('data/south.shp')\n",
|
||||||
|
"w.transform = 'r'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = np.array(df['HR80'].tolist())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Local Moran\n",
|
||||||
|
"$li_i = \\frac{n-1}{\\sum_j z_j^2}z_i \\sum_j w_{i,j} z_j$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"li_hr80 = ps.Moran_Local(y,w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"li_hr80.p_sim.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sig01 = 1.* (li_hr80.p_sim<=0.01)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sig01.sum()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sig05 = 1.* (li_hr80.p_sim<=0.05)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sig05.sum()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df['sig01'] = sig01\n",
|
||||||
|
"df['sig05'] = sig05"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','HR80'],\n",
|
||||||
|
" fill_color='YlGnBu', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" legend_name='Homicide Rate HR80')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','sig01'],\n",
|
||||||
|
" fill_color='YlOrRd', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" threshold_scale=[0,1],\n",
|
||||||
|
" legend_name='Homicide Rate HR80 - Significant LISA (0.01)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "raw",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Note that folium only is bound to threshold scales - no discrete or ordinal scale yet, so we have to fake it with two class threshold scales."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','sig01'],\n",
|
||||||
|
" fill_color='YlOrRd', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" legend_name='Homicide Rate HR80 - Significant LISA (0.01)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','sig05'],\n",
|
||||||
|
" fill_color='YlOrRd', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" legend_name='Homicide Rate HR80 - Significant LISA (0.05)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"li_hr80.q"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cold_spots = 1. * (li_hr80.q==3) * (li_hr80.p_sim < 0.01)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cold_spots.sum()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Cold spots in pandas'y way\n",
|
||||||
|
"lisa_df = pd.DataFrame({'q': li_hr80.q, 'p_sim': li_hr80.p_sim})\n",
|
||||||
|
"cold = lisa_df[(lisa_df['q']==3) & (lisa_df['p_sim']<0.01)]\n",
|
||||||
|
"print cold.info()\n",
|
||||||
|
"print '\\n~~~~~~~~~~\\n'\n",
|
||||||
|
"# Hot spots in pandas'y way\n",
|
||||||
|
"hot = lisa_df[(lisa_df['q']==1) & (lisa_df['p_sim']<0.01)]\n",
|
||||||
|
"print hot.info()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot_spots = 1. * (li_hr80.q==1) * (li_hr80.p_sim < 0.01)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot_spots.sum()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df['hot_01'] = hot_spots\n",
|
||||||
|
"df['cold_01'] = cold_spots"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df.hot_01"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df.cold_01"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','hot_01'],\n",
|
||||||
|
" fill_color='YlOrRd', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" threshold_scale=[0,1],\n",
|
||||||
|
" legend_name='Homicide Rate HR80 - Hot Spots (0.01)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"map_osm = folium.Map(location=[33.7550, -87.3900], zoom_start=5,)\n",
|
||||||
|
"map_osm.geo_json(geo_path='data/south.json',\n",
|
||||||
|
" key_on='feature.properties.FIPS',\n",
|
||||||
|
" data_out='data.json', data=df,\n",
|
||||||
|
" columns=['FIPS','cold_01'],\n",
|
||||||
|
" fill_color='YlGnBu', fill_opacity=0.7,\n",
|
||||||
|
" line_opacity=0.2,\n",
|
||||||
|
" threshold_scale=[0,1],\n",
|
||||||
|
" legend_name='Homicide Rate HR80 - Cold Spots (0.01)')\n",
|
||||||
|
"embed_map(map_osm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dbf = ps.open('data/south.dbf')\n",
|
||||||
|
"dbf.header"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
" df['STATE_NAME'] = dbf.by_col('STATE_NAME')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df['County'] = dbf.by_col('NAME')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot_01 = df['hot_01']"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot = df[hot_01>0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df[['STATE_NAME', 'County']]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df.head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df.tail()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df[[0,2,4]]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df[df.hot_01 > 0 ].all()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df[0:10]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot = df[df['hot_01']>0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df[df['hot_01']>0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df[df['cold_01']>0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print df[df['cold_01']>0].to_string() "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hot_01"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w = ps.queen_from_shapefile('data/south.shp')\n",
|
||||||
|
"w.transform = 'r'\n",
|
||||||
|
"wy = ps.lag_spatial(w,y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.scatter(y,wy, alpha=0.5, lw=0)\n",
|
||||||
|
"plt.vlines(y.mean(),wy.min(),wy.max())\n",
|
||||||
|
"plt.hlines(wy.mean(),y.min(),y.max())\n",
|
||||||
|
"plt.xlabel('y')\n",
|
||||||
|
"plt.ylabel('wy')\n",
|
||||||
|
"lm = ps.spreg.OLS(wy[:, None], y[:, None])\n",
|
||||||
|
"plt.plot(y, lm.predy, color='red')\n",
|
||||||
|
"plt.title('Moran Scatter Plot')\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"c = 2*hot_01 + df.cold_01\n",
|
||||||
|
"plt.scatter(y,wy, c=c, s=(sig01+1)*50)\n",
|
||||||
|
"plt.vlines(y.mean(),wy.min(),wy.max())\n",
|
||||||
|
"plt.hlines(wy.mean(),y.min(),y.max())\n",
|
||||||
|
"plt.xlabel('y')\n",
|
||||||
|
"plt.ylabel('wy')\n",
|
||||||
|
"plt.title('Moran Scatter Plot')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sig01.sum()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df['wHR80'] = wy"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print df[df['cold_01']>0].to_string() "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print df[df['hot_01']>0].to_string() "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"lg = ps.G_Local(y, w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"lg.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sig = lg.p_sim < 0.01"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"lg.p_sim[sig]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"lgs = ps.G_Local(y,w, star=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"lgs.p_sim[lgs.p_sim < 0.01]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sum(sig)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"sum(lgs.p_sim < 0.01)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,642 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Conway's Game of Life with PySAL"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Rules ##\n",
|
||||||
|
"\n",
|
||||||
|
" 1. A cell that is currently alive and that has two **or** three live neighbors stays alive\n",
|
||||||
|
" 2. A cell that is currently dead with **exactly** three live neighbors comes alive \n",
|
||||||
|
" 3. All other cells remain dead, or die due to loneliness (less than 2 neighbors) or overcrowding (more than 3 neighbors)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pysal as ps\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"from scipy.stats import bernoulli\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"k = 8 # dimension of lattice"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w = ps.lat2W(k,k,rook=False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.neighbors[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.neighbors[45]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"w.weights[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = bernoulli.rvs(0.45,size=w.n)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wy = ps.lag_spatial(w,y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"wy"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Rules ##\n",
|
||||||
|
"\n",
|
||||||
|
" 1. A cell that is currently alive and that has two **or** three live neighbors stays alive\n",
|
||||||
|
" 2. A cell that is currently dead with **exactly** three live neighbors comes alive \n",
|
||||||
|
" 3. All other cells remain dead, or die due to loneliness (less than 2 neighbors) or overcrowding (more than 3 neighbors)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Rule 1: find live cells and count their neighbors"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ywy = y*wy"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"lw23 = np.nonzero( (ywy==2) + (ywy==3) )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"lw23"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Rule 2: find dead cells with exactly 3 neighbors"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dw3 = (1-y) * wy"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"np.nonzero(dw3==3)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Rules 1 and 2 give us the surviving cells"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"live_next = np.nonzero( (ywy==2) + (ywy==3) + (dw3==3) )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"live_next"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y[live_next]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"So we see that in the future, some dead cells will becoming alive. But what about live cells now that die in the next period?\n",
|
||||||
|
"\n",
|
||||||
|
"We know that they will be dead next period. Allocate an array with zeros for the next period and assign the live cells. Everyone else is dead."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y1 = np.zeros_like(y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y1[live_next] = 1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "raw",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"We would then iterate these steps for future generations. \n",
|
||||||
|
"\n",
|
||||||
|
"Let's place the process inside a function for reuse later"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def generation(y,w):\n",
|
||||||
|
" y1 = np.zeros_like(y)\n",
|
||||||
|
" wy = ps.lag_spatial(w,y)\n",
|
||||||
|
" ywy = y * wy\n",
|
||||||
|
" live_next = np.nonzero( ( ywy == 2 ) + ( ywy == 3 ) + ( ( 1-y ) * wy == 3 ) )\n",
|
||||||
|
" y1[live_next] = 1\n",
|
||||||
|
" return y1\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "raw",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Try this out on some fresh data."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y = bernoulli.rvs(0.45,size=w.n)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y1 = generation(y,w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y2 = generation(y1,w)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"One interesting initial pattern is the so called R-pentomino.\n",
|
||||||
|
"\n",
|
||||||
|
"We will create one and then run a simulation to see how the solutions evolve."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ngen=350\n",
|
||||||
|
"k = 50\n",
|
||||||
|
"w = ps.lat2W(k, k, rook=False)\n",
|
||||||
|
"#y = bernoulli.rvs(0.45,size=w.n)\n",
|
||||||
|
"y = np.zeros((w.n,))\n",
|
||||||
|
"#R-pentomino pattern\n",
|
||||||
|
"kd2 = k/2\n",
|
||||||
|
"top = kd2 + k * ( kd2 - 1 )\n",
|
||||||
|
"topr = top + 1\n",
|
||||||
|
"midl = top + k -1\n",
|
||||||
|
"mid = midl + 1\n",
|
||||||
|
"bot = mid + k\n",
|
||||||
|
"y[[top, topr, midl, mid, bot]] = 1\n",
|
||||||
|
"results = {}\n",
|
||||||
|
"for i in xrange(ngen):\n",
|
||||||
|
" y1 = generation(y,w)\n",
|
||||||
|
" results[i] = y1\n",
|
||||||
|
" if np.all(y == y1):\n",
|
||||||
|
" break\n",
|
||||||
|
" print i, y.sum(), y1.sum()\n",
|
||||||
|
" y = y1\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "raw",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Create some graphs to look at both the the individual maps from every 10th generation and then plot the aggregate population size by generation."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"generations = np.zeros((ngen,))\n",
|
||||||
|
"living = np.zeros_like(generations)\n",
|
||||||
|
"keys = results.keys()\n",
|
||||||
|
"keys.sort()\n",
|
||||||
|
"for i in keys:\n",
|
||||||
|
" generations[i] = i\n",
|
||||||
|
" living[i] = results[i].sum()\n",
|
||||||
|
" if not i%10:\n",
|
||||||
|
" ymat = results[i]\n",
|
||||||
|
" ymat.shape = (50,50)\n",
|
||||||
|
" plt.imshow(ymat,cmap='Greys', interpolation='nearest')\n",
|
||||||
|
" plt.title(\"Generation %d\"%i)\n",
|
||||||
|
" plt.show()\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"generations.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.plot(generations,living)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ymat = results[ngen-1]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ymat.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ymat.shape=(50,50)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.imshow(ymat, cmap='Greys', interpolation='nearest')\n",
|
||||||
|
"plt.title(\"Last Generation\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ymat = results[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ymat.shape = (50,50)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plt.imshow(ymat, cmap='Greys', interpolation='nearest')\n",
|
||||||
|
"plt.title('First Generation: R-pentomino')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ymat = results[ngen-2]\n",
|
||||||
|
"ymat.shape=(50,50)\n",
|
||||||
|
"plt.imshow(ymat, cmap='Greys', interpolation='nearest')\n",
|
||||||
|
"plt.title(\"Penultimate Generation\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ymat = results[ngen-1]\n",
|
||||||
|
"ymat.shape=(50,50)\n",
|
||||||
|
"plt.imshow(ymat, cmap='Greys', interpolation='nearest')\n",
|
||||||
|
"plt.title(\"Final Generation\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
State,pcgdp1940,pcgdp1950,pcgdp1960,pcgdp1970,pcgdp1980,pcgdp1990,pcgdp2000,hanson03,hanson98,esquivel99,inegi,inegi2
|
||||||
|
Aguascalientes,10384.000,6234.000,8714.000,16078.000,21022.000,20787.000,27782.000,2.000,2.000,3.000,4.000,4.000
|
||||||
|
Baja California,22361.000,20977.000,17865.000,25321.000,29283.000,26839.000,29855.000,1.000,1.000,5.000,1.000,1.000
|
||||||
|
Baja California Sur,9573.000,16013.000,16707.000,24384.000,29038.000,25842.000,26103.000,2.000,2.000,6.000,1.000,1.000
|
||||||
|
Campeche,3758.000,4929.000,5925.000,10274.000,12166.000,51123.000,36163.000,6.000,5.000,4.000,5.000,5.000
|
||||||
|
Chiapas,2934.000,4138.000,5280.000,7015.000,16200.000,8637.000,8684.000,5.000,5.000,7.000,5.000,5.000
|
||||||
|
Chihuahua,8578.000,13997.000,16265.000,19178.000,23399.000,25332.000,30735.000,1.000,1.000,5.000,1.000,2.000
|
||||||
|
Coahuila,8537.000,9673.000,12318.000,20562.000,25688.000,26084.000,28460.000,1.000,1.000,5.000,2.000,2.000
|
||||||
|
Colima,6909.000,6049.000,6036.000,12551.000,17427.000,18313.000,21358.000,3.000,3.000,6.000,4.000,4.000
|
||||||
|
Distrito Federal,17816.000,17119.000,23174.000,32386.000,42028.000,43810.000,54349.000,4.000,4.000,1.000,3.000,3.000
|
||||||
|
Durango,12132.000,8859.000,9323.000,12700.000,16726.000,17353.000,17379.000,2.000,2.000,3.000,1.000,2.000
|
||||||
|
Guanajuato,4359.000,5686.000,8209.000,11635.000,13864.000,13607.000,15585.000,3.000,3.000,3.000,4.000,4.000
|
||||||
|
Guerrero,2181.000,3629.000,4991.000,6497.000,8727.000,9084.000,11820.000,5.000,5.000,7.000,5.000,5.000
|
||||||
|
Hidalgo,4414.000,5194.000,6399.000,7767.000,12391.000,13091.000,12348.000,3.000,3.000,2.000,3.000,3.000
|
||||||
|
Jalisco,5309.000,8232.000,9953.000,16288.000,20659.000,20133.000,21610.000,3.000,3.000,6.000,4.000,4.000
|
||||||
|
Mexico,3408.000,4972.000,9053.000,17164.000,20165.000,18547.000,16322.000,4.000,4.000,1.000,3.000,3.000
|
||||||
|
Michoacan,3327.000,5272.000,5244.000,8109.000,11206.000,10980.000,11838.000,3.000,3.000,7.000,4.000,4.000
|
||||||
|
Morelos,6936.000,8962.000,10499.000,13892.000,16513.000,17701.000,18170.000,3.000,3.000,2.000,3.000,3.000
|
||||||
|
Nayarit,4836.000,7515.000,7621.000,10880.000,13354.000,12757.000,11478.000,2.000,2.000,6.000,4.000,4.000
|
||||||
|
Nuevo Leon,9073.000,11490.000,20117.000,28206.000,34856.000,34726.000,38672.000,1.000,1.000,5.000,2.000,2.000
|
||||||
|
Oaxaca,1892.000,4538.000,4140.000,5230.000,7730.000,8465.000,9010.000,5.000,5.000,7.000,5.000,5.000
|
||||||
|
Puebla,3569.000,6415.000,6542.000,9775.000,13374.000,11895.000,15685.000,3.000,3.000,2.000,3.000,5.000
|
||||||
|
Quertaro,11016.000,5560.000,7110.000,14073.000,20088.000,22441.000,26149.000,3.000,3.000,3.000,3.000,4.000
|
||||||
|
Quintana Roo,21965.000,28747.000,9677.000,17046.000,26695.000,25049.000,33442.000,6.000,5.000,4.000,5.000,5.000
|
||||||
|
San Luis Potosi,4372.000,7533.000,6440.000,9721.000,12691.000,15436.000,15866.000,2.000,2.000,3.000,4.000,4.000
|
||||||
|
Sinaloa,4840.000,6663.000,9613.000,14477.000,15312.000,15823.000,15242.000,2.000,2.000,6.000,1.000,1.000
|
||||||
|
Sonora,6399.000,10345.000,12134.000,22662.000,23181.000,24784.000,24068.000,1.000,1.000,5.000,1.000,1.000
|
||||||
|
Tabasco,2459.000,3857.000,6494.000,9367.000,42361.000,16055.000,13360.000,6.000,5.000,4.000,5.000,5.000
|
||||||
|
Tamaulipas,7508.000,8536.000,8383.000,17128.000,21937.000,19983.000,23546.000,1.000,1.000,5.000,2.000,2.000
|
||||||
|
Tlaxcala,3605.000,4178.000,4357.000,6245.000,9882.000,10339.000,11701.000,3.000,3.000,2.000,3.000,3.000
|
||||||
|
Veracruz,5203.000,10143.000,11404.000,12240.000,14252.000,13796.000,12191.000,3.000,3.000,4.000,5.000,5.000
|
||||||
|
Yucatan,7990.000,8428.000,10067.000,11665.000,15239.000,13979.000,17509.000,6.000,5.000,4.000,5.000,5.000
|
||||||
|
Zacatecas,3734.000,6435.000,5821.000,7426.000,8876.000,11656.000,11130.000,2.000,2.000,3.000,4.000,4.000
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
"Name","STATE_FIPS",1929,1930,1931,1932,1933,1934,1935,1936,1937,1938,1939,1940,1941,1942,1943,1944,1945,1946,1947,1948,1949,1950,1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009
|
||||||
|
"Alabama",1,323,267,224,162,166,211,217,251,267,244,252,281,375,518,658,738,784,754,805,881,833,909,1045,1106,1161,1139,1273,1356,1421,1468,1526,1558,1587,1667,1758,1890,2030,2169,2294,2516,2748,2979,3225,3544,3960,4351,4765,5323,5817,6500,7199,7892,8712,9185,9783,10800,11583,12202,12912,13842,14899,15832,16536,17462,17991,18860,19683,20329,21129,22123,22987,23471,24467,25161,26065,27665,29097,30634,31988,32819,32274
|
||||||
|
"Arizona",4,600,520,429,321,308,362,416,462,504,478,490,505,638,917,1004,1050,1124,1117,1186,1324,1305,1367,1623,1716,1716,1696,1752,1850,1893,1885,1974,2059,2103,2167,2204,2310,2412,2587,2754,3092,3489,3843,4145,4487,4929,5329,5528,6074,6642,7586,8604,9590,10658,10945,11654,12885,13808,14463,15130,15795,16568,17211,17563,18131,18756,19774,20634,21611,22781,24133,25189,25578,26232,26469,27106,28753,30671,32552,33470,33445,32077
|
||||||
|
"Arkansas",5,310,228,215,157,157,187,207,247,256,231,249,260,342,481,559,687,740,756,741,886,813,847,957,1027,1066,1074,1176,1230,1244,1314,1412,1405,1511,1574,1656,1777,1886,2094,2219,2409,2619,2849,3096,3415,3985,4376,4655,5155,5670,6509,7088,7586,8564,8952,9476,10560,11264,11734,12184,13016,13813,14509,15255,16425,16995,17750,18546,19442,20229,21260,22244,22257,23532,23929,25074,26465,27512,29041,31070,31800,31493
|
||||||
|
"California",6,991,887,749,580,546,603,660,771,795,771,781,844,1013,1286,1549,1583,1583,1671,1693,1763,1744,1877,2080,2207,2249,2227,2379,2495,2579,2596,2740,2823,2880,3004,3102,3274,3417,3663,3878,4207,4540,4815,5034,5451,5947,6553,7091,7815,8570,9618,10846,12029,13205,13774,14491,15927,16909,17628,18625,19713,20765,21889,22024,22722,22927,23473,24496,25563,26759,28280,29910,32275,32750,32900,33801,35663,37463,40169,41943,42377,40902
|
||||||
|
"Colorado",8,634,578,471,354,353,368,444,542,532,506,516,545,649,895,1039,1065,1189,1211,1359,1454,1430,1521,1796,1880,1813,1773,1869,1960,2104,2157,2261,2340,2417,2471,2539,2638,2800,2982,3142,3381,3686,4055,4413,4791,5310,5864,6321,6895,7567,8539,9596,10809,12141,12945,13570,14751,15416,15772,16417,17285,18548,19703,20487,21447,22526,23498,24865,26231,27950,29860,31546,32949,34228,33963,34092,35543,37388,39662,41165,41719,40093
|
||||||
|
"Connecticut",9,1024,921,801,620,583,653,706,806,860,769,836,918,1145,1418,1593,1599,1565,1580,1702,1720,1663,1891,2158,2298,2393,2350,2477,2684,2810,2731,2832,2926,3042,3175,3250,3401,3583,3874,4195,4443,4847,5090,5300,5697,6241,6813,7239,7885,8712,9720,10971,12439,13865,14903,15799,17580,18763,20038,21895,24007,25797,26736,26863,28635,29602,30532,31947,33472,35596,37452,39300,40640,42279,42021,42398,45009,47022,51133,53930,54528,52736
|
||||||
|
"Delaware",10,1032,857,775,590,564,645,701,868,949,795,899,1027,1164,1291,1464,1504,1526,1565,1664,1685,1805,2075,2155,2244,2341,2306,2507,2749,2645,2651,2724,2805,2815,2933,3049,3210,3468,3610,3785,4079,4421,4608,4892,5303,5871,6347,6729,7349,7913,8658,9549,10803,11873,12727,13529,14816,16056,16781,17933,19312,20930,21636,22342,23094,23823,24530,25391,26640,27405,29571,30778,31255,32664,33463,34123,35998,37297,39358,40251,40698,40135
|
||||||
|
"Florida",12,518,470,398,319,288,348,376,450,487,460,495,522,609,787,1009,1114,1174,1169,1167,1201,1208,1304,1388,1474,1567,1562,1673,1791,1836,1887,2010,2023,2039,2113,2200,2348,2498,2685,2909,3249,3659,4006,4286,4703,5235,5616,5895,6376,7010,7921,8879,10049,11195,11789,12637,13764,14705,15423,16415,17593,19045,19855,20189,20661,21652,22340,23512,24616,25722,26930,27780,28145,28852,29499,30277,32462,34460,36934,37781,37808,36565
|
||||||
|
"Georgia",13,347,307,256,200,204,244,268,302,313,290,309,337,419,567,725,831,879,845,887,988,969,1065,1204,1280,1326,1302,1423,1499,1523,1583,1660,1698,1744,1844,1961,2082,2258,2462,2655,2891,3162,3394,3666,4038,4497,4867,5152,5694,6221,6989,7722,8474,9435,10054,10849,12185,13143,13990,14820,15876,16803,17738,18289,19333,20129,21170,22230,23586,24547,26134,27340,27940,28596,28660,29060,29995,31498,32739,33895,34127,33086
|
||||||
|
"Idaho",16,507,503,374,274,227,403,399,475,423,426,437,463,596,914,1025,1095,1135,1203,1278,1345,1277,1329,1497,1644,1550,1559,1596,1728,1783,1817,1891,1898,1975,2092,2168,2250,2554,2591,2755,2910,3269,3558,3761,4150,4709,5382,5571,6116,6510,7319,7894,8735,9405,9621,10315,11069,11647,11968,12611,13548,14803,15866,16195,17236,18258,18846,19630,20353,20830,21923,22835,24180,25124,25485,25912,27846,29003,30954,32168,32322,30987
|
||||||
|
"Illinois",17,948,807,671,486,437,505,573,650,731,648,704,751,892,1036,1258,1386,1465,1534,1639,1816,1685,1831,2026,2091,2209,2177,2270,2452,2522,2511,2642,2700,2789,2902,2981,3131,3360,3599,3785,4045,4354,4580,4874,5266,5903,6464,6986,7623,8385,9277,10243,11077,12250,12771,13289,14682,15508,16284,17289,18461,19634,20756,21320,22764,23386,24440,25643,27005,28347,29974,31145,32259,32808,33325,34205,35599,36825,39220,41238,42049,40933
|
||||||
|
"Indiana",18,607,514,438,310,294,359,421,481,547,472,518,551,724,913,1138,1198,1251,1204,1313,1456,1361,1524,1711,1778,1943,1805,1907,2008,2042,2015,2131,2209,2246,2399,2486,2617,2856,3041,3153,3401,3714,3810,4105,4455,5100,5440,5830,6516,7199,8006,8814,9449,10355,10698,11203,12445,13143,13821,14664,15616,16770,17625,18055,19269,20112,21153,21845,22775,23748,25182,26143,27011,27590,28059,29089,30126,30768,32305,33151,33978,33174
|
||||||
|
"Iowa",19,581,510,400,297,253,269,425,393,523,458,475,501,609,835,1024,1003,1092,1245,1217,1642,1356,1532,1638,1725,1657,1788,1670,1758,1939,1990,2026,2061,2180,2284,2432,2549,2833,3067,3093,3312,3652,3862,4005,4473,5398,5596,6192,6580,7283,8438,9114,9671,10968,11227,11485,12798,13395,14020,14899,15315,16562,17380,17859,18939,18929,20498,21181,22713,23798,24844,25615,26723,27315,28232,28835,31027,31656,33177,35008,36726,35983
|
||||||
|
"Kansas",20,532,467,401,266,250,287,362,387,428,383,382,425,552,851,1045,1169,1165,1136,1310,1352,1299,1463,1602,1827,1739,1791,1753,1821,1911,2098,2106,2165,2227,2305,2384,2516,2689,2894,3032,3268,3548,3816,4145,4613,5274,5717,6186,6721,7307,8082,9240,10038,11248,11989,12373,13602,14330,14904,15583,16331,17093,18182,18832,19955,20510,21352,21889,23121,24355,25687,26824,27816,28979,29067,30109,31181,32367,34934,36546,37983,37036
|
||||||
|
"Kentucky",21,393,325,291,211,205,233,265,294,341,297,305,320,395,538,701,767,803,826,865,995,938,990,1153,1237,1305,1289,1346,1437,1491,1543,1604,1633,1731,1822,1906,1972,2134,2329,2501,2718,2971,3184,3391,3715,4145,4607,4933,5462,6095,6784,7640,8231,9110,9589,9859,11062,11558,11995,12782,13570,14602,15484,16241,17320,17815,18514,19215,20155,21215,22353,23237,24294,24816,25297,25777,26891,27881,29392,30443,31302,31250
|
||||||
|
"Louisiana",22,414,355,318,241,227,265,290,330,353,348,360,364,449,592,786,876,889,833,885,1019,1074,1117,1210,1278,1343,1339,1397,1502,1616,1635,1685,1690,1741,1806,1909,2003,2139,2331,2527,2749,2901,3106,3328,3593,3994,4510,4956,5557,6135,6951,7813,8833,10037,10558,10865,11628,12121,12028,12266,13113,13997,15223,16076,16968,17717,18779,19541,20254,21209,22352,22847,23334,25116,25683,26434,27776,29785,33438,34986,35730,35151
|
||||||
|
"Maine",23,601,576,491,377,371,416,430,506,510,471,495,526,631,857,1102,1102,1079,1134,1169,1240,1183,1195,1319,1438,1449,1448,1581,1669,1720,1797,1850,1919,1903,1983,2049,2212,2383,2539,2658,2872,3140,3423,3594,3864,4319,4764,5019,5708,6142,6751,7497,8408,9231,9873,10551,11665,12533,13463,14595,15813,16886,17479,17662,18350,18810,19531,20240,21293,22305,23529,24603,25623,27068,27731,28727,30201,30721,32340,33620,34906,35268
|
||||||
|
"Maryland",24,768,712,638,512,466,523,548,618,665,633,663,710,870,1117,1287,1324,1312,1315,1355,1500,1496,1642,1815,1944,2017,1938,2047,2184,2267,2258,2324,2407,2507,2638,2722,2881,3055,3284,3516,3831,4209,4573,4894,5291,5822,6382,6878,7536,8191,9062,10035,11230,12403,13246,14228,15693,16961,17966,19216,20626,22001,23023,23571,24358,25104,26046,26896,27844,29222,30850,32465,33872,35430,36293,37309,39651,41555,43990,45827,47040,47159
|
||||||
|
"Massachusetts",25,906,836,759,613,559,609,643,714,732,672,724,779,901,1073,1262,1299,1348,1396,1435,1512,1480,1656,1817,1895,1947,1930,2071,2194,2296,2321,2430,2511,2605,2734,2799,2932,3101,3331,3583,3903,4207,4486,4748,5106,5551,6024,6439,6994,7636,8480,9472,10673,11830,12803,13859,15549,16720,17954,19504,21334,22458,23223,23749,24876,25664,26841,28051,29618,31332,33394,35551,37992,39247,39238,39869,41792,43520,46893,49361,50607,49590
|
||||||
|
"Michigan",26,790,657,540,394,347,453,530,619,685,572,625,680,829,1050,1354,1391,1325,1329,1466,1563,1527,1718,1896,1985,2202,2076,2238,2275,2309,2246,2358,2438,2427,2592,2728,2949,3215,3450,3554,3906,4145,4194,4501,4966,5552,5926,6279,7084,7957,8834,9701,10369,11125,11462,12243,13576,14734,15573,16130,17198,18276,19022,19318,20278,21390,22862,23975,24447,25570,26807,28113,29612,30196,30410,31446,31890,32516,33452,34441,35215,34280
|
||||||
|
"Minnesota",27,599,552,457,363,308,358,451,472,540,494,517,524,615,797,947,1006,1110,1190,1270,1451,1328,1437,1582,1633,1711,1724,1790,1838,1930,2016,2065,2155,2236,2331,2460,2540,2781,2997,3182,3463,3779,4053,4275,4628,5431,5838,6216,6729,7559,8471,9409,10320,11320,11992,12594,14255,15093,15881,16899,17592,18966,20011,20489,21698,22068,23467,24583,26267,27548,29503,30793,32101,32835,33553,34744,36505,37400,39367,41059,42299,40920
|
||||||
|
"Mississippi",28,286,202,175,127,131,174,177,229,224,201,205,215,307,439,533,629,629,611,670,802,705,770,851,906,940,928,1045,1051,1063,1156,1245,1237,1322,1362,1499,1557,1688,1839,2001,2197,2408,2641,2867,3208,3613,3936,4205,4757,5259,5806,6549,7076,7901,8301,8615,9463,9922,10293,10913,11695,12540,13164,13806,14711,15468,16549,17185,18044,18885,20013,20688,20993,22222,22540,23365,24501,26120,27276,28772,29591,29318
|
||||||
|
"Missouri",29,621,561,491,365,334,367,420,466,508,475,504,519,640,806,963,1068,1130,1191,1224,1376,1327,1427,1554,1659,1737,1726,1818,1905,1949,2044,2126,2156,2215,2330,2427,2533,2738,2895,3067,3370,3561,3843,4107,4443,4937,5282,5733,6306,6991,7787,8713,9390,10457,11035,11716,12960,13868,14505,15250,16086,17083,17751,18560,19542,20295,21267,22094,23099,24252,25403,26376,27445,28156,28771,29702,30847,31644,33354,34558,35775,35106
|
||||||
|
"Montana",30,592,501,382,339,298,364,476,475,512,517,533,569,713,901,1150,1183,1206,1308,1484,1642,1412,1654,1806,1823,1810,1771,1894,1929,1983,2068,2023,2075,2025,2363,2330,2366,2548,2730,2805,2955,3284,3625,3789,4355,5012,5380,5794,6200,6636,7721,8299,9143,10244,10672,11045,11705,11900,12465,12996,13362,14623,15524,16509,17114,18072,18129,18764,19383,20167,21324,22019,22569,24342,24699,25963,27517,28987,30942,32625,33293,32699
|
||||||
|
"Nebraska",31,596,521,413,307,275,259,409,396,415,405,400,442,551,822,1019,1091,1186,1186,1274,1558,1346,1560,1641,1761,1676,1753,1650,1675,1936,2025,2022,2125,2134,2292,2341,2392,2656,2890,2990,3167,3572,3796,4121,4527,5269,5465,6168,6453,6993,8120,8784,9272,10685,11228,11601,12968,13743,14215,15035,15984,16878,18088,18766,19688,20167,21168,22196,24045,24590,25861,27049,27829,29098,29499,31262,32371,33395,34753,36880,38128,37057
|
||||||
|
"Nevada",32,868,833,652,550,495,546,658,843,762,780,861,895,982,1558,1511,1483,1611,1757,1770,1758,1795,1991,2211,2397,2447,2415,2527,2488,2562,2594,2749,2890,2957,3184,3174,3209,3299,3471,3660,4114,4520,4946,5227,5557,6114,6490,7009,7719,8550,9780,10765,11780,12780,12986,13465,14435,15332,16027,16886,18180,19568,20674,21283,22694,23465,24635,25808,27142,28201,29806,31022,30529,30718,30849,32182,34757,37555,38652,40326,40332,38009
|
||||||
|
"New Hampshire",33,686,647,558,427,416,476,498,537,565,533,561,579,708,851,976,1054,1111,1150,1217,1291,1274,1348,1508,1577,1653,1703,1829,1900,2007,2004,2124,2197,2281,2392,2427,2552,2708,2963,3162,3441,3744,3896,4102,4423,4880,5279,5592,6258,6892,7786,8781,9915,11079,11906,13041,14534,15819,16974,18371,19759,20635,20713,21326,22154,22521,23820,25008,26042,27607,29679,31114,33332,33940,34335,34892,36758,37536,39997,41720,42461,41882
|
||||||
|
"New Jersey",34,918,847,736,587,523,573,625,709,747,697,749,820,957,1167,1429,1554,1582,1526,1571,1648,1624,1802,2000,2114,2232,2219,2300,2448,2551,2525,2660,2764,2830,2990,3064,3224,3414,3652,3892,4237,4525,4835,5127,5521,6043,6576,7037,7703,8462,9408,10469,11778,13057,13999,15036,16549,17652,18711,20230,22142,23595,24766,25153,26597,27101,27885,29277,30795,32372,34310,35551,36983,37959,38240,38768,40603,42142,45668,48172,49233,48123
|
||||||
|
"New Mexico",35,410,334,289,208,211,247,292,343,362,338,357,378,474,636,773,881,942,929,1013,1124,1145,1204,1346,1423,1444,1462,1543,1626,1733,1825,1895,1884,1952,2006,2054,2134,2250,2386,2490,2709,2921,3197,3431,3761,4137,4568,5045,5527,6087,6847,7619,8402,9334,9894,10367,11215,11999,12226,12686,13322,14085,14960,15744,16425,17226,17946,18852,19478,20233,21178,21853,22203,24193,24446,25128,26606,28180,29778,31320,32585,32197
|
||||||
|
"New York",36,1152,1035,881,676,626,680,722,808,838,789,825,869,996,1169,1384,1538,1644,1697,1725,1771,1729,1858,2002,2057,2144,2175,2295,2416,2522,2553,2695,2788,2866,2980,3070,3254,3422,3657,3923,4295,4603,4887,5179,5538,5980,6492,6955,7477,8153,8979,9927,11095,12364,13344,14188,15739,16734,17827,19031,20604,21966,23315,23942,25199,25589,26359,27721,29266,30480,32236,33890,34547,35371,35332,36077,38312,40592,43892,47514,48692,46844
|
||||||
|
"North Carolina",37,332,292,248,187,208,253,271,297,324,295,315,324,422,572,693,766,823,866,900,1003,969,1077,1192,1230,1274,1293,1368,1436,1424,1505,1581,1629,1689,1797,1877,2009,2143,2363,2525,2754,3051,3285,3510,3899,4365,4743,5039,5584,6058,6780,7461,8247,9184,9690,10480,11788,12649,13444,14325,15461,16539,17367,17879,19120,20042,20931,21938,22940,24188,25454,26003,27194,27650,27726,28208,29769,31209,32692,33966,34340,33564
|
||||||
|
"North Dakota",38,382,311,187,176,146,180,272,234,326,282,319,355,529,665,966,1031,1046,1088,1494,1483,1211,1360,1444,1318,1336,1364,1490,1556,1598,1847,1679,1821,1653,2320,2142,2112,2463,2507,2592,2719,3052,3214,3669,4377,6172,6120,6334,6184,6427,8136,8398,8095,10342,10990,11386,12307,12811,13126,13565,12745,14357,15880,16270,17692,17830,19033,19084,21166,20798,22767,23313,25068,26118,26770,29109,29676,31644,32856,35882,39009,38672
|
||||||
|
"Ohio",39,771,661,563,400,385,455,516,593,648,561,615,658,821,1021,1253,1312,1340,1310,1401,1539,1456,1608,1835,1916,2024,1965,2087,2182,2243,2177,2314,2391,2405,2515,2604,2754,2948,3181,3309,3616,3934,4101,4328,4691,5218,5733,6087,6753,7511,8326,9251,10103,10982,11485,12167,13449,14295,14933,15675,16739,17825,18792,19217,20242,20999,22063,22887,23613,24913,26164,27152,28400,28966,29522,30345,31240,32097,33643,34814,35521,35018
|
||||||
|
"Oklahoma",40,455,368,301,216,222,252,298,321,376,346,349,374,432,626,782,947,970,952,1028,1143,1166,1144,1290,1401,1475,1458,1516,1593,1657,1794,1857,1916,1947,1994,2055,2196,2361,2517,2702,2948,3198,3477,3711,4020,4524,4986,5475,5974,6586,7387,8485,9580,11003,11817,11725,12687,13265,13288,13464,14257,15265,16214,16721,17526,18085,18730,19394,20151,21106,22199,22953,23517,25059,25059,25719,27516,29122,31753,32781,34378,33708
|
||||||
|
"Oregon",41,668,607,505,379,358,439,458,548,556,531,571,609,818,1118,1381,1389,1357,1379,1504,1646,1604,1657,1830,1912,1916,1867,1978,2070,2055,2106,2244,2283,2349,2457,2541,2685,2852,3033,3201,3448,3677,3940,4212,4625,5135,5726,6181,6913,7556,8476,9415,10196,10862,11128,11832,12866,13547,14162,14911,16062,17222,18253,18806,19558,20404,21421,22668,23649,24845,25958,27023,28350,28866,29387,30172,31217,32108,34212,35279,35899,35210
|
||||||
|
"Pennsylvania",42,772,712,600,449,417,482,517,601,636,563,601,650,776,951,1146,1250,1280,1289,1364,1436,1405,1552,1713,1789,1894,1827,1915,2063,2174,2164,2240,2301,2334,2439,2511,2662,2830,3040,3246,3507,3815,4077,4294,4683,5168,5704,6170,6800,7493,8305,9225,10151,11184,11887,12455,13512,14445,15186,16142,17323,18725,19823,20505,21550,22211,22864,23738,24838,26092,27358,28605,29539,30085,30840,31709,33069,34131,36375,38003,39008,38827
|
||||||
|
"Rhode Island",44,874,788,711,575,559,600,645,711,731,672,720,750,934,1149,1201,1274,1278,1369,1459,1433,1378,1553,1717,1765,1855,1847,1958,1998,2024,2067,2183,2234,2329,2457,2552,2691,2870,3113,3336,3595,3865,4114,4295,4625,4972,5405,5844,6411,7004,7693,8595,9742,10815,11605,12439,13717,14685,15587,16651,18271,19657,20194,20363,21257,22137,22762,24046,25123,26631,28012,29377,29685,31378,32374,33690,35318,36461,38610,40421,41542,41283
|
||||||
|
"South Carolina",45,271,243,205,159,175,211,229,258,273,250,276,310,394,545,648,732,753,780,793,911,873,925,1115,1196,1233,1166,1229,1260,1286,1316,1392,1437,1498,1602,1669,1787,1958,2176,2340,2570,2827,3064,3274,3603,4029,4459,4720,5257,5684,6334,7044,7794,8651,9071,9775,10910,11666,12258,13056,14045,14834,16050,16409,17165,17805,18686,19473,20403,21385,22544,23545,24321,24871,25279,25875,27057,28337,29990,30958,31510,30835
|
||||||
|
"South Dakota",46,426,366,241,189,129,184,309,244,323,320,345,361,475,757,846,979,1086,1132,1270,1526,1116,1283,1497,1327,1436,1457,1342,1419,1669,1740,1564,1870,1863,2092,2020,2000,2278,2500,2577,2773,2995,3256,3538,4065,5163,5178,5667,5591,6351,7347,8158,8142,9451,9915,10195,11619,11942,12486,13217,13807,14767,16238,16961,17966,18565,19607,19848,21736,22275,23797,25045,26115,27531,27727,30072,31765,32726,33320,35998,38188,36499
|
||||||
|
"Tennessee",47,378,325,277,198,204,245,264,304,334,300,311,340,436,561,728,864,912,872,892,965,951,1028,1122,1178,1276,1274,1327,1422,1476,1512,1598,1618,1690,1771,1855,1965,2128,2332,2473,2741,2967,3189,3451,3808,4298,4696,5017,5574,6108,6895,7618,8319,9196,9695,10276,11453,12247,12995,13909,14910,15883,16821,17503,18840,19741,20696,21800,22450,23324,24576,25574,26239,27059,27647,28501,29734,30764,32314,33578,34243,33512
|
||||||
|
"Texas",48,479,412,348,266,257,294,326,372,418,404,417,438,528,719,944,1045,1058,1047,1147,1214,1300,1363,1488,1564,1598,1630,1697,1784,1856,1871,1948,1955,2021,2079,2155,2284,2433,2630,2840,3105,3373,3646,3861,4192,4683,5194,5738,6362,6979,7912,8929,9957,11391,11961,12303,13396,14196,14165,14486,15324,16323,17458,18150,19146,19825,20590,21526,22557,24242,25803,26858,27871,28519,28295,28929,30392,32448,34489,36020,36969,35674
|
||||||
|
"Utah",49,551,498,369,305,298,310,389,463,444,444,458,480,594,880,1126,1049,1121,1094,1180,1257,1262,1348,1544,1596,1604,1573,1666,1758,1862,1888,1970,2035,2091,2230,2281,2386,2494,2605,2721,2900,3103,3391,3658,3979,4326,4743,5150,5739,6328,7041,7786,8464,9290,9807,10333,11233,11846,12248,12638,13156,13977,14996,15661,16354,17031,17912,18858,19955,21156,22294,23288,23907,24899,25010,25192,26169,27905,29582,31009,31253,30107
|
||||||
|
"Vermont",50,634,576,474,365,338,383,414,471,485,457,491,516,643,775,932,953,1035,1090,1127,1192,1125,1169,1334,1380,1432,1456,1524,1655,1720,1732,1832,1923,1994,2072,2128,2266,2456,2729,2885,3128,3388,3634,3856,4176,4548,4869,5192,5753,6179,7036,7853,8702,9717,10287,10968,12048,12994,13842,14992,16197,17517,18055,18218,19293,19785,20553,21359,22295,23362,24803,25889,26901,28140,28651,29609,31240,31920,34394,36018,36940,36752
|
||||||
|
"Virginia",51,434,384,370,284,285,320,350,390,423,390,426,467,582,785,843,900,950,1002,1015,1148,1132,1257,1420,1510,1533,1554,1637,1712,1738,1784,1885,1936,2005,2121,2218,2403,2563,2746,2967,3252,3558,3795,4092,4486,4972,5484,5934,6534,7192,8040,8995,10176,11291,12075,12936,14298,15286,16237,17332,18556,19780,20538,21092,21965,22773,23709,24456,25495,26768,28343,29789,31162,32747,33235,34451,36285,38304,40644,42506,43409,43211
|
||||||
|
"Washington",53,741,658,534,402,376,443,490,569,599,582,614,658,864,1196,1469,1527,1419,1401,1504,1624,1595,1721,1874,1973,2066,2077,2116,2172,2262,2281,2380,2436,2535,2680,2735,2858,3078,3385,3566,3850,4097,4205,4381,4731,5312,5919,6533,7181,7832,8887,9965,10913,11903,12431,13124,14021,14738,15522,16300,17270,18670,20026,20901,21917,22414,23119,23878,25287,26817,28632,30392,31528,32053,32206,32934,34984,35738,38477,40782,41588,40619
|
||||||
|
"West Virginia",54,460,408,356,257,259,313,337,390,418,370,388,407,498,613,739,820,888,925,1032,1110,1023,1056,1182,1247,1276,1225,1318,1477,1594,1551,1597,1625,1671,1762,1853,1974,2126,2271,2417,2573,2798,3117,3378,3682,4026,4457,4974,5479,6053,6703,7432,8172,8866,9439,9626,10417,10936,11464,11950,12708,13529,14579,15219,16118,16724,17413,17913,18566,19388,20246,20966,21915,23333,24103,24626,25484,26374,28379,29769,31265,31843
|
||||||
|
"Wisconsin",55,673,588,469,362,333,380,461,518,551,507,513,547,672,866,1053,1109,1182,1211,1296,1434,1387,1506,1736,1799,1839,1774,1875,1990,2060,2075,2225,2258,2304,2412,2458,2616,2789,3025,3180,3441,3751,3983,4238,4595,5130,5622,6061,6670,7417,8292,9281,10161,11006,11592,12046,13182,13845,14530,15358,16201,17299,18160,18711,19872,20639,21699,22573,23554,24790,26245,27390,28232,29161,29838,30657,31703,32625,34535,35839,36594,35676
|
||||||
|
"Wyoming",56,675,585,476,374,371,411,496,551,607,561,587,602,779,944,1150,1224,1258,1362,1506,1610,1647,1719,1955,1912,1932,1858,1913,2011,2132,2172,2278,2312,2387,2502,2535,2588,2743,2906,3121,3315,3584,3919,4269,4709,5410,6172,6701,7212,8152,9384,10572,11753,12879,13251,12723,13493,14242,14004,14194,14968,16383,17996,18867,19550,20287,20957,21514,22098,23820,24927,26396,27230,29122,29828,31544,33721,36683,41548,43453,45177,42504
|
|
After Width: | Height: | Size: 125 KiB |
After Width: | Height: | Size: 168 KiB |
After Width: | Height: | Size: 194 KiB |
After Width: | Height: | Size: 426 KiB |
After Width: | Height: | Size: 345 KiB |
After Width: | Height: | Size: 241 KiB |
After Width: | Height: | Size: 206 KiB |
After Width: | Height: | Size: 139 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 100 KiB |
After Width: | Height: | Size: 264 KiB |
After Width: | Height: | Size: 118 KiB |
After Width: | Height: | Size: 190 KiB |
After Width: | Height: | Size: 95 KiB |
After Width: | Height: | Size: 164 KiB |
After Width: | Height: | Size: 216 KiB |
After Width: | Height: | Size: 145 KiB |
After Width: | Height: | Size: 184 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 108 KiB |
After Width: | Height: | Size: 103 KiB |
After Width: | Height: | Size: 226 KiB |
After Width: | Height: | Size: 215 KiB |
After Width: | Height: | Size: 139 KiB |
After Width: | Height: | Size: 452 KiB |
After Width: | Height: | Size: 114 KiB |