How to make Value-By-Alpha Maps in Python | by Abdishakur | Towards Data Science

Ever since I have seen the beautiful aesthetics of Value-By-Alpha maps, I wanted to make one. It was not that hard to make them with a bit of hacking in QGIS or ArcGIS back then. Now, you can make your value-by-alpha map with Python thanks to Pysal library and Splot.
In this tutorial, I will guide you on how to make value-by-alpha map using Python. But first, let us understand what value-by-alpha map is, why use it, and when is it appropriate to use?
Value-by-alpha is bivariate choropleth technique where we consider two variables that affect each other say, for example, election results and population density. The second variable acts as an equalizer for the other variable of interest.
VBA modifies therefore through the alpha variable (transparency) the background colour. Thus, lower values fade into the background, while higher values pop up. The VBA maps came into existence to reduce the larger size bias in choropleth maps.
Besides the aesthetics part, VBA can be a good fit when you want to highlight spotlights through colour instead of size, using choropleth techniques. It does portray much better information than the pure choropleth map.
However, you first need to have a variable which is consequential to the variable of interest ( I,e. counties with more voters have consequential effects on election results).
We will be using the Splot Python library to create our map and Geopandas to read the Geospatial data. We use a subset data of 2012 elections in the USA. Let us first read the data and look at the first few rows.
import geopandas as gpdfrom splot.mapping import vba_choroplethimport matplotlib.pyplot as plt%matplotlib inlinegdf = gpd.read_file("data/MN_elections_2012.gpkg")gdf.head()
As you can see from the table below, we have a Geodataframe with different columns. For example, Barack OBA column holds the results in the area.
We use choropleth maps most of the time, but as we have mentioned in the preceding section, It has its limitations. Let us first make a choropleth map out of the data to create a benchmark for the data visualization. We can pick, for example, to use Barack Obama column.
fig, ax = plt.subplots(figsize=(12,10))gdf.plot(column=’BARACK OBA’, scheme=’quantiles’, cmap=’RdBu’, ax=ax)ax.axis(“off”)plt.show()
Now, we create a value-by-alpha map. To create our map, we need two columns. The first column is what we are interested in displaying in the map; for example, Barack Obama results. The second column holds the alpha values to equalize. This alpha equalization will eliminate low values from the map appearance and increases the spotlight effects in areas with high values. We can choose the total results as the alpha values.
fig, ax = plt.subplots(figsize=(12,10))vba_choropleth(     gdf[‘BARACK OBA’].values,      gdf[‘Total Resu’].values,      gdf,      rgb_mapclassify=dict(classifier=’quantiles’),     alpha_mapclassify=dict(classifier=’quantiles’),     cmap=’RdBu’,      ax=ax,     revert_alpha=False )plt.show()
The value by alpha map highlights the high-value areas immediately, even if they are n. Compared to the choropleth map, which the large areas dominate the visual, the value-by-alpha map shows clearly the high-value areas.
We can simply use a black background to emphasize the spotlight effect as well. Compare the below value-by-alpha map with the one above.
plt.style.use('dark_background')fig, ax = plt.subplots(figsize=(12,10))vba_choropleth(     gdf[‘BARACK OBA’].values,      gdf[‘Total Resu’].values,      gdf,      rgb_mapclassify=dict(classifier=’quantiles’),     alpha_mapclassify=dict(classifier=’quantiles’),     cmap=’RdBu’,      ax=ax,     revert_alpha=False )plt.show()
Without a legend, the value-by-alpha maps are hard to read. Splot provides an easy way to create a Legend, way easier than even other heavyweight GIS software applications. We also revert back to a white background.
plt.style.use('default')fig, ax = plt.subplots(figsize=(12,10))vba_choropleth(     gdf[‘BARACK OBA’].values,      gdf[‘Total Resu’].values,      gdf,      rgb_mapclassify=dict(classifier=’quantiles’),     alpha_mapclassify=dict(classifier=’quantiles’),     cmap=’RdBu’,      ax=ax,     revert_alpha=False,     legend = True )plt.show()
Finally, we can add some context to the map. For example, we can add major cities to help the reader effectively read the map.
cities = gpd.read_file(“data/ne_10_populated_places.geojson”)cities = cities.to_crs(“EPSG:26915”)fig, ax = plt.subplots(figsize=(12,10))vba_choropleth(     gdf[‘BARACK OBA’].values,      gdf[‘Total Resu’].values,      gdf,      rgb_mapclassify=dict(classifier=’quantiles’),     alpha_mapclassify=dict(classifier=’quantiles’),     cmap=’RdBu’,      ax=ax,     revert_alpha=False,     legend = True )cities.plot(ax=ax, color = "white")for x, y, label in zip(cities.geometry.x, cities.geometry.y, cities.NAME):    ax.annotate(label, xy=(x, y), xytext=(3, 3), textcoords="offset points")plt.show()
Now, we have an aesthetically pleasing map that helps us to convey the spotlight effect without the limitations of choropleth maps.
Value-by-alpha maps are an effective way to visualize geospatial data, avoiding the limitations of both choropleth and cartogram maps. In this tutorial, we have explored what value-by-alpha map is and how to create one using the Splot library.
The code for this tutorial is available at this Github repository.