Liam
Liam

Reputation: 429

Matplotlib choropleth map plotting two different pieces of data

So far I have produced a Choropleth map by:

fig, ax = plt.subplots(1, figsize=(32, 16))
ax.axis('off')

df.plot(column='Income Rank', scheme='quantiles', k=7,legend=True, cmap='YlOrRd_r', ax=ax)
ax.annotate(xy=(0.1, .08),  xycoords='figure fraction', horizontalalignment='left', verticalalignment='top'
            ,s='Income deprivation Rank. Lowest rank = most deprived.')

which looks like this: enter image description here

My DF Looks like this:

geometry    Counts  WardCode Ward Name   Income Rank                                                                
POLYGON (())    1545    N09000001   Abbey   3

So it plots the rank of each area in relation to income data I have in df. Is it possible for me to plot crimes on this map aswell? I am trying to show the link between low income and high crime rate.. For example, with markers or maybe use a different colour scheme to represent high crime areas? The dataframe with my crimes looks like this:

WARDNAME    Counts
0   CENTRAL 3206
1   DUNCAIRN    757
2   BLACKSTAFF  584

I also have a df of crimes that have Latitude and Longitude, that looks like this:

Crime ID    Date    Longit  Latit   Crime type  Ward Name   Ward Code
0   01  2016-01 -5.886699   54.591309  Theft    CENTRAL N08000313

Is the only way I can plot both of these things on the same map by using Folium and plotting the Choropleth with the income values and then plotting the crimes as markers? Or can I do it without folium? Thanks

Upvotes: 2

Views: 3664

Answers (1)

swatchai
swatchai

Reputation: 18812

For choropleth maps with 2 overlaying layers of polygons, you need to use (semi or) transparent plots on top layer. Let me demonstrate with this example. You need to install geopandas to run this.

import geopandas as gpd
import matplotlib.pyplot as plt

# load world data (provided with geopandas)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

# select some countries (for top layer plots)
brazil = world[world.name == u'Brazil']
russia = world[world.name == u'Russia']

# grouping countries by continents for base layer
world = world[['continent', 'geometry']]  # for grouping purposes, take 2 columns
continents = world.dissolve(by='continent')  # only column 'geometry' is retained; no aggregated attribute

# plot world's continents first as base layer
ax1 = continents.plot(figsize=(12, 8), cmap='Set2')

# plot other polygons on top of the base layer
# 'facecolor' = 'None' specifies transparent area
# plot Brazil at upper level (zorder=11) using 'hatch' as symbol
# higher value of zorder causes the plot on top of layers with lower values
kwarg3s = {'facecolor': 'None', 'edgecolor': 'green', 'linewidth': 1.5, 'hatch': '|||'}
brazil.plot(zorder=11, ax=ax1, **kwarg3s)

# plot Russia at upper level using 'hatch' as symbol
kwarg4s = {'facecolor': 'None', 'edgecolor': 'red', 'linewidth': 0.5, 'hatch': 'xx'}
russia.plot(zorder=11, ax=ax1, **kwarg4s)
plt.show()

The resulting plot: enter image description here

Upvotes: 2

Related Questions