Cluster

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

Create a Cluster plot

Parameters:

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

Keyword Arguments:
 
  • algorithm – the algorithm to use (currently visigoth.map_layers.cluster.AgglomerativeAlgorithm or visigoth.map_layers.cluster.KMeansAlgorithm is supported)
  • 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
  • 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

(Link to open example in new tab):

Source Code (Link to Github):

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

import math
import random

from visigoth import Diagram
from visigoth.containers import Map, Box
from visigoth.map_layers import Cluster
from visigoth.map_layers.cluster import AgglomerativeAlgorithm

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))

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

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