PyChart Reference Manual

Node:bar_plot, Next:line_plot, Previous:area, Up:Top

## Bar Plots

XXX %%bar_plot.T

bcol int | Attribute

Default: 0. Specifies the column from which base values (i.e., X values when attribute "direction" is "vertical", Y values otherwise) are extracted. The combination of "data", "bcol", and "hcol" attributes defines the set of boxes drawn by this chart. See the below example:
                  d = [[5,10], [7,22], [8,25]]
                  p = bar_plot.T(data = d, bcol = 1, hcol = 2)
Here, three bars will be drawn. The X values of the bars will be 5, 7, and 8. The Y values of the bars will be 10, 22, and 25, respectively. (In practice, because the values of bcol and hcol defaults to 1 and 2, you can write the above example just as "p = bar_plot.T(data = d)".

cluster tuple | Attribute

Default: (0, 1). This attribute is used to cluster multiple bar plots side by side in a single chart. The value should be a tuple of two integers. The second value should be equal to the total number of bar plots in the chart. The first value should be the relative position of this chart; 0 places this chart the leftmost, and N-1 (where N is the 2nd value of this attribute) places this chart the rightmost. Consider the below example:
        a = area.T(...)
        p1 = bar_plot.T(data = [[1,20][2,30]], cluster=(0,2))
        p2 = bar_plot.T(data = [[1,25],[2,10]], cluster=(1,2))
        a.add_plot(p1, p2)
        a.draw()
In this example, one group of bars will be drawn side-by-side at position x=1, one with height 20, the other with height 25. The other two bars will be drawn side by side at position x=2, one with height 30 and the other with height 10.

cluster_sep number | Attribute

Default: 0. The separation between clustered boxes. The unit is points.

data any | Attribute

Default: None. Specifies the data points. See chart_data.

data_label_format printf format string | Attribute

Default: None. The format string for the label displayed besides each bar. It can be a `printf' style format string, or a two-parameter function that takes (x,y) values and returns a string. The appearance of the string produced here can be controlled using escape sequences. See font.

data_label_offset (x,y) | Attribute

Default: (0, 5). The location of data labels relative to the sample point. See also attribute data_label_format.

direction str | Attribute

Default: "vertical". The direction the growth of the bars. The value is either 'horizontal' or 'vertical'.

error_bar <class 'pychart.error_bar.Base'> | Attribute

Default: None. The style of the error bar. See error_bar.

error_minus_col int | Attribute

Default: None. Specifies the column from which the depth of the errorbar is extracted. This attribute is meaningful only when error_bar != None.

error_plus_col int | Attribute

Default: None. The depth of the errorbar is extracted from this column in data. This attribute is meaningful only when error_bar != None.

fill_style <class 'pychart.fill_style.T'> | Attribute

Default: By default, a style is picked from standard styles round-robin. See fill_style.. Fill style of each box.

hcol int | Attribute

Default: 1. The column from which the height of each bar is extracted. See also the description of the 'bcol' attribute.

label str | Attribute

Default: "???". The label to be displayed in the legend. See legend., See font.

line_style <class 'pychart.line_style.T'> | Attribute

Default: line_style.black. The style of the outer frame of each box.

qerror_minus_col int | Attribute

Default: None. The depth of the "quartile" errorbar is extracted from this column in data. This attribute is meaningful only when error_bar != None.

qerror_plus_col int | Attribute

Default: None. The depth of the "quartile" errorbar is extracted from this column in data. This attribute is meaningful only when error_bar != None.

stack_on any | Attribute

Default: None. The value must be either None or bar_plot.T. If not None, bars of this plot are stacked on top of another bar plot.

width number | Attribute

Default: 5. Width of each box. The unit is in points.

Sample bar charts

Below is the source code that produces the above chart.

    from pychart import *
    theme.get_options()

    data = [(10, 20, 30, 5), (20, 65, 33, 5),
            (30, 55, 30, 5), (40, 45, 51, 7), (50, 25, 27, 3)]

    chart_object.set_defaults(area.T, size = (150, 120), y_range = (0, None),
                              x_coord = category_coord.T(data, 0))

    chart_object.set_defaults(bar_plot.T, data = data)

    # Draw the 1st graph. The Y upper bound is calculated automatically.
    ar = area.T(x_axis=axis.X(label="X label", format="/a-30{}%d"),
                y_axis=axis.Y(label="Y label", tic_interval=10))

    plot1=bar_plot.T(label="foo", cluster=(0, 3))
    plot2=bar_plot.T(label="bar", hcol=2, cluster=(1, 3))
    plot3=bar_plot.T(label="baz", hcol=3, cluster=(2, 3))
    ar.add_plot(plot1, plot2, plot3)
    ar.draw()

    ar = area.T(legend = legend.T(), loc=(250,0),
                x_axis=axis.X(label="X label", format="/a-30{}%d"),
                y_axis=axis.Y(label="Y label", tic_interval=10))

    bar_plot.fill_styles.reset();
    plot1=bar_plot.T(label="foo")
    plot2=bar_plot.T(label="bar", hcol=2, stack_on = plot1)
    plot3=bar_plot.T(label="baz", hcol=3, stack_on = plot2)

    ar.add_plot(plot1, plot2, plot3)
    ar.draw()

Vertical bar chart

Below is the source code that produces the above chart.

    from pychart import *
    theme.get_options()

    data = [(10, 20, 30, 5), (20, 65, 33, 5), (30, 55, 30, 5), (40, 45, 51, 7),
            (50, 25, 27, 3), (60, 75, 30, 5), (70, 80, 42, 5), (80, 62, 32, 5),
            (90, 42, 39, 5), (100, 32, 39, 4)]

    # The attribute y_coord_system="category" tells that the Y axis values
    # should be taken from samples, y_category_col'th column of
    # y_category_data.  Thus, in this example, Y values will be
    # [40,50,60,70,80].
    ar = area.T(y_coord = category_coord.T(data[3:8], 0),
                x_grid_style=line_style.gray50_dash1,
                x_grid_interval=20,
                x_range = (0,100),
                x_axis=axis.X(label="X label"),
                y_axis=axis.Y(label="Y label"),
                bg_style = fill_style.gray90,
                border_line_style = line_style.default)

    # Below call sets the default attributes for all bar plots.
    chart_object.set_defaults(bar_plot.T, direction="horizontal", data=data)

    ar.add_plot(bar_plot.T(label="foo", cluster=(0,3)))
    ar.add_plot(bar_plot.T(label="bar", hcol=2, cluster=(1,3)))
    ar.add_plot(bar_plot.T(label="baz", hcol=3, cluster=(2,3)))
    ar.draw()