Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import matplotlib.pyplot as plt
import mapclassify as mc
# load the data
obesity_by_state = pd.read_csv(gplt.datasets.get_path('obesity_by_state'), sep='\t')
contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa'))
contiguous_usa['Obesity Rate'] = contiguous_usa['state'].map(
lambda state: obesity_by_state.query("State == @state").iloc[0]['Percent']
)
scheme = mc.Quantiles(contiguous_usa['Obesity Rate'], k=5)
ax = gplt.cartogram(
contiguous_usa,
scale='Obesity Rate', limits=(0.75, 1),
projection=gcrs.AlbersEqualArea(central_longitude=-98, central_latitude=39.5),
hue='Obesity Rate', cmap='Reds', scheme=scheme,
linewidth=0.5,
legend=True, legend_kwargs={'loc': 'lower right'}, legend_var='hue',
figsize=(8, 12)
)
gplt.polyplot(contiguous_usa, facecolor='lightgray', edgecolor='None', ax=ax)
plt.title("Adult Obesity Rate by State, 2013")
plt.savefig("obesity.png", bbox_inches='tight', pad_inches=0.1)
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
)
gplt.polyplot(nyc_boroughs, zorder=1, ax=ax1)
plt.title("Failure to Yield Right-of-Way Crashes, 2016")
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
# load the data
nyc_boroughs = gpd.read_file(gplt.datasets.get_path('nyc_boroughs'))
nyc_injurious_collisions = gpd.read_file(gplt.datasets.get_path('nyc_injurious_collisions'))
# A plot type using Voronoi tessellation: https://en.wikipedia.org/wiki/Voronoi_diagram
proj = gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059)
f, axarr = plt.subplots(
1, 2, figsize=(16, 8), subplot_kw={'projection': proj}
)
axarr[0].axis('off')
axarr[1].axis('off')
gplt.voronoi(
nyc_injurious_collisions.head(1000),
edgecolor='lightsteelblue', linewidth=0.5, ax=axarr[0],
)
gplt.polyplot(nyc_boroughs, linewidth=0.5, ax=axarr[0])
gplt.voronoi(
nyc_injurious_collisions.head(1000),
hue='NUMBER OF PERSONS INJURED', cmap='Reds',
edgecolor='white', clip=nyc_boroughs.geometry,
linewidth=0.5, ax=axarr[1]
)
# If we have data in the shape of points in space, we may generate a
# three-dimensional heatmap on it using ``kdeplot``.
ax = geoplot.kdeplot(
collisions.head(1000), clip=boroughs.geometry,
shade=True, cmap='Reds',
projection=geoplot.crs.AlbersEqualArea())
geoplot.polyplot(boroughs, ax=ax, zorder=1)
###############################################################################
# Alternatively, we may partition the space into neighborhoods automatically,
# using Voronoi tessellation. This is a good way of visually verifying whether
# or not a certain data column is spatially correlated.
ax = geoplot.voronoi(
collisions.head(1000), projection=geoplot.crs.AlbersEqualArea(),
clip=boroughs.simplify(0.001),
hue='NUMBER OF PERSONS INJURED', cmap='Reds',
legend=True,
edgecolor='white'
)
geoplot.polyplot(boroughs, edgecolor='black', zorder=1, ax=ax)
``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)
`_.
"""
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)
This example was inspired by the blog post `"Californians love Brooklyn, New Jerseyans love
Midtown: Mapping NYC’s Visitors Through Parking Tickets"
`_.
"""
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
# load the data
nyc_boroughs = gpd.read_file(gplt.datasets.get_path('nyc_boroughs'))
tickets = gpd.read_file(gplt.datasets.get_path('nyc_parking_tickets'))
proj = gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059)
def plot_state_to_ax(state, ax):
gplt.choropleth(
tickets.set_index('id').loc[:, [state, 'geometry']],
hue=state, cmap='Blues',
linewidth=0.0, ax=ax
)
gplt.polyplot(
nyc_boroughs, edgecolor='black', linewidth=0.5, ax=ax
)
f, axarr = plt.subplots(2, 2, figsize=(12, 12), subplot_kw={'projection': proj})
plt.suptitle('Parking Tickets Issued to State by Precinct, 2016', fontsize=16)
plt.subplots_adjust(top=0.95)
plot_state_to_ax('ny', axarr[0][0])
"""
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
# load the data
nyc_boroughs = gpd.read_file(gplt.datasets.get_path('nyc_boroughs'))
nyc_fatal_collisions = gpd.read_file(gplt.datasets.get_path('nyc_fatal_collisions'))
nyc_injurious_collisions = gpd.read_file(gplt.datasets.get_path('nyc_injurious_collisions'))
fig = plt.figure(figsize=(10,5))
proj = projection=gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059)
ax1 = plt.subplot(121, projection=proj)
ax2 = plt.subplot(122, projection=proj)
ax1 = gplt.pointplot(
nyc_fatal_collisions, projection=proj,
hue='BOROUGH', cmap='Set1',
edgecolor='white', linewidth=0.5,
scale='NUMBER OF PERSONS KILLED', limits=(8, 24),
legend=True, legend_var='scale',
legend_kwargs={'loc': 'upper left', 'markeredgecolor': 'black'},
legend_values=[2, 1], legend_labels=['2 Fatalities', '1 Fatality'],
ax=ax1
)
gplt.polyplot(nyc_boroughs, ax=ax1)
ax1.set_title("Fatal Crashes in New York City, 2016")
==========================================
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)