Skip to content

pie

Mojo function 🡭

pie

fn def pie(df: DataFrame, categories: String, values: String, inner_radius_fraction: Float64 = 0, theme: Theme = Theme(WHITE, Color(UInt8(30), UInt8(100), UInt8(180), UInt8(255)), Color(UInt8(80), UInt8(80), UInt8(80), UInt8(255)), Color(UInt8(225), UInt8(225), UInt8(225), UInt8(255)), Color(UInt8(40), UInt8(40), UInt8(40), UInt8(255)), SIMD(12), SIMD(3.5), SIMD(2), Int(60), Int(20), Int(20), Int(50), True, False, False, Color(UInt8(240), UInt8(240), UInt8(240), UInt8(255)), Int(3), Color(UInt8(60), UInt8(110), UInt8(200), UInt8(255)), Color(UInt8(235), UInt8(235), UInt8(235), UInt8(255)), Color(UInt8(220), UInt8(90), UInt8(40), UInt8(255)), ColorRamp(), ColorPalette(), SIMD(3), SIMD(15), True, SIMD(1), Int(0), False, Color(UInt8(200), UInt8(60), UInt8(60), UInt8(255)), Color(UInt8(224), UInt8(224), UInt8(224), UInt8(255)), Color(UInt8(120), UInt8(120), UInt8(120), UInt8(255)), Color(UInt8(100), UInt8(100), UInt8(100), UInt8(255)), Color(UInt8(230), UInt8(230), UInt8(230), UInt8(255)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), UInt8(90), UInt8(160), False, False, SIMD(0), SIMD(18), SIMD(14), Color(UInt8(110), UInt8(110), UInt8(110), UInt8(255)), SIMD(14), Color(UInt8(150), UInt8(150), UInt8(150), UInt8(255)), Color(UInt8(224), UInt8(236), UInt8(246), UInt8(200)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), True, False, False, True, AxisPosition.EDGE, AxisPosition.EDGE, String("sans-serif"), True, UInt8(90), Int(5), Int(4), Int(130), Int(14), Int(8), Int(14), Int(100), Int(8), SIMD(4), OutputFormat.SVG, Tooltips.AUTO, Missing.DRAW, String("(missing)"), Int(1000), False, LineStyle.SOLID, SIMD(1.5), LineStyle.SOLID, LegendPosition.RIGHT, XAxisLabelRotation.AUTO, TickFormat.AUTO, TickFormat.AUTO), width: Int = Int(640), height: Int = Int(420), title: String = "", subtitle: String = "", x_title: String = "", y_title: String = "") -> Chart[Arc]

