Skip to content
scipython.com

Making a map out of dots

Posted on

A dotted map depicts land as a regular lattice of dots or other markers, a style much (over)used in infographics and graphic design. This post describes the dotted-map Python package, which draws dotted maps as SVG, PNG and PDF images in different map projections, using Python or the command line.

The dotted-map package lays out a lattice of dots over a map, finds how much of the area around each dot is land, and draws the dots that are mostly on land. It requires Python 3.10 or later, NumPy, pyproj (a Python interface to the PROJ library of map projections) and Shapely (a library of operations on geometric shapes), and includes the land and country outlines that it uses:

text
pip install dotted-map

Usage

The dotted-map command draws a map. The command below draws the British Isles as an SVG image 600 px wide, with dots 8 px apart:

text
dotted-map --extent -11 2 49.8 61 --width 600 --spacing 8 --colour '#4b2a8e' -o british-isles.svg

The DottedMap class does the same in Python:

python
from dotted_map import DottedMap

dmap = DottedMap((-11, 2, 49.8, 61), width=600, spacing=8)
dmap.save("british-isles.svg", colour="#4b2a8e")
Map of the British Isles drawn as purple dots on a hexagonal lattice, from Shetland in the north to the coast of northern France in the south-east.
The British Isles, in the Lambert azimuthal equal-area projection centred on 55.4°N, 4.5°W. Data: Natural Earth.

The extent is given as (lon_min, lon_max, lat_min, lat_max), in degrees, and the height of the image follows from its width and the shape of the extent on the map. The save method writes an SVG, PNG or PDF image, or a JSON file of the dots' positions and colours, according to the file's extension, and the svg method returns the SVG image as a string; both take the colour of the dots and, as size, their diameter as a fraction of the spacing (0.6 by default). The dots have no outline, so neighbouring dots stay separate at any spacing for any size less than 1. Each command-line option corresponds to an argument of DottedMap or save, with a hyphen for each underscore, as in --sea-size for sea_size.

In the SVG image, the sea dots, land dots, arcs and pins (described below) are in groups with the ids sea, land, arcs and pins, which can be selected and restyled as a whole in a vector graphics editor such as Inkscape. PNG and PDF output requires the CairoSVG package or the rsvg-convert program, part of the librsvg library; a PNG image is twice the size of the SVG image by default, for high-resolution screens. The documentation describes the other options.

Dots spaced evenly in longitude and latitude draw a map in the equirectangular (plate carrée) projection, which maps longitude and latitude linearly to \(x\) and \(y\). A degree of longitude at latitude \(\phi\) is \(\cos\phi\) times as long as a degree of latitude, so this projection stretches east–west distances by a factor of \(1/\cos\phi\): by 1.74 at 55°N, the latitude of the British Isles. The dotted-map package therefore spaces its dots evenly in projected coordinates, and converts the position of each one back to longitude and latitude to find whether it is on land.

The projection can be anything that pyproj accepts, such as an EPSG code or a PROJ string; the post Converting between an OS grid reference and longitude/latitude describes another, the transverse Mercator projection of the British National Grid. By default, the projection is the Lambert azimuthal equal-area projection centred on the extent, which preserves areas and distorts shapes least near its centre, or, for extents wider than 180° of longitude, the Equal Earth projection, an equal-area projection designed for world maps [Šavrič, Patterson and Jenny, International Journal of Geographical Information Science 33, 454 (2019), doi:10.1080/13658816.2018.1504949].

Two dotted maps of the British Isles on the same square lattice. On the left, Great Britain and Ireland are wide and squat; on the right, they have their usual proportions.
The British Isles in the plate carrée projection (left) and the Lambert azimuthal equal-area projection (right), on square lattices with the same spacing. Data: Natural Earth.

Coastlines

Each dot stands for a cell of the lattice: a rectangle as wide as the spacing between dots and as high as the spacing between rows, so that the cells tile the plane. The fraction of a cell that is land, its land fraction, is estimated from a \(5 \times 5\) array of points within it (set by the samples argument), and a dot is drawn if the land fraction is at least 0.5 (set by threshold). Testing only the centre of each cell would decide it by a single point: a small island under the centre would get a dot, but a cell half covered by land away from its centre would not. With halftone=True, the area of each dot is proportional to its cell's land fraction, as in halftone printing, and cells that are at least 15% land are drawn, which smooths the coastlines and keeps small islands.

Three dotted maps of Greece and the Aegean Sea. The first two are much alike, drawn in whole dots, with single dots for a few islands; in the third, the dots shrink towards the coasts and many more islands appear.
Greece and the Aegean Sea, with each cell tested at its centre only (left), and at 25 points, with a threshold of 0.5 (centre) and as halftone dots (right). Data: Natural Earth.

Lattice and markers

On a hexagonal lattice, the default, alternate rows are offset by half the spacing, \(s\), and the rows are \(\sqrt{3}s/2\) apart, so that each dot has six neighbours at distance \(s\). A square lattice (grid="square"), on which each dot has four, holds \(\sqrt{3}/2 \approx 0.87\) times as many dots in a given area, and its rows and columns are more conspicuous. Markers of each shape (shape="circle", "hexagon" or "square") have the area of a circle of radius \(r\): a hexagon has circumradius \(r\sqrt{2\pi/(3\sqrt{3})} \approx 1.10r\) and a square has side \(r\sqrt{\pi} \approx 1.77r\), so that changing the shape does not change how dark the map looks. The hexagons are pointy-topped, with a vertex at the top, which makes them nest on the hexagonal lattice, as the tiles do in the post Hexagonal Truchet tiling:

