Bar

class visigoth.charts.bar.Bar(data, width, height, palette, waterfall=False, draw_axis=True, draw_grid=True, stroke='black', stroke_width=2, font_height=24, spacing_fraction=0.1, text_attributes={}, labelfn=<function Bar.<lambda>>, axis_max=None, axis_min=None)

Create a Bar Chart

Parameters
  • data (dict) – A list containing a (category,value)

  • 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
  • waterfall (boolean) – draw bars connected into a waterfall

  • draw_axis (boolean) – draw a y-axis

  • draw_grid (boolean) – draw grid lines

  • stroke (str) – stroke color for bars

  • stroke_width (int) – stroke width for bars

  • font_height (int) – the height of the font for text labels

  • spacing_fraction (float) – ratio of bar width to spacing

  • text_attributes (dict) – SVG attribute name value pairs to apply to labels

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

  • axis_max (int|float) – set the maximum value on the axis

  • axis_min (int|float) – set the minimum value on the axis

Example

Screenshot (Link to open SVG for interactive features):

../_images/example1.png

Source Code (Link to Github):


import os.path
import sys
import argparse

from visigoth.diagram import Diagram
from visigoth.charts.bar import Bar
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")
    palette.addCategory("B1","red").addCategory("B2","green")
    palette.addCategory("C","#B28DFF").addCategory("D","#ACE7FF")
    palette.addCategory("D1","orange").addCategory("D2","purple")

    data = [("A",10),("B",[("B1",5),("B2",15)]),("C",-4),("D",[("D1",-5),("D2",-2)])]

    d = Diagram(fill="white")

    bar1 = Bar(data, 400, 400, palette,labelfn=lambda k,v:"%d"%v)
    d.add(bar1).add(Space(20))
    bar2 = Bar(data, 400, 400, palette,waterfall=True,draw_axis=True,draw_grid=False,labelfn=lambda k,v:"%d"%v)
    d.add(bar2).add(Space(20))
    legend = Legend(palette,400,legend_columns=2)
    d.add(legend)

    # connect all pairs of main elements via a brushing channel
    for e1 in [legend,bar1,bar2]:
        for e2 in [legend,bar1,bar2]:
            if e1 != e2:
                d.connect(e1,e2,"brushing")
    svg = d.draw()

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