Charts (Pie and Bar)

In Ziaplot, the term “chart” is used for figures where the x value is qualitative. This includes Pie charts and bar charts.


Bar Chart

A bar chart with a single series of data is made using ziaplot.bar.BarChart. Using the chart’s context manager, each bar is added using ziaplot.bar.BarSingle.

with zp.BarChart() as p:
    zp.BarSingle(3).name('January')
    zp.BarSingle(5).name('February')
    zp.BarSingle(4).name('March')
    zp.BarSingle(8).name('April')
    p.xname = 'Month'
    p.yname = 'Number'
    p.title = 'Single Series Bar Chart'
_images/charts_1_0.svg

Alternatively, bar charts may be created from using the fromdict class method:

items = {'January': 4,
         'February': 6,
         'March': 2,
         'April': 5}
zp.BarChart.fromdict(
     items,
     xname='Month',
     yname='Number',
     title='Bar Chart From Dictionary')
_images/charts_2_0.svg

A ziaplot.bar.BarChartGrouped creates a bar chart with multiple data series, grouped by x-value. The group value names are provided when the BarChart is instantiated. Sets of bars are then added as ziaplot.bar.BarChart.BarSeries.

with zp.BarChartGrouped(groups=['January', 'February', 'March', 'April']):
    zp.BarSeries(4, 4, 5, 6).name('Apple')
    zp.BarSeries(3, 4, 4, 5).name('Blueberry')
    zp.BarSeries(2, 1, 5, 4).name('Cherry')
_images/charts_3_0.svg

Bar charts may also be drawn with horizontal bars.

with zp.BarChartGrouped(groups=['January', 'February', 'March', 'April'], horiz=True):
    zp.BarSeries(4, 4, 5, 6).name('Apple')
    zp.BarSeries(3, 4, 4, 5).name('Blueberry')
    zp.BarSeries(2, 1, 5, 4).name('Cherry')
_images/charts_4_0.svg

Or from a dictionary:

items = {'Apple': (4, 4, 5, 6),
         'Blueberry': (3, 4, 4, 5),
         'Cherry': (2, 2, 5, 4)}
zp.BarChartGrouped.fromdict(
    items,
    groups=['January', 'February', 'March', 'April'])
_images/charts_5_0.svg

Pie Chart

ziaplot.pie.Pie charts consist of wedges that are added to the pie as ziaplot.pie.Pie.PieSlice. Note the use of extrude to pull a single pie wedge out from the center. The slice values are normalized so the pie will always fill to 100%.

with zp.Pie(labelmode='percent'):
    zp.PieSlice(3).name('a').extrude(True)
    zp.PieSlice(10).name('b')
    zp.PieSlice(5).name('c').color('green')
_images/charts_6_0.svg

Pie Charts may also be made from dictionaries or from lists.

zp.Pie().fromdict({'a': 20, 'b': 30, 'c': 40, 'd': 10}, labelmode='name')
_images/charts_7_0.svg
zp.Pie().fromlist((3, 4, 2, 2, 5, 1), labelmode='value')
_images/charts_8_0.svg

The labelmode parameter changes what is displayed outside each slice, and may be name, value, percent, or none.