class visigoth.map_layers.cartogram.Cartogram(data, lon=0, lat=1, colour=None, label=None, size=None, iterations=30, font_height=24, text_attributes={}, link_stroke='grey', link_stroke_width=2, f1=0.01, f2=0.5, palette=None, marker_manager=None)

Create a Cartogram plot layer in which a list of points with assigned radii are peturbed from their original positions to avoid overlap.

Hover over a point to see a link to the original position


data (list) – A relational data set (for example, list of dicts/lists/tuples describing each row)

Keyword Arguments:
  • lat (str or int) – Identify the column to provide the latitude value for each point
  • lon (str or int) – Identify the column to provide the longitude value for each point
  • colour (str or int) – Identify the column to define the point colour (use palette default colour if not specified)
  • label (str or int) – Identify the column to define the label
  • size (str or int) – Identify the column to determine the size of each marker
  • iterations (int) – number of iterations to run
  • font_height (int) – the height of the font for text labels
  • text_attributes (dict) – SVG attribute name value pairs to apply to labels
  • link_stroke (str) – colour to draw links to original position
  • link_stroke_width (int) – the width of links
  • f1 (float) – relative force attracting each point to its original location
  • f2 (float) – relative force repelling overlapping points
  • palette (object) – a ContinuousPalette or DiscretePalette instance to control chart colour
  • marker_manager (object) – a MarkerManager instance to control marker appearance



(Link to open example in new tab):

Source Code (Link to Github):

# -*- coding: utf-8 -*-

import random
import math

from visigoth.diagram import Diagram
from visigoth.containers import Map, Box
from visigoth.common import Legend
from visigoth.map_layers.cartogram import Cartogram
from visigoth.utils.mapping.projections import Projections
from visigoth.utils.colour import DiscretePalette
from visigoth.utils.marker import MarkerManager

d = Diagram(fill="white")

rng = random.Random(3)
cluster_count = 6

def gen(px,py,maxdist):
    angle = rng.random()*2*math.pi
    d = rng.random()*maxdist
    dx = d*math.sin(angle)
    dy = d*math.cos(angle)
    size = random.choice([2,3,4])
    cat = random.choice(["cat1", "cat2", "cat3"])
    label = "cat: "+cat
    return (px+dx,py+dy,cat,label,size)

palette = DiscretePalette()
mm = MarkerManager(min_radius=40)

cluster_centers = [(0.05+0.9*rng.random(),0.05+0.9*rng.random()) for x in range(0,cluster_count)]
data = [gen(cx,cy,0.1) for (cx,cy) in cluster_centers for x in range(0,10)]

bounds  = ((0.0,0.0),(1.0,1.0))
m = Map(512,bounds,projection=Projections.IDENTITY)
c = Cartogram(data, palette=palette, marker_manager=mm, lon=0, lat=1, colour=2, label=3, size=4)
legend = Legend(palette, width=500, legend_columns=3)


html = d.draw(format="html")

f = open("example.html", "w")