Skip to content

plot.py

The file math/src/tonnetz/plot.py. 81 lines, 1 definitions.

Matplotlib drawings for the report figures (optional dependency: matplotlib).

Same layout as the lattice on the site: the fifth is horizontal, the major third goes 60 degrees up. Major triangles point up and are filled, minor triangles point down and are hatched.

Code, comments and printed output are in Turkish. The explanations are translated.

Top of file: imports

"""Rapor şekilleri için matplotlib çizimleri (isteğe bağlı bağımlılık: matplotlib).Sitedeki kafesle aynı düzen: beşli yatay, büyük üçlü 60 derece yukarı.Majör üçgenler yukarı bakar ve düz doludur, minör üçgenler aşağı bakar ve taralıdır."""from __future__ import annotationsfrom math import ceil, floor, sqrtfrom .core import Chord, Tonnetzfrom .lattice import pitch_at, xy

H

constant
H = sqrt(3) / 2

MAJOR_COLOR

constant
MAJOR_COLOR = "#1f3a93"

MINOR_COLOR

constant
MINOR_COLOR = "#c0392b"

draw_lattice

function

Draws a window of the lattice.

chords: the chords to fill. nodes: the pitches to highlight (if None, all are plain). node_values: if a pitch -> number dictionary is given, the nodes are coloured as a heat map.

def draw_lattice(    t: Tonnetz,    ax,    cols: float = 11,    rows: float = 8,    chords=(),    nodes=None,    node_values=None,    cmap="Blues",    labels=True,):    """Kafesin bir penceresini çizer.    chords: doldurulacak akorlar. nodes: vurgulanacak perdeler (None ise hepsi sade).    node_values: perde -> sayı sözlüğü verilirse düğümler ısı haritası olarak boyanır.    """    from matplotlib import colormaps    from matplotlib.patches import Circle, Polygon    chords = set(chords)    x0, x1, y0, y1 = -cols / 2, cols / 2, -rows * H / 2, rows * H / 2    def inside(p):        return x0 - 1e-9 <= p[0] <= x1 + 1e-9 and y0 - 1e-9 <= p[1] <= y1 + 1e-9    vmax = max(node_values.values()) if node_values else 1    for j in range(floor(y0 / H) - 1, ceil(y1 / H) + 2):        for i in range(floor(x0 - j / 2) - 1, ceil(x1 - j / 2) + 2):            p = pitch_at(t, i, j)            tris = [                (Chord("M", p), [xy(i, j), xy(i + 1, j), xy(i, j + 1)]),                (Chord("m", p), [xy(i, j), xy(i + 1, j), xy(i + 1, j - 1)]),            ]            for chord, pts in tris:                if not all(inside(q) for q in pts):                    continue                on = chord in chords                if on and chord.kind == "M":                    style = dict(facecolor=MAJOR_COLOR, edgecolor="#333333")                elif on:                    style = dict(facecolor="white", edgecolor=MINOR_COLOR, hatch="////")                else:                    style = dict(facecolor="none", edgecolor="#bbbbbb")                ax.add_patch(Polygon(pts, closed=True, linewidth=0.8, zorder=1, **style))            c = xy(i, j)            if not inside(c):                continue            if node_values is not None:                value = node_values[p]                face = colormaps[cmap](0.15 + 0.8 * value / vmax if vmax else 0.15)                text, color = str(value), ("white" if value / max(vmax, 1) > 0.55 else "black")            else:                marked = nodes is not None and p in nodes                face = "#111111" if marked else "white"                text, color = str(p), ("white" if marked else ("#999999" if nodes is not None else "black"))            ax.add_patch(Circle(c, 0.24, facecolor=face, edgecolor="#333333", linewidth=0.8, zorder=2))            if labels:                ax.text(*c, text, ha="center", va="center", fontsize=7, color=color, zorder=3)    ax.set_xlim(x0 - 0.4, x1 + 0.4)    ax.set_ylim(y0 - 0.4, y1 + 0.4)    ax.set_aspect("equal")    ax.axis("off")    return ax

Every number on this site comes from data produced by the Python core and is checked by tests.

TÜBİTAK 2204-A mathematics project