plot_bars

plot_bars(
    data,
    label=None,
    value='pct',
    orientation='auto',
    sort='auto',
    wrap=None,
    flip=None,
    avg_line=False,
    axis_labels=False,
    unit=None,
    max=None,
    fill=None,
    title=None,
    label_size=None,
)

Bar chart of a percentage table (auto-laid-out).

Plots the output of calc_percentage() (or calc_percentage_multi()) as a labelled bar chart, with a tidy auto-scaled axis (nice_max()) and the ezrsurvey theme. By default the layout is chosen for you: vertical columns for a few short labels, horizontal bars for many or long ones, with long labels wrapped, the text size stepped down as bars multiply, and the bars ordered so the longest sits at the top (bars) or on the left (cols).

The label column is auto-detected as the first column that is neither n nor the value, so a piped percentage table just works. The layout decisions all read from the bar_* options (ezrsurvey_options()), and sort="auto" leaves an ordered categorical (a registered or explicit order) as it is. Bars are drawn to a constant thickness regardless of how many there are (bar_width / bar_ref_items), so a three-answer chart and a ten-answer chart look like they belong in the same deck. Add decision guidance with annotate_bands().

Parameters

Name Type Description Default
data pandas.DataFrame A table with a category column and a value column. required
label str Category column. None (default) uses the first non-n, non-value column. None
value str Value column. Default “pct”. 'pct'
orientation str “auto” (default), “cols” (vertical) or “bars” (horizontal). 'auto'
sort str “auto” (default), “none”, “asc” or “desc”. 'auto'
wrap int Label wrap width in characters. None uses bar_wrap_cols / bar_wrap_bars for the chosen orientation. None
flip bool Back-compatible shortcut: True forces “bars”, False forces “cols”. None
avg_line bool Add a reference line at the mean value. Default False. False
axis_labels bool Show the percentage axis. Default False (the bars carry data labels). False
unit float Axis rounding step for nice_max(). None uses the pct_axis_unit option. None
max float Fixed y-axis maximum (e.g. 100). None uses the pct_axis_max option. None
fill str Bar colour. None uses the brand primary colour when a brand is set, else pal_neutral. None
title str Plot title. None
label_size float Data-label size, in R’s millimetre units as the bar_* options are. None steps down from bar_label_size as bars multiply. None

Returns

Name Type Description
plotnine.ggplot The chart.

See Also

calc_percentage, scale_y_pct, annotate_bands

Examples

>>> plot_bars(calc_percentage(podracing_survey, "demo_gender"))
>>> plot_bars(calc_percentage(podracing_survey, "fav_driver"))
>>> plot_bars(calc_percentage(podracing_survey, "demo_edu"), orientation="bars")