Cluster

class visigoth.map_layers.cluster.Cluster(data, algorithm, fill=[], stroke='black', stroke_width=1, radius=5, font_height=12, text_attributes={}, label_fill='#B0B0B080')

Create a Cluster plot

Parameters

data (list) – list of (lon,lat) pairs to plot

Keyword Arguments
  • algorithm – the algorithm to use (currently visigoth.map_layers.cluster.AgglomerativeAlgorithm or visigoth.map_layers.cluster.KMeansAlgorithm is supported)

  • fill (tuple) – tuple containing the colour for each center

  • stroke (str) – stroke colour for circles representing points

  • stroke_width (int) – stroke width for circles representing points

  • radius (int) – radius of circles representing points

  • font_height (int) – font size in pixels for cluster labels (set to 0 to hide labels)

  • text_attributes (dict) – a dict containing SVG name/value attributes to apply to cluster labels

  • label_fill (str) – background colour to use for cluster labels

Example

Screenshot (Link to open SVG for interactive features):

../_images/example23.png

Source Code (Link to Github):


import os.path
import sys
import argparse
import math
import random

from visigoth import Diagram
from visigoth.containers.map import Map
from visigoth.utils.mapping import Mapping
from visigoth.containers.box import Box
from visigoth.map_layers.cluster import Cluster
from visigoth.map_layers.cluster import KMeansAlgorithm, AgglomerativeAlgorithm

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("--outpath", help="path for output SVG", default="example.svg")
    args = parser.parse_args()

    d = Diagram(fill="white")

    rng = random.Random()
    bounds = ((0,0),(1,1))
    cluster_count = 5

    def jitter(px,py,maxdist):
        angle = rng.random()*2*math.pi
        r = rng.random()*maxdist
        dx = r*math.sin(angle)
        dy = r*math.cos(angle)
        return (px+dx,py+dy)

    cluster_centers = [(rng.random(),rng.random(),0.1*rng.random()) for x in range(0,cluster_count)]
    data = [jitter(cx,cy,cr) for (cx,cy,cr) in cluster_centers for x in range(0,20)]
    m1 = Map(512,bounds,zoom_to=4)
    # alg = KMeansAlgorithm(cluster_count_min=3,cluster_count_max=8)
    alg = AgglomerativeAlgorithm(max_distance=10000)
    m1.addLayer(Cluster(data,algorithm=alg))
    d.add(Box(m1))

    svg = d.draw()

    f = open(args.outpath, "wb")
    f.write(svg)
    f.close()