Skip to content

contourf

Mojo function 🡭

contourf

fn def contourf(df: DataFrame, row: String, column: String, value: String, levels: List[Float64] = List(), level_count: Int = Int(8), 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[Contourf]

contourf() over a long-form dataframe_mojo DataFrame (#743): one row per cell, with row and column giving the cell’s coordinates and value its height.

A field is a grid, and a frame is a list of cells, so the rows are pivoted into one. Both axes come out ascending. A cell with no row is missing, not zero: it comes out blank and takes no part in the color limits (#367), which is what lets a table that was never rectangular be drawn as a field.

Args:

  • df (DataFrame): The frame to read.
  • row (String): The numeric column giving each cell’s row coordinate.
  • column (String): The numeric column giving each cell’s column coordinate.
  • value (String): The numeric column holding each cell.
  • levels (List[Float64]): Explicit contour levels; see the list overload.
  • level_count (Int): How many levels when levels is empty.
  • theme (Theme): Full styling knobs beyond this function’s own parameters.
  • 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 line under the title.
  • x_title (String): The x-axis caption; defaults to column.
  • y_title (String): The y-axis caption; defaults to row.

Returns:

Chart[Contourf]: The finished chart – unrendered.

Raises:

Error: A named column is missing, is not numeric, the columns differ in length, or a (row, column) pair repeats.

fn def contourf[dtype: DType](z: List[List[Scalar[dtype]]], levels: List[Float64] = List(), level_count: Int = Int(8), x: List[Float64] = List(), y: List[Float64] = List(), 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[Contourf]

A filled contour plot: the bands between consecutive levels of a scalar field, shaded rather than outlined – contour()’s companion, and the better read when the field’s shape matters more than its exact level lines.

Mark.CONTOURF: the same marching-squares tracing contour() uses, filling each level’s region instead of stroking its boundary. See Plot.encode_contour() (plot.mojo) for the grid’s shape.

Example:

from std.math import exp

from dataviz import contourf
from dataviz import save

def main() raises:
    # The same illustrative two-ridge elevation model as contour().
    var z = List[List[Float64]]()
    for r in range(48):
        var row = List[Float64]()
        for c in range(64):
            var x = (Float64(c) - 31.5) / 10.0
            var y = (Float64(r) - 23.5) / 10.0
            var west = exp(-((x + 1.5) * (x + 1.5) + y * y))
            var east = exp(
                -((x - 1.4) * (x - 1.4) + (y - 0.4) * (y - 0.4))
            )
            var saddle = exp(-(x * x / 2.2 + (y + 0.2) * (y + 0.2)))
            row.append(420.0 + 980.0 * west + 760.0 * east - 260.0 * saddle)
        z.append(row^)

    var c = contourf(
        z,
        level_count=11,
        title="Illustrative Mountain Elevation Bands (m)",
        x_title="East-west grid cell",
        y_title="North-south grid cell",
    )
    save(c, "docs/src/examples/out_contourf.svg")

Parameters:

  • dtype (DType)

Args:

  • z (List[List[Scalar[dtype]]]): The grid, row-major (z[row][col]), rectangular and at least 2x2. Rows are the y axis, columns the x axis, in grid-index units unless x/y give them coordinates.
  • levels (List[Float64]): The band boundaries. Left empty (the default), level_count of them are spaced evenly inside z’s own range.
  • level_count (Int): How many levels to choose when levels is empty; defaults to 8. Ignored when levels is given.
  • x (List[Float64]): One x coordinate per column of z, strictly increasing. Empty (the default) leaves the x axis in grid-index units.
  • y (List[Float64]): One y coordinate per row of z, strictly increasing. Empty (the default) leaves the y axis in grid-index units.
  • theme (Theme): Full styling knobs beyond this function’s own parameters (colors, margins, fonts, gridlines, …) – 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): The x-axis caption.
  • y_title (String): The y-axis caption.

Returns:

Chart[Contourf]: 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: