Note
Click here to download the full example code
KDEPlot of two NYC traffic accident contributing factors¶
This example shows traffic accident densities for two common contributing factors: loss of consciousness and failure to yield right-of-way. These factors have very different geospatial distributions: loss of consciousness crashes are more localized to Manhattan.
Out:
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'rocket' which already exists.
mpl_cm.register_cmap(_name, _cmap)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'rocket_r' which already exists.
mpl_cm.register_cmap(_name + "_r", _cmap_r)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'mako' which already exists.
mpl_cm.register_cmap(_name, _cmap)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'mako_r' which already exists.
mpl_cm.register_cmap(_name + "_r", _cmap_r)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'icefire' which already exists.
mpl_cm.register_cmap(_name, _cmap)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'icefire_r' which already exists.
mpl_cm.register_cmap(_name + "_r", _cmap_r)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'vlag' which already exists.
mpl_cm.register_cmap(_name, _cmap)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'vlag_r' which already exists.
mpl_cm.register_cmap(_name + "_r", _cmap_r)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'flare' which already exists.
mpl_cm.register_cmap(_name, _cmap)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'flare_r' which already exists.
mpl_cm.register_cmap(_name + "_r", _cmap_r)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'crest' which already exists.
mpl_cm.register_cmap(_name, _cmap)
/Users/alekseybilogur/opt/miniconda3/envs/geoplot-dev/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'crest_r' which already exists.
mpl_cm.register_cmap(_name + "_r", _cmap_r)
Text(0.5, 1.0, 'Loss of Consciousness Crashes, 2016')
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
nyc_boroughs = gpd.read_file(gplt.datasets.get_path('nyc_boroughs'))
nyc_collision_factors = gpd.read_file(gplt.datasets.get_path('nyc_collision_factors'))
proj = gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059)
fig = plt.figure(figsize=(10, 5))
ax1 = plt.subplot(121, projection=proj)
ax2 = plt.subplot(122, projection=proj)
gplt.kdeplot(
nyc_collision_factors[
nyc_collision_factors['CONTRIBUTING FACTOR VEHICLE 1'] == "Failure to Yield Right-of-Way"
],
cmap='Reds',
projection=proj,
shade=True, thresh=0.05,
clip=nyc_boroughs.geometry,
ax=ax1
)
gplt.polyplot(nyc_boroughs, zorder=1, ax=ax1)
ax1.set_title("Failure to Yield Right-of-Way Crashes, 2016")
gplt.kdeplot(
nyc_collision_factors[
nyc_collision_factors['CONTRIBUTING FACTOR VEHICLE 1'] == "Lost Consciousness"
],
cmap='Reds',
projection=proj,
shade=True, thresh=0.05,
clip=nyc_boroughs.geometry,
ax=ax2
)
gplt.polyplot(nyc_boroughs, zorder=1, ax=ax2)
ax2.set_title("Loss of Consciousness Crashes, 2016")
Total running time of the script: ( 0 minutes 8.911 seconds)