Pie

class visigoth.charts.pie.Pie(data, width, height, palette, stroke='black', stroke_width=2, doughnut=False, labelfn=<function Pie.<lambda>>, font_height=20, text_attributes={})

Create a Hierarchical Pie/Doughnut Chart

Parameters
  • data (dict) – A list containing (category,value) pairs where value may be a numeric value or a nested list.

  • width (int) – the width of the plot in pixels

  • height (int) – the height of the plot in pixels

  • palette (list) – a list of (category, colour) pairs

Keyword Arguments
  • stroke (str) – stroke color for pie sectors

  • stroke_width (int) – stroke width for pie sectors

  • doughnut (boolean) – set True to draw as a doughnut rather than a pie chart

  • labelfn (lambda) – function to compute a label string, given a category and numeric value

  • font_height (int) – sets the maximum font height used to display labels

  • text_attributes (dict) – attributes to apply to text labels

Example

Screenshot (Link to open SVG for interactive features):

../_images/example3.png

Source Code (Link to Github):


import os.path
import sys
import argparse

from visigoth.diagram import Diagram
from visigoth.charts.pie import Pie
from visigoth.utils.colour import DiscretePalette
from visigoth.containers.box import Box
from visigoth.common.legend import Legend
from visigoth.common.text import Text
from visigoth.common.space import Space

if __name__ == "__main__":

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

    palette = DiscretePalette()
    palette.addCategory("A","#E7FFAC").addCategory("B","#FFC9DE").addCategory("C","#B28DFF").addCategory("D","#ACE7FF")
    palette.addCategory("D.1","red").addCategory("D.2","green").addCategory("D.3","blue")
    palette.addCategory("B.2","purple").addCategory("B.1","orange")

    data = [("A",10),("B",[("B.1",15),("B.2",5)]),("C",4),("D",[("D.1",12),("D.2",3),("D.3",5)])]

    d = Diagram(fill="white")

    pie = Pie(data, 400, 400, palette, doughnut=True)
    d.add(pie)
    d.add(Space(20))
    legend = Legend(palette,400,legend_columns=2)
    d.add(legend)
    d.connect(legend,pie,"brushing")
    d.connect(pie,legend,"brushing")
    svg = d.draw()

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