python
dmap = DottedMap((-20, 52, -36, 38), width=600, spacing=10)
dmap.save("africa.svg", colour="#d44627", shape="hexagon", size=0.8)
Map of Africa and Madagascar drawn as red hexagons that nest on a hexagonal lattice, with a gap for Lake Victoria.
Africa, with hexagonal markers of size 0.8. Data: Natural Earth.

Regions

The land outlines are those of Natural Earth (public domain) at a scale of 1:50 million, which show coastlines to within a few kilometres, with its lakes removed, so that the Caspian Sea, the Great Lakes and Lake Victoria are drawn as water. Natural Earth's country outlines are included too, so that a country, continent or subregion can be named by its ISO 3166 code (as in GBR) or its English name: region frames the map on it, land draws only the land within it, and highlight (described below) colours it. The frame holds the region's largest landmass and any land within 500 km of it, so that a map of Italy includes Sicily and Sardinia, and a map of France includes Corsica but not French Guiana. The land argument also takes a Shapely shape, for other land outlines, such as Natural Earth's more detailed 1:10 million outlines for maps of small areas.

python
dotted-map --region Italy --only Italy --shape hexagon --colour '#009246' --width 400 -o italy.svg
Italy, Sicily and Sardinia drawn as green hexagons on a hexagonal lattice, with no other land.
Italy, framed with --region Italy and drawn alone with --only Italy. Data: Natural Earth.

Places and routes

The add_pin method highlights the dot nearest to a longitude and latitude: it enlarges the dot, colours it and surrounds it with two rings. It returns an index for the pin, which add_arc uses to join two pins with a curve (a quadratic Bézier curve) that bows upwards, in the colour of the first pin unless given another. The sea argument sets the colour of dots drawn in the sea, in each cell that lies at least half within the projection's domain (the region of the plane onto which it maps the Earth), which outlines a world map or a globe. Points outside the domain are found by converting each point to longitude and latitude and back: some inverse projections return finite but meaningless coordinates for such points, which do not survive the round trip.

python
dmap = DottedMap((-180, 180, -57, 84), width=1200, spacing=8)
london = dmap.add_pin(-0.13, 51.51, colour="#f0532f")
nairobi = dmap.add_pin(36.82, -1.29, colour="#f0532f")
singapore = dmap.add_pin(103.82, 1.35, colour="#f0532f")
dmap.add_arc(london, nairobi, bend=0.25)
dmap.add_arc(london, singapore, bend=0.25)
dmap.save("world.svg", colour="#3a3f4b", sea="#e3e6ec", size=0.62)
World map, without Antarctica, in dark grey dots on a hexagonal lattice, with pale dots in the oceans that fill the rounded outline of the Equal Earth projection. Orange pins at San Francisco, New York, São Paulo, London, Nairobi, Singapore, Tokyo and Sydney are joined by curved orange lines.
The world, without Antarctica, in the Equal Earth projection, with eight pins and seven arcs. Data: Natural Earth.

Colours

The colour of the land dots can also be given by a function, which is called with arrays of the dots' longitudes and latitudes and returns a colour for each. The function below shades the dots from teal at the equator to indigo at 70° of latitude and beyond, in eight steps; each colour is a separate group in the SVG image, so a small number of colours keeps the file simple. The globe is drawn in the orthographic projection, which shows the Earth as seen from far away; with no extent given, the map shows the whole world, which in this projection is the hemisphere facing the viewer:

python
import numpy as np


def by_latitude(lon, lat):
    """Shade from teal at the equator to indigo at 70 degrees and beyond."""
    t = np.round(np.clip(np.abs(lat) / 70, 0, 1) * 7) / 7
    rgb = np.array([38, 166, 154]) + np.outer(t, [25, -85, 27])
    return [f"#{r:02x}{g:02x}{b:02x}" for r, g, b in rgb.astype(int)]


dmap = DottedMap(projection="+proj=ortho +lon_0=-25 +lat_0=28", width=600,
                 spacing=8)
dmap.save("globe.svg", colour=by_latitude, sea="#e3e6ec", size=0.66)
Globe centred on the Atlantic Ocean, drawn as dots on a hexagonal lattice: the Americas, Europe and Africa in shades from teal near the equator to indigo at high latitudes, with pale grey dots in the sea filling the disc.
A globe centred on 28°N, 25°W, in the orthographic projection, coloured by latitude. Data: Natural Earth.

The highlight argument colours the land in one or more regions, as in the post Highlighting a country on a Cartopy map. Each dot takes the colour of the region that holds the point of land in its cell nearest to its centre, so a coastal dot whose centre lies offshore is coloured too. The code below draws Europe in EPSG:3035, the Lambert azimuthal equal-area projection that Eurostat, the statistical office of the European Union, uses for its maps; the bounds are in metres in this projection:

python
dmap = DottedMap(projection="EPSG:3035", width=700, spacing=6,
                 bounds=(2.55e6, 1.35e6, 6.55e6, 5.5e6))
dmap.save("europe.svg", colour="#8e98aa", halftone=True,
          highlight={"GBR": "#f0532f"})
Map of Europe in grey dots, smaller along the coasts, with the United Kingdom, including Northern Ireland and Shetland, in orange.
Europe in the LAEA Europe projection (EPSG:3035), with halftone coastlines and the United Kingdom highlighted. Data: Natural Earth.

Code

The source code of the dotted-map package is on GitHub, and its documentation has a user guide, a list of the regions that can be named, and a reference for the command and the Python interface. The script below draws the figures in this post, in light and dark versions.

dotted_map_for_web_figs.py
A script to make the figures for the scipython.com dotted maps blog post

Comments

Leave a comment

Optional, and never shown: only the moderator sees it.

Comments are written in Markdown (code between ``` lines) and appear once approved. Your name and comment are published; your email address, if you give one, is seen only by the moderator.