Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
KDEPlot of two NYC traffic accident contributing factors
========================================================
This example shows a ``kdeplot`` of traffic accident densities for two common contributing factors:
loss of consciousness and failure to yield right-of-way. It shows how the geospatial incidence
pattern differs between the two: lost consciousness crashes are more localized to Manhattan.
"""
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, shade_lowest=False,
clip=nyc_boroughs.geometry,
ax=ax1
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
import mplleaflet
# load the data
boston_airbnb_listings = gpd.read_file(gplt.datasets.get_path('boston_airbnb_listings'))
# we're building a webmap, so we'll first create an unprojected map.
ax = gplt.kdeplot(boston_airbnb_listings)
# Now we'll output this map to mplleaflet to generate our webmap. In this example we'll actually go one step further,
# and use a non-default tile layer as well. The default mplleaflet webmap uses the default Leaflet tile service,
# which is Open Street Map (OSM). OSM works great in a lot of cases, but tends to be very busy at a local level (an
# actual strategic choice on the part of the OSM developers, as the higher visibility rewards contributions to the
# project).
#
# Luckily Leaflet (and, by extension, mplleaflet) can be made to work with any valid time service. To do this we can use
# the mplleaflet.fig_to_html method, which creates a string (which we'll write to a file) containing our desired
# data. Here is the method signature that we need:
# >>> mplleaflet.fig_to_html(, tiles=(, )
# For this demo we'll use the super-basic Hydda.Base tile layer.
#
This example demonstrates a combined application of ``kdeplot`` and ``pointplot`` to a
dataset of AirBnB locations in Boston. The result is outputted to a webmap using the nifty
``mplleaflet`` library. We sample just 1000 points, which captures the overall trend without
overwhelming the renderer.
`Click here to see this plot as an interactive webmap.
`_
"""
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
import mplleaflet
boston_airbnb_listings = gpd.read_file(gplt.datasets.get_path('boston_airbnb_listings'))
ax = gplt.kdeplot(
boston_airbnb_listings, cmap='viridis', projection=gcrs.WebMercator(), figsize=(12, 12),
shade=True
)
gplt.pointplot(boston_airbnb_listings, s=1, color='black', ax=ax)
gplt.webmap(boston_airbnb_listings, ax=ax)
plt.title('Boston AirBnB Locations, 2016', fontsize=18)
fig = plt.gcf()
plt.savefig("boston-airbnb-kde.png", bbox_inches='tight', pad_inches=0.1)
# mplleaflet.show(fig)
In this case we see that there is small but significant amount of variation in the percentage
of trees classified per area, which ranges from 88% to 98%.
For more tools for visualizing data nullity, `check out the missingno library
`_.
"""
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
trees = gpd.read_file(gplt.datasets.get_path('san_francisco_street_trees_sample'))
sf = gpd.read_file(gplt.datasets.get_path('san_francisco'))
ax = gplt.quadtree(
trees.assign(nullity=trees['Species'].notnull().astype(int)),
projection=gcrs.AlbersEqualArea(),
hue='nullity', nmax=1, cmap='Greens', k=5, legend=True,
clip=sf, edgecolor='white', linewidth=1
)
gplt.polyplot(sf, facecolor='None', edgecolor='gray', linewidth=1, zorder=2, ax=ax)
plt.savefig("san-francisco-street-trees.png", bbox_inches='tight', pad_inches=0)
"""
Quadtree of NYC traffic collisions
==================================
This example plots traffic collisions in New York City. Overlaying a ``pointplot`` on a
``quadtree`` like this communicates information on two visual channels, position and texture,
simultaneously.
"""
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'))
collisions = gpd.read_file(gplt.datasets.get_path('nyc_collision_factors'))
ax = gplt.quadtree(
collisions, nmax=1,
projection=gcrs.AlbersEqualArea(), clip=nyc_boroughs,
facecolor='lightgray', edgecolor='white', zorder=0
)
gplt.pointplot(collisions, s=1, ax=ax)
plt.title("New York Ciy Traffic Collisions, 2016")
plt.savefig("nyc-collisions-quadtree.png", bbox_inches='tight', pad_inches=0)
classified into a specific species than others.
In this case we see that there is small but significant amount of variation in the percentage
of trees classified per area, which ranges from 88% to 98%.
For more tools for visualizing data nullity, `check out the missingno library
`_.
"""
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
trees = gpd.read_file(gplt.datasets.get_path('san_francisco_street_trees_sample'))
sf = gpd.read_file(gplt.datasets.get_path('san_francisco'))
ax = gplt.quadtree(
trees.assign(nullity=trees['Species'].notnull().astype(int)),
projection=gcrs.AlbersEqualArea(),
hue='nullity', nmax=1, cmap='Greens', k=5, legend=True,
clip=sf, edgecolor='white', linewidth=1
)
gplt.polyplot(sf, facecolor='None', edgecolor='gray', linewidth=1, zorder=2, ax=ax)
plt.savefig("san-francisco-street-trees.png", bbox_inches='tight', pad_inches=0)
"""
Sankey of traffic volumes in Washington DC
==========================================
This example plots
`annual average daily traffic volume `_
in Washington DC.
"""
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
dc_roads = gpd.read_file(gplt.datasets.get_path('dc_roads'))
gplt.sankey(
dc_roads, projection=gcrs.AlbersEqualArea(),
scale='aadt', limits=(0.1, 10), color='black'
)
plt.title("Streets in Washington DC by Average Daily Traffic, 2015")
plt.savefig("dc-street-network.png", bbox_inches='tight', pad_inches=0.1)