pie() over named columns of a dataframe_mojo DataFrame (#743). Each argument names a column instead of holding the values.

See Plot.encode_frame() for how columns are read and what a column with missing values does.

Args:

  • df (DataFrame): The frame to read.
  • categories (String): The string column for this channel.
  • values (String): The numeric column for this channel.
  • inner_radius_fraction (Float64): See the list overload.
  • theme (Theme): See the list overload.
  • width (Int): See the list overload.
  • height (Int): See the list overload.
  • title (String): See the list overload.
  • subtitle (String): See the list overload.
  • x_title (String): See the list overload.
  • y_title (String): See the list overload.

Returns:

Chart[Arc]: The finished chart – unrendered.

Raises:

Error: A named column is missing, has the wrong dtype for its channel, or has missing values.

fn def pie[dtype: DType](categories: List[String], values: List[Scalar[dtype]], inner_radius_fraction: Float64 = 0, theme: Theme = Theme(WHITE, Color(UInt8(30), UInt8(100), UInt8(180), UInt8(255)), Color(UInt8(80), UInt8(80), UInt8(80), UInt8(255)), Color(UInt8(225), UInt8(225), UInt8(225), UInt8(255)), Color(UInt8(40), UInt8(40), UInt8(40), UInt8(255)), SIMD(12), SIMD(3.5), SIMD(2), Int(60), Int(20), Int(20), Int(50), True, False, False, Color(UInt8(240), UInt8(240), UInt8(240), UInt8(255)), Int(3), Color(UInt8(60), UInt8(110), UInt8(200), UInt8(255)), Color(UInt8(235), UInt8(235), UInt8(235), UInt8(255)), Color(UInt8(220), UInt8(90), UInt8(40), UInt8(255)), ColorRamp(), ColorPalette(), SIMD(3), SIMD(15), True, SIMD(1), Int(0), False, Color(UInt8(200), UInt8(60), UInt8(60), UInt8(255)), Color(UInt8(224), UInt8(224), UInt8(224), UInt8(255)), Color(UInt8(120), UInt8(120), UInt8(120), UInt8(255)), Color(UInt8(100), UInt8(100), UInt8(100), UInt8(255)), Color(UInt8(230), UInt8(230), UInt8(230), UInt8(255)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), UInt8(90), UInt8(160), False, False, SIMD(0), SIMD(18), SIMD(14), Color(UInt8(110), UInt8(110), UInt8(110), UInt8(255)), SIMD(14), Color(UInt8(150), UInt8(150), UInt8(150), UInt8(255)), Color(UInt8(224), UInt8(236), UInt8(246), UInt8(200)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), True, False, False, True, AxisPosition.EDGE, AxisPosition.EDGE, String("sans-serif"), True, UInt8(90), Int(5), Int(4), Int(130), Int(14), Int(8), Int(14), Int(100), Int(8), SIMD(4), OutputFormat.SVG, Tooltips.AUTO, Missing.DRAW, String("(missing)"), Int(1000), False, LineStyle.SOLID, SIMD(1.5), LineStyle.SOLID, LegendPosition.RIGHT, XAxisLabelRotation.AUTO, TickFormat.AUTO, TickFormat.AUTO), width: Int = Int(640), height: Int = Int(420), title: String = "", subtitle: String = "", x_title: String = "", y_title: String = "") -> Chart[Arc]

A pie chart, showing each category’s share of a whole as a wedge’s angle. Best for a small number of categories (roughly two to six) that sum to a meaningful total; more than that becomes hard to compare by angle alone, where a bar chart usually reads faster.

Mark.ARC over a categorical x and continuous y (the same shape bar() takes; values must be non-negative, with at least one positive). Pass inner_radius_fraction=0.55 (any value in [0.0, 1.0)) for a donut; see Plot.mark_arc().

Example:

from dataviz import pie
from dataviz import save

def main() raises:
    # Illustrative share of 10,000 support requests by intake channel.
    var channels: List[String] = [
        "Email", "Live chat", "Phone", "Web form", "Other",
    ]
    var tickets: List[Int] = [4200, 2800, 1700, 900, 400]

    var c = pie(
        channels,
        tickets,
        title="Illustrative Support Mix",
        width=400,
        height=300,
    )
    save(c, "docs/src/examples/out_pie.svg")

Example (Donut (donut_inner_radius_fraction)): ```mojo from dataviz import pie from dataviz import save from dataviz import Theme

def main() raises:
    # The same illustrative support mix as the pie example.
    var channels: List[String] = [
        "Email", "Live chat", "Phone", "Web form", "Other",
    ]
    var tickets: List[Int] = [4200, 2800, 1700, 900, 400]

    var c_donut = pie(
        channels,
        tickets,
        title="Illustrative Support Mix",
        inner_radius_fraction=0.55,
        width=400,
        height=300,
    )
    save(c_donut, "docs/src/examples/out_pie_donut.svg")
```

Parameters:

  • dtype (DType)

Args:

  • categories (List[String]): One wedge per entry, in the given order.
  • values (List[Scalar[dtype]]): Each category’s share; every value must be non-negative, and at least one positive.
  • inner_radius_fraction (Float64): Hole radius as a fraction of the pie radius – above 0.0 makes a donut; defaults to 0.0.
  • theme (Theme): Full styling knobs beyond this function’s own parameters – see Theme’s docstring.
  • width (Int): Pixel width of the returned Plot (.size()).
  • height (Int): Pixel height of the returned Plot (.size()).
  • title (String): The chart’s title, shown above the plot.
  • subtitle (String): A secondary line shown under the title.
  • x_title (String): Unused – a pie chart has no x-axis to label.
  • y_title (String): Unused – a pie chart has no y-axis to label.

Returns:

Chart[Arc]: The finished chart – unrendered. Call save(plot, path) to write it (any of .svg/.png/.bmp), or render(plot)/render_svg(plot) for the explicit two-step.

Raises: