Skip to content

Bar

bar() over named columns of a dataframe_mojo DataFrame (#364): a string x column is categorical, while a date or datetime x column uses a real time axis. The numeric y column gives bar heights, and axis titles default to both column names.

Bar

Usage

from dataviz import bar
from dataviz import save
from dataviz.core.colors import SEAGREEN
from dataviz import Theme

def main() raises:
    var categories: List[String] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    var values: List[Int] = [118, 146, 131, 159, 172, 94, 76]

    var c = bar(
        categories,
        values,
        title="Orders Shipped by Day",
        x_title="Day",
        y_title="Orders shipped",
        theme=Theme(mark_color=SEAGREEN),
    )
    save(c, "docs/src/examples/out_bar.svg")

Diverging bars (color_by_sign)

Bar – Diverging bars (color_by_sign)

from dataviz import bar
from dataviz import save
from dataviz import Theme

def main() raises:
    var months: List[String] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    var net_change: List[Float64] = [15.0, -8.0, 22.0, -3.0, 10.0, -12.0]

    var c_diverging = bar(
        months,
        net_change,
        title="Monthly Budget Variance",
        x_title="Month",
        y_title="Variance ($k)",
        theme=Theme(color_by_sign=True),
    )
    save(c_diverging, "docs/src/examples/out_bar_diverging.svg")

See Data shapes for shared input conventions.

Args:

  • df: The frame to read.
  • x: A string category or date/datetime time column.
  • y: The numeric column giving each bar’s height.
  • theme: Full styling knobs beyond this function’s own parameters.
  • width: Pixel width of the returned Plot (.size()).
  • height: Pixel height of the returned Plot (.size()).
  • title: The chart’s title, shown above the plot.
  • subtitle: A line under the title.
  • x_title: The x-axis caption; defaults to x.
  • y_title: The y-axis caption; defaults to y.
  • horizontal: Draw the bars left-to-right instead of upward.

Related

Related charts: Area · Pie/Donut

Relevant API: bar