Drawing cartograms with Matplotlib?
Asked Answered
H

5

18

In case somebody doesn't know: A cartogram is a type of map where some country/region-dependent numeric property scales the respective regions so that that property's density is (close to) constant. An example is

Example cartogram

from worldmapper.org. In this example, countries are scaled according to their population, resulting in near-constant population density.

Needless to say, this is really cool. Does anyone know of a Matplotlib-based library for drawing such maps? The method used at worldmapper.org is described in (1), so it would surprise me if no one has implemented this yet...

I'm also interested in hearing about other cartogram libraries, even if they're not made for Matplotlib.

(1) Michael T. Gastner and M. E. J. Newman, Diffusion-based method for producing density-equalizing maps, Proc. Nat. Acad. Sci. USA, 101, 7499-7504 (2004). Available at arXiv.

Hom answered 18/11, 2010 at 16:41 Comment(1)
There's a nice animated example here, with some (but maybe not all of the) details about how it was made.Substance
S
1

There's this, though it's based and a different algorithm (and though it's on the ESRI site, it doesn't require ArcGIS). Of course, once you have the cartogram you can plot it in matplotlib.

Stile answered 22/11, 2010 at 4:53 Comment(3)
Brilliant, thanks! I guess I was confusing myself when I said "with Matplotlib." I guess I was thinking about what the Matplotlib people call a toolkit, such as the Map toolkit, but there's absolutely no reason why I actually want that. Your solution will certainly suffice :-)Hom
What are you referring to? What algorithm is used? What language is it in? How does it relate to matplotlib other than perhaps generating a bitmap? The link for "this" currently leads to 2171 results including water distribution, x-ray, philadelphia open mobile stuff etc.Halette
Yes, what are you referring to? "This" just links to ArcGIS Code Sharing gallery with ~3000 user-submitted things, but many seem to be embedded tools/DLLs for ArcGIS, not scripts; so "it doesn't require ArcGIS" seems to be incorrect for many of those. And only 4 of those tools even match "cartogram", one of which is an installer.Irenairene
U
1

Here is a Javascript plugin to make cartograms using D3. It is a good, simple solution if you are not too concerned about the regions being sized accurately. If accuracy is important, there are other options available that give you more freedom to play with the algorithm's parameters to get to a more accurate result.

Here are two great standalone programs I know of:

Scapetoad is very easy to use. Just give it a shapefile, tell it which attribute to use for the scaling, and set a few accuracy parameters. If there is any doubt, this post describes the process.

Carto3F is more complex and allows for greater accuracy, though it is a bit trickier to figure out - lots of parameter settings without much documentation explaining them.

There is also a QGIS cartogram plugin, written in Python. Though I have not been able to get it to work, so cannot comment on that one.

Unison answered 5/6, 2016 at 13:7 Comment(0)
H
1

The geoplot.cartogram function in Geoplot: geospatial data visualization — geoplot 0.2.0

says it is a high-level Python geospatial plotting library, and an extension to cartopy and matplotlib.

Halette answered 20/3, 2019 at 17:55 Comment(2)
The documentation link has been updated; here is a new one that works: residentmario.github.io/geoplot/plot_references/….Stinkwood
Note that unfortunately geoplot does not support contiguous cartograms, at time of writing. There is a tracking issue for this: github.com/ResidentMario/geoplot/issues/150.Stinkwood
J
0

In short, no. But Newman has an excellent little implementation of his and Gastner's method on his website. Installing it is easy and it works from the command line. Here's an example of a workflow using this software that worked for me.

  1. Compute a grid of density estimates over some region, e.g. in Python. Store it as a matrix of numbers.
  2. Run the cart program with your density matrix as input from the command line or from as subprocess in Python.
  3. The program returns a list of new coordinates for each grid point.
  4. Pipe your shapefile points through the interp program and into a new shapefile to get the transformed map.

There are nice instructions on the main page.

Jahvist answered 30/11, 2017 at 9:59 Comment(0)
H
0

Try this library if you are using geopandas, it is quick and doesnt require much customization. https://github.com/mthh/cartogram_geopandas

Hallowell answered 20/5, 2020 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.