{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#chorogrid tutorial, part 2:\n",
"\n",
"## Chorogrid class\n",
"\n",
"This class can be used independently of the Colorbin class shown in part 1, and vice-versa, but they work well together."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class Chorogrid in module chorogrid.Chorogrid:\n",
"\n",
"class Chorogrid(builtins.object)\n",
" | An object which makes choropleth grids, instantiated with:\n",
" | csv_path: the path to a csv data file with the following columns:\n",
" | * ids: e.g., states or countries, corresponding to\n",
" | the Colorbin.colorlist\n",
" | * coordinates or path\n",
" | ids: a listlike object of ids corresponding to colors\n",
" | colors: a listlike object of colors in hex (#123456) format\n",
" | corresponding to ids\n",
" | id_column: the name of the column in csv_path containing ids\n",
" | if there is not a 1:1 map between the ids object\n",
" | and the contents of id_column, you will be warned\n",
" | \n",
" | Methods (introspect to see arguments)\n",
" | set_colors: pass a new list of colors to replace the one\n",
" | used when the class was instantiated\n",
" | set_title: set a title for the map\n",
" | set_legend: set a legend\n",
" | add_svg: add some custom svg code. This must be called\n",
" | after the draw_... method, because it needs to know\n",
" | the margins.\n",
" | \n",
" | draw_squares: draw a square grid choropleth\n",
" | draw_hex: draw a hex-based choropleth\n",
" | draw_multihex: draw a multiple-hex-based choropleth\n",
" | draw_multisquare: draw a multiple-square-based choropleth\n",
" | draw_map: draw a regular, geographic choropleth\n",
" | \n",
" | done: save and/or display the result in IPython notebook\n",
" | done_with_overlay: overlay two Chorogrid objects\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __init__(self, csv_path, ids, colors, id_column='abbrev')\n",
" | \n",
" | add_svg(self, text, offset=[0, 0])\n",
" | Adds svg text to the final output. Can be called more than once.\n",
" | \n",
" | done(self, show=True, save_filename=None)\n",
" | if show == True, displays the svg in IPython notebook. If save_filename\n",
" | is specified, saves svg file\n",
" | \n",
" | done_and_overlay(self, other_chorogrid, show=True, save_filename=None)\n",
" | Overlays a second chorogrid object on top of the root object.\n",
" | \n",
" | draw_hex(self, x_column='hex_x', y_column='hex_y', true_rows=True, **kwargs)\n",
" | Creates an SVG file based on a hexagonal grid, with coordinates \n",
" | from the specified columns in csv_path (specified when Chorogrid class\n",
" | initialized).\n",
" | \n",
" | Note that hexagonal grids can have two possible layouts:\n",
" | 1. 'true rows' (the default), in which:\n",
" | * hexagons lie in straight rows joined by vertical sides to east and west\n",
" | * hexagon points lie to north and south\n",
" | * the home point (x=0, y=0 from upper left/northwest) has (1,0) to its immediate east\n",
" | * the home point (0,0) shares its southeast side with (0,1)'s northwest side\n",
" | * then (0,1) shares its southwest side with (0,2)'s northeast side\n",
" | * thus odd rows are offset to the east of even rows\n",
" | 2. 'true columns', in which:\n",
" | * hexagons lie in straight columns joined by horizontal sides to north and south\n",
" | * hexagon points lie to east and west\n",
" | * the home point (x=0, y=0 from upper left/northwest) has (0,1) to its immediate south\n",
" | * the home point (0,0) shares its southeast side with (1,0)'s northwest side.\n",
" | * then (1,0) shares its northeast side with (2,0)'s southwest side.\n",
" | * thus odd columns are offset to the south of even columns\n",
" | \n",
" | Note on kwarg dicts: defaults will be used for all keys unless \n",
" | overridden, i.e. you don't need to state all the key-value pairs.\n",
" | \n",
" | kwarg: font_dict\n",
" | default: {'font-style': 'normal', 'font-weight': 'normal', \n",
" | 'font-size': '12px', 'line-height': '125%', \n",
" | 'text-anchor': 'middle', 'font-family': 'sans-serif', \n",
" | 'letter-spacing': '0px', 'word-spacing': '0px', \n",
" | 'fill-opacity': 1, 'stroke': 'none', \n",
" | 'stroke-width': '1px', 'stroke-linecap': 'butt', \n",
" | 'stroke-linejoin': 'miter', 'stroke-opacity': 1,\n",
" | 'fill': '#000000'}\n",
" | \n",
" | kwarg: spacing_dict\n",
" | default: {'margin_left': 30, 'margin_top': 60, \n",
" | 'margin_right': 40, 'margin_bottom': 20, \n",
" | 'cell_width': 40, 'title_y_offset': 30, \n",
" | 'name_y_offset': 15, 'stroke_width': 0\n",
" | 'gutter': 1, 'stroke_color': '#ffffff', \n",
" | 'missing_color': '#a0a0a0',\n",
" | 'legend_offset': [0, -10]}\n",
" | \n",
" | kwarg: font_colors\n",
" | default: \"#000000\"\n",
" | if specified, must be either listlike object of colors \n",
" | corresponding to ids, a dict of hex colors to font color, or a \n",
" | string of a single color.\n",
" | \n",
" | draw_map(self, path_column='map_path', **kwargs)\n",
" | Creates an SVG file based on SVG paths delineating a map, \n",
" | with paths from the specified columns in csv_path \n",
" | (specified when Chorogrid class initialized).\n",
" | \n",
" | Note on kwarg dict: defaults will be used for all keys unless \n",
" | overridden, i.e. you don't need to state all the key-value pairs.\n",
" | \n",
" | Note that the map does not have an option for font_dict, as\n",
" | it will not print labels.\n",
" | \n",
" | kwarg: spacing_dict\n",
" | # Note that total_width and total_height will depend on where \n",
" | # the paths came from.\n",
" | # For the USA map included with this python module,\n",
" | # they are 959 and 593.\n",
" | default: {'map_width': 959, 'map_height': 593,\n",
" | 'margin_left': 10, 'margin_top': 20, \n",
" | 'margin_right': 80, 'margin_bottom': 20, \n",
" | 'title_y_offset': 45,\n",
" | 'stroke_color': '#ffffff', 'stroke_width': 0.5, \n",
" | 'missing_color': '#a0a0a0',\n",
" | 'legend_offset': [0, 0]}\n",
" | \n",
" | draw_multihex(self, x_column='fourhex_x', y_column='fourhex_y', contour_column='fourhex_contour', x_label_offset_column='fourhex_label_offset_x', y_label_offset_column='fourhex_label_offset_y', **kwargs)\n",
" | Creates an SVG file based on a hexagonal grid, with contours\n",
" | described by the following pattern:\n",
" | a: up and to the right\n",
" | b: down and to the right\n",
" | c: down\n",
" | d: down and to the left\n",
" | e: up and to the left\n",
" | f: up\n",
" | Capital letters signify a move without drawing.\n",
" | \n",
" | Note on kwarg dicts: defaults will be used for all keys unless \n",
" | overridden, i.e. you don't need to state all the key-value pairs.\n",
" | \n",
" | kwarg: font_dict\n",
" | default: {'font-style': 'normal', 'font-weight': 'normal', \n",
" | 'font-size': '12px', 'line-height': '125%', \n",
" | 'text-anchor': 'middle', 'font-family': 'sans-serif', \n",
" | 'letter-spacing': '0px', 'word-spacing': '0px', \n",
" | 'fill-opacity': 1, 'stroke': 'none', \n",
" | 'stroke-width': '1px', 'stroke-linecap': 'butt', \n",
" | 'stroke-linejoin': 'miter', 'stroke-opacity': 1,\n",
" | 'fill': '#000000'}\n",
" | \n",
" | kwarg: spacing_dict\n",
" | default: {'margin_left': 30, 'margin_top': 60, \n",
" | 'margin_right': 40, 'margin_bottom': 20, \n",
" | 'cell_width': 30, 'title_y_offset': 30, \n",
" | 'name_y_offset': 15, 'stroke_width': 1\n",
" | 'stroke_color': '#ffffff', 'missing_color': '#a0a0a0',\n",
" | 'legend_offset': [0, -10]}\n",
" | (note that there is no gutter)\n",
" | \n",
" | kwarg: font_colors\n",
" | default = \"#000000\"\n",
" | if specified, must be either listlike object of colors \n",
" | corresponding to ids, a dict of hex colors to font color, or a \n",
" | string of a single color.\n",
" | \n",
" | draw_multisquare(self, x_column='multisquare_x', y_column='multisquare_y', contour_column='multisquare_contour', x_label_offset_column='multisquare_label_offset_x', y_label_offset_column='multisquare_label_offset_y', **kwargs)\n",
" | Creates an SVG file based on a square grid, with contours\n",
" | described by the following pattern:\n",
" | a: right\n",
" | b: down\n",
" | c: left\n",
" | d: up\n",
" | A: right (without drawing)\n",
" | B: down (without drawing)\n",
" | C: left (without drawing)\n",
" | D: up (without drawing)\n",
" | \n",
" | Note on kwarg dicts: defaults will be used for all keys unless \n",
" | overridden, i.e. you don't need to state all the key-value pairs.\n",
" | \n",
" | kwarg: font_dict\n",
" | default: {'font-style': 'normal', 'font-weight': 'normal', \n",
" | 'font-size': '12px', 'line-height': '125%', \n",
" | 'text-anchor': 'middle', 'font-family': 'sans-serif', \n",
" | 'letter-spacing': '0px', 'word-spacing': '0px', \n",
" | 'fill-opacity': 1, 'stroke': 'none', \n",
" | 'stroke-width': '1px', 'stroke-linecap': 'butt', \n",
" | 'stroke-linejoin': 'miter', 'stroke-opacity': 1,\n",
" | 'fill': '#000000'}\n",
" | \n",
" | kwarg: spacing_dict\n",
" | default: {'margin_left': 30, 'margin_top': 60, \n",
" | 'margin_right': 40, 'margin_bottom': 20, \n",
" | 'cell_width': 30, 'title_y_offset': 30, \n",
" | 'name_y_offset': 15, 'stroke_width': 1\n",
" | 'stroke_color': '#ffffff', 'missing_color': '#a0a0a0',\n",
" | 'legend_offset': [0, -10]}\n",
" | (note that there is no gutter)\n",
" | \n",
" | kwarg: font_colors\n",
" | default = \"#000000\"\n",
" | if specified, must be either listlike object of colors \n",
" | corresponding to ids, a dict of hex colors to font color, or a \n",
" | string of a single color.\n",
" | \n",
" | draw_squares(self, x_column='square_x', y_column='square_y', **kwargs)\n",
" | Creates an SVG file based on a square grid, with coordinates from \n",
" | the specified columns in csv_path (specified when Chorogrid class\n",
" | initialized).\n",
" | \n",
" | Note on kwarg dicts: defaults will be used for all keys unless\n",
" | overridden, i.e. you don't need to state all the key-value pairs.\n",
" | \n",
" | kwarg: font_dict\n",
" | default: {'font-style': 'normal', 'font-weight': 'normal', \n",
" | 'font-size': '12px', 'line-height': '125%', \n",
" | 'text-anchor': 'middle', 'font-family': 'sans-serif', \n",
" | 'letter-spacing': '0px', 'word-spacing': '0px', \n",
" | 'fill-opacity': 1, 'stroke': 'none', \n",
" | 'stroke-width': '1px', 'stroke-linecap': 'butt', \n",
" | 'stroke-linejoin': 'miter', 'stroke-opacity': 1,\n",
" | 'fill': '#000000'}\n",
" | \n",
" | kwarg: spacing_dict\n",
" | default: {'margin_left': 30, 'margin_top': 60, \n",
" | 'margin_right': 40, 'margin_bottom': 20, \n",
" | 'cell_width': 40, 'title_y_offset': 30, \n",
" | 'name_y_offset': 15, 'roundedness': 3, \n",
" | 'gutter': 1, 'stroke_color': '#ffffff', \n",
" | 'stroke_width': 0, 'missing_color': '#a0a0a0',\n",
" | 'legend_offset': [0, -10]}\n",
" | \n",
" | kwarg: font_colors\n",
" | default = \"#000000\"\n",
" | if specified, must be either listlike object of colors \n",
" | corresponding to ids, a dict of hex colors to font color, or a \n",
" | string of a single color.\n",
" | \n",
" | set_colors(self, colors)\n",
" | change colors list specified when Chorogrid is instantiated\n",
" | \n",
" | set_legend(self, colors, labels, title=None, width='square', height=100, gutter=2, stroke_width=0.5, label_x_offset=2, label_y_offset=3, stroke_color='#303030', **kwargs)\n",
" | Creates a legend that will be included in any draw method.\n",
" | * width can be the text \"square\" or a number of pixels.\n",
" | * a gradient can be made with a large number of colors, and ''\n",
" | for each label that is not specified, and non-square width\n",
" | * height does not include title\n",
" | * if len(labels) can be len(colors) or len(colors)+1; the labels\n",
" | will be aside the boxes, or at the interstices/fenceposts, \n",
" | respectively; alternately, if len(labels) == 2, two fenceposts\n",
" | will be assigned\n",
" | \n",
" | kwarg: font_dict\n",
" | default: {'font-style': 'normal', 'font-weight': 'normal', \n",
" | 'font-size': '12px', 'line-height': '125%', \n",
" | 'text-anchor': 'left', 'font-family': 'sans-serif', \n",
" | 'letter-spacing': '0px', 'word-spacing': '0px', \n",
" | 'fill-opacity': 1, 'stroke': 'none', \n",
" | 'stroke-width': '1px', 'stroke-linecap': 'butt', \n",
" | 'stroke-linejoin': 'miter', 'stroke-opacity': 1,\n",
" | 'fill': '#000000'}\n",
" | \n",
" | set_title(self, title, **kwargs)\n",
" | Set a title for the grid\n",
" | kwargs:\n",
" | font_dict\n",
" | default = {'font-style': 'normal', 'font-weight': 'normal', \n",
" | 'font-size': '21px', 'line-height': '125%', \n",
" | 'text-anchor': 'middle', 'font-family': 'sans-serif', \n",
" | 'letter-spacing': '0px', 'word-spacing': '0px', \n",
" | 'fill-opacity': 1, 'stroke': 'none', \n",
" | 'stroke-width': '1px', 'stroke-linecap': 'butt', \n",
" | 'stroke-linejoin': 'miter', 'stroke-opacity': 1,\n",
" | 'fill': '#000000'}\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors defined here:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
"\n"
]
}
],
"source": [
"# import the classes\n",
"from chorogrid import Colorbin, Chorogrid\n",
"\n",
"# read the docs\n",
"help(Chorogrid)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"We'll load some sample data that reports the number of people living in the same home as one year ago per state in the United States, and use Colorbin to associate these numbers to colors. See Tutorial 1 for information on how Colorbin works."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mycolors = ['#b35806', '#f1a340', '#fee0b6', '#d8daeb', '#998ec3', '#542788']\n",
"import pandas as pd\n",
"df = pd.read_csv('chorogrid/sample_data/sample_state_data.csv')\n",
"mybin = Colorbin(df['Percent_living_in_same_home_as_one_year_ago'], mycolors, proportional=True, decimals=None)\n",
"mybin.set_decimals(1)\n",
"mybin.recalc(fenceposts=True)\n",
"mybin.calc_complements(0.5, '#e0e0e0', '#101010')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's the tail of the dataframe."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
"
\n",
"
\n",
"
state
\n",
"
Percent_living_in_same_home_as_one_year_ago
\n",
"
\n",
" \n",
" \n",
"
\n",
"
46
\n",
"
CA
\n",
"
84.2
\n",
"
\n",
"
\n",
"
47
\n",
"
AZ
\n",
"
80.4
\n",
"
\n",
"
\n",
"
48
\n",
"
AR
\n",
"
83.6
\n",
"
\n",
"
\n",
"
49
\n",
"
AL
\n",
"
85.0
\n",
"
\n",
"
\n",
"
50
\n",
"
AK
\n",
"
80.3
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state Percent_living_in_same_home_as_one_year_ago\n",
"46 CA 84.2\n",
"47 AZ 80.4\n",
"48 AR 83.6\n",
"49 AL 85.0\n",
"50 AK 80.3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.tail()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now here's a look at all the objects we'll use to make further maps, lists that are either the length of the number of observations (51) or the number of colors (6)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" states: len 51: ['WY', 'WV', 'WI']...\n",
" colors_by_state: len 51: ['#fee0b6', '#542788', '#d8daeb']...\n",
"font_colors_by_state: len 51: ['#101010', '#e0e0e0', '#101010']...\n",
" legend_colors: len 6: ['#b35806', '#f1a340', '#fee0b6']...\n",
" legend_labels: len 6: ['77.7-79.8', '79.8-81.8', '81.8-83.9']...\n"
]
}
],
"source": [
"states = list(df.state)\n",
"colors_by_state = mybin.colors_out\n",
"font_colors_by_state = mybin.complements\n",
"legend_colors = mybin.colors_in\n",
"legend_labels = mybin.labels\n",
"\n",
"for lst in ['states', 'colors_by_state', 'font_colors_by_state', 'legend_colors', 'legend_labels']:\n",
" obj = eval(lst)\n",
" print(\"{:>20}: len {:2}: {}...\".format(lst, len(obj), obj[:3]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the database within Chorogrid for U.S. states, containing identifying information and instructions for hex, square, multihex and map choropleths."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abbrev AK\n",
"full_name Alaska\n",
"long_abbrev Alas.\n",
"FIPS 2\n",
"pop 710231\n",
"sqmi 663267.3\n",
"map_path m 135.58488,358.02208 -0.24846,65.59232 1.2422...\n",
"map_fill_default 2\n",
"map_label_x 99.76261\n",
"map_label_y 398.1729\n",
"map_label_text_anchor middle\n",
"map_label_line_path NaN\n",
"altmap_path m 151.26632,459.09682 -0.31386,83.24785 1.5692...\n",
"square_x 0\n",
"square_y 0\n",
"altsquare_x 0\n",
"altsquare_y 0\n",
"hex_x 1\n",
"hex_y 0\n",
"althex_x 0\n",
"althex_y 0\n",
"fourhex_x 2\n",
"fourhex_y 1\n",
"fourhex_contour ababcdcdedefaf\n",
"fourhex_label_offset_x 0.25\n",
"fourhex_label_offset_y 0.5\n",
"Name: 0, dtype: object\n"
]
}
],
"source": [
"_ = pd.read_csv('chorogrid/databases/usa_states.csv')\n",
"print(_.iloc[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And here's a help file with descriptions of all the columns in the cell above."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abbrev Postal abbreviation for 50 states and D.C.\n",
"full_name Full name\n",
"long_abbrev Abbreviation, based on but not identical to recommendations of Associated Press\n",
"FIPS Federal Information Processing Standards\n",
"pop Population in 2013\n",
"sqmi Area in square miles\n",
"\n",
"map_path SVG path for geographic map\n",
"map_fill_default Number, 1-4, so that no states sharing a border will have same fill\n",
"map_label_x X-coordinate for map label, e.g. state name\n",
"map_label_y Y-coordinate for map label\n",
"map_label_text_anchor Text anchor (start, middle, end) for label\n",
"map_label_line_path Path for line connecting state and label, if applicable\n",
"\n",
"altmap_path Alternate SVG path, without labels\n",
"\n",
"square_x Horizontal position of square grid\n",
"square_y Vertical position of square grid\n",
"\n",
"altsquare_x Alternate horizontal position of square grid\n",
"altsquare_y Alternate vertical position of square grid\n",
"\n",
"hex_x Horizontal position of hex grid\n",
"hex_y Vertical position of hex grid\n",
"\n",
"fourhex_x Horizontal position of topmost, then leftmost, hex in four-hex multihex layout\n",
"fourhex_y Vertical position of topmost, then leftmost, hex in four-hex multihex layout\n",
"fourhex_contour Contour of four-hex layout: a=up&right, b=down&right, c=down, d=down&left, e=up&left, f=up\n",
"fourhex_label_offset_x Horizontal offset of label, in terms of hex width\n",
"fourhex_label_offset_y Vertical offset of label, in terms of hex width\n",
"\n"
]
}
],
"source": [
"with open('chorogrid/databases/usa_states_column_descriptions.txt') as f:\n",
" print(f.read())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cg = Chorogrid('chorogrid/databases/usa_states.csv', states, colors_by_state)\n",
"cg.set_title('% Living at same address as one year ago', font_dict={'font-size': 19})\n",
"cg.set_legend(legend_colors, legend_labels, title='% of population')\n",
"cg.draw_squares(spacing_dict={'margin_right': 150}) # otherwise legend will be cut off\n",
" # another strategy would be to pass a legend_offset to spacing_dict\n",
"cg.done(show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that it's very difficult to see the text in the darkest-colored states. Luckily we've create a list of font colors based on Colorbin's ``complement`` method. Let's rerun the last two lines of the cell above, but with font colors specified."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cg.draw_squares(spacing_dict={'margin_right': 150}, font_colors=font_colors_by_state)\n",
"cg.done(show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's an alternate layout of squares."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cg.draw_squares(x_column='altsquare_x', y_column='altsquare_y', spacing_dict={'margin_right': 150},\n",
" font_colors=font_colors_by_state)\n",
"cg.done(show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And here's a hex layout."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cg.draw_hex(spacing_dict={'margin_right': 150}, font_colors=font_colors_by_state)\n",
"cg.done(show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And an alternate hex layout"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cg.draw_hex(x_column='althex_x', y_column='althex_y', spacing_dict={'margin_right': 150},\n",
" font_colors=font_colors_by_state)\n",
"cg.done(show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And a traditional choropleth map."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cg = Chorogrid('chorogrid/databases/usa_states.csv', states, colors_by_state)\n",
"cg.draw_map(spacing_dict={'legend_offset': [-150,-25]})\n",
"cg.done(show=True)\n",
"\n",
"# To Do: Add state names. The required data is there in the database, but hasn't been implemented in code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And a fancy one where states are represented by four hexes each."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cg.draw_multihex()\n",
"cg.done(show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"There are currently three databases in Chorogrid: USA by state, as seen above, USA by county, which we'll look at ehre, and finally Europe by country, which will be shown below. FIPS (Federal Information Processing Codes) standards are used to identify counties."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df = pd.read_csv('chorogrid/sample_data/sample_county_data.csv', encoding='latin-1')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's have a look at the data. There are 3143 counties (including the 0-indexed one): counties are redefined every few years, this is the most recent count as of June 2015. If you have older data or if the borders change after that date, the mapping between data and map may not be perfect."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
" County and state name fips \\\n",
"3138 Yuma County, AZ 4027 \n",
"3139 Yuma County, CO 8125 \n",
"3140 Zapata County, TX 48505 \n",
"3141 Zavala County, TX 48507 \n",
"3142 Ziebach County, SD 46137 \n",
"\n",
" Median_value_of_owner-occupied_housing_units_2009-2013 \n",
"3138 118000 \n",
"3139 136600 \n",
"3140 55700 \n",
"3141 39900 \n",
"3142 70100 "
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.tail()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use colorbin to divide the quantities into six bins"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 76400, 90500, 108700, 136200, 176800, 929700]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mybin = Colorbin(df['Median_value_of_owner-occupied_housing_units_2009-2013'], mycolors, proportional=False, decimals=None)\n",
"mybin.fenceposts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Reset the fenceposts in order to have whole numbers"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['0-50000',\n",
" '50000-100000',\n",
" '100000-150000',\n",
" '150000-250000',\n",
" '250000-500000',\n",
" '500000-1000000']"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mybin.fenceposts = [0, 50000, 100000, 150000, 250000, 500000, 1000000]\n",
"mybin.recalc(False)\n",
"mybin.labels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are the columns for the counties database. Note that it only has maps, not squares or hexes or multihexes (for now, anyway)."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"name County name, e.g. \"Santa Barbara\"\n",
"state 2-letter state abbreviation, e.g. CA\n",
"map_path SVG path for county outline\n",
"fips Federal Information Processing Standards code for county in string format, e.g. \"06083\"\n",
"fips_integer Federal Information Processing Standards code for county in integer format, e.g. 6083\n",
"middle_x horizontal coordinate of center of county; not used for anything in this script, but provided just in case it's useful\n",
"middle_y vertical coordinate of center of county; not used for anything in this script, but provided just in case it's useful\n",
"\n"
]
}
],
"source": [
"with open('chorogrid/databases/usa_counties_column_descriptions.txt', 'r', encoding='utf-8') as f:\n",
" print(f.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And here's the map."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"