Skip to content
render_grid

render_grid

Mojo function 🡭

render_grid

fn def render_grid(plots: List[AnyChart], cells: List[GridCell], width: Int, height: Int, row_weights: List[Float64] = List(), col_weights: List[Float64] = List(), shared_y_scale: Bool = False, shared_x_scale: Bool = False, align_axes: Bool = False, title: String = "") -> Canvas

Place each plot in its own cell of one width by height canvas.

The figure size is given rather than derived from the plots, because unequal cells have no single plot size to derive it from. Each plot’s own .size() is ignored; its cell’s rect is what it renders into.

from dataviz import GridCell, Plot, render_grid
from canvas.io.png import write_png

def main():
    var x = List[Float64](0.0, 1.0, 2.0, 3.0)
    var top = Plot().mark_line().encode(x=x, y=List[Float64](1.0, 4.0, 2.0, 5.0))
    var left = Plot().mark_point().encode(x=x, y=List[Float64](2.0, 1.0, 3.0, 2.0))
    var right = Plot().mark_bar().encode(x=x, y=List[Float64](3.0, 1.0, 2.0, 4.0))

    var plots = List[AnyChart](top, left, right)
    var cells = List[GridCell](
        GridCell(0, 0, col_span=2), GridCell(1, 0), GridCell(1, 1)
    )
    var rows = List[Float64](2.0, 1.0)
    write_png(render_grid(plots, cells, 800, 600, row_weights=rows), "grid.png")

Args:

  • plots (List[AnyChart]): The charts, one per cell.
  • cells (List[GridCell]): Where each plot goes.
  • width (Int): Figure width in pixels.
  • height (Int): Figure height in pixels.
  • row_weights (List[Float64]): Relative row heights, empty for equal rows.
  • col_weights (List[Float64]): Relative column widths, empty for equal columns.
  • shared_y_scale (Bool): Give every cell one y-domain.
  • shared_x_scale (Bool): Give every cell one x-domain. The continuous marks pool their x extents; categorical cells must name the same categories in the same order.
  • align_axes (Bool): Share plot-rect edges down each column and across each row, so a cell lines up with its neighbors rather than with whatever its own tick labels happened to need (#569). Costs a second measuring render.
  • title (String): A figure title, centered above the cells, which share the height left under it. Empty for none.

Returns:

Canvas: The rendered figure.

Raises:

Error: An empty figure, a cell count that does not match, an overlapping or out-of-range cell, or a bad weight.