.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/map/plot_choropleth_rick.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_map_plot_choropleth_rick.py: RICK Choropleth Map =================== Example chloropleth map. .. GENERATED FROM PYTHON SOURCE LINES 7-36 .. code-block:: default from sqlalchemy import create_engine import matplotlib.pyplot as plt import matplotlib as mpl import pandas as pd import configparser from psycopg2 import connect import psycopg2.sql as pg import pandas.io.sql as pandasql import numpy as np import datetime import math import rick import geopandas as gpd import os import shapely from shapely.geometry import Point os.environ["PROJ_LIB"]=r"C:\Users\rliu4\AppData\Local\Continuum\anaconda3\Library\share" import importlib import matplotlib.ticker as ticker import matplotlib.font_manager as font_manager CONFIG = configparser.ConfigParser() # CONFIG.read(r'C:\Users\rliu4\Documents\Python\config.cfg') CONFIG.read(r'/home/cnangini/db.cfg') dbset = CONFIG['DBSETTINGS'] con = connect(**dbset) .. GENERATED FROM PYTHON SOURCE LINES 37-41 Data Collection ---------------- This Section grabs and formats the data. .. GENERATED FROM PYTHON SOURCE LINES 41-67 .. code-block:: default query = ''' WITH sum AS ( SELECT extract(month from pickup_datetime) as mon, extract(year from pickup_datetime) as yr, extract(week from pickup_datetime) as wk, pickup_neighbourhood, sum(count) as count FROM ptc.trip_data_agg_neighbourhood GROUP BY pickup_datetime, pickup_neighbourhood ), ward1 AS ( SELECT avg(count) as count, pickup_neighbourhood from sum WHERE (yr=2018 AND mon IN (9)) GROUP BY pickup_neighbourhood ORDER BY count ), ward2 AS ( SELECT avg(count) as count, pickup_neighbourhood from sum WHERE (yr=2016 AND mon IN (10)) GROUP BY pickup_neighbourhood ORDER BY count ) SELECT pickup_neighbourhood, geom, (b.count - a.count)/(a.count)*100 as growth FROM ward2 a LEFT JOIN ward1 b USING ( pickup_neighbourhood) LEFT JOIN gis.neighbourhood ON area_s_cd::integer=pickup_neighbourhood ''' .. GENERATED FROM PYTHON SOURCE LINES 68-69 Rotates data 17 degrees to orient Toronto perpendicularly .. GENERATED FROM PYTHON SOURCE LINES 69-80 .. code-block:: default data = gpd.GeoDataFrame.from_postgis(query, con, geom_col='geom') # data = data.to_crs({'init' :'epsg:3857'}) data = data.to_crs(epsg=3857) ## Below doesn't work because one row is MULTIPOLYGON and the ## other rows are POLYGON # for index, row in data.iterrows(): # rotated = shapely.affinity.rotate(row['geom'], angle=-17, origin = Point(0, 0)) # data.at[index, 'geom'] = rotated data['geom']=data['geom'].apply(lambda x: shapely.affinity.rotate(x, angle=-17, origin = Point(0, 0))) .. GENERATED FROM PYTHON SOURCE LINES 81-82 The function only needs these columns, in this order .. GENERATED FROM PYTHON SOURCE LINES 82-85 .. code-block:: default data=data[['geom', 'growth']] .. GENERATED FROM PYTHON SOURCE LINES 86-90 Calls the Function .. GENERATED FROM PYTHON SOURCE LINES 90-94 .. code-block:: default fig, ax = rick.charts.chloro_map(con, data, subway = True, lower = 0, upper = 300, title = 'Growth in Trips', island = False, unit = '%', nbins = 3) .. image:: /auto_examples/map/images/sphx_glr_plot_choropleth_rick_001.png :alt: plot choropleth rick :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /etc/jupyterhub/.venv/lib/python3.5/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=:' syntax is deprecated. ':' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6 return _prepare_from_string(" ".join(pjargs)) /etc/jupyterhub/.venv/lib/python3.5/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=:' syntax is deprecated. ':' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6 return _prepare_from_string(" ".join(pjargs)) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.183 seconds) .. _sphx_glr_download_auto_examples_map_plot_choropleth_rick.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_choropleth_rick.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_choropleth_rick.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_