class visigoth.common.legend.Legend(palette, width=512, label=None, orientation='horizontal', legend_columns=1, stroke='black', stroke_width=2, font_height=24, text_attributes={}, decimal_places=3)

Create a legend graphic describing the colours used in a palette


palette (visigoth.utils.colour.Palette) – a palette object

Keyword Arguments
  • width (int) – width of the legend area

  • label (str) – a descriptive label to display

  • orientation (str) – “horizontal”|”vertical” whether to display legend horizontally or vertically (continuous palette)

  • legend_columns (int) – the number of columns to split the legend into

  • stroke (str) – the stroke colour for the line around the toggle control

  • stroke_width (int) – the stroke width for the line around the toggle control

  • font_height (int) – the font size for the legend (optional, defaults to 24)

  • text_attributes (dict) – a dict containing SVG name/value pairs

  • decimal_places (int) – the number of decimal places to display


Screenshot (Link to open SVG for interactive features):


Source Code (Link to Github):

import os
import sys
import argparse

from visigoth import Diagram

from visigoth.common.legend import Legend
from visigoth.common.space import Space

from visigoth.utils.colour import ContinuousPalette, DiscretePalette

if __name__ == "__main__":

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

    discrete_palette = DiscretePalette()
    discrete_palette.addCategory("A", "green").addCategory("B", "blue").addCategory("C", "red").addCategory("D", "orange").addCategory("E","purple")

    continuous_palette = ContinuousPalette()

    d = Diagram(fill="white")
    d.add(Legend(discrete_palette,width=700, legend_columns=3))

    svg = d.draw()

    f = open(args.outpath, "wb")