Skip to content

tripcolor

Mojo function 🡭

tripcolor

fn def tripcolor(df: DataFrame, x: String, y: String, z: String, gouraud: Bool = False, 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[Tripcolor]

tripcolor() 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.
  • x (String): The numeric column for this channel.
  • y (String): The numeric column for this channel.
  • z (String): The numeric column for this channel.
  • gouraud (Bool): 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[Tripcolor]: The finished chart – unrendered.

Raises:

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

fn def tripcolor[dtype: DType](x: List[Scalar[dtype]], y: List[Scalar[dtype]], z: List[Scalar[dtype]], gouraud: Bool = False, 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[Tripcolor]

A pseudocolor plot over scattered samples: the points are Delaunay- triangulated and every triangle filled from the values at its three vertices – the unstructured counterpart of a heatmap, for a field measured at stations, boreholes or any other irregular set of positions.

Mark.TRIPCOLOR. See _render_tripcolor for the painting, triplot() for the bare mesh, and tricontourf() for filled contour bands over the same samples.

Shading is flat: one color per triangle, from the mean of its three vertex values. Colors come from Theme’s color scale, so Theme(color_ramp=viridis()) makes it perceptually uniform.

Prefer this over tricontourf() when you want to see the sampling itself – every triangle is visible, so the chart shows where the data is dense and where it is guesswork. Prefer tricontourf() when the field matters more than the mesh.

Example:

from std.math import exp, sin

from dataviz import tripcolor
from dataviz.core.colormaps import viridis
from dataviz import Theme
from dataviz import save

def main() raises:
    var x = List[Float64]()
    var y = List[Float64]()
    var z = List[Float64]()
    var seed = 12345
    for _ in range(240):
        seed = (seed * 1103515245 + 12345) % 2147483648
        var px = Float64(seed % 1200) / 100.0
        seed = (seed * 1103515245 + 12345) % 2147483648
        var py = Float64(seed % 800) / 100.0
        x.append(px)
        y.append(py)
        # Same illustrative monitoring-well survey as tricontour()
        # and tricontourf(); each triangle exposes sampling density.
        var farm_x = (px - 3.2) / 1.6
        var farm_y = (py - 5.3) / 1.3
        var plant_x = (px - 9.0) / 1.2
        var plant_y = (py - 2.1) / 1.0
        var nitrate = 1.8 + 0.12 * px + 0.4 * sin(py * 1.7)
        nitrate += 5.8 * exp(-(farm_x * farm_x + farm_y * farm_y))
        nitrate += 3.6 * exp(-(plant_x * plant_x + plant_y * plant_y))
        z.append(nitrate)

    var c = tripcolor(
        x,
        y,
        z,
        theme=Theme(color_ramp=viridis()),
        title="Illustrative Well-Survey Nitrate Triangles (mg/L)",
        x_title="Easting (km)",
        y_title="Northing (km)",
    )
    save(c, "docs/src/examples/out_tripcolor.svg")

Parameters:

  • dtype (DType)

Args:

  • x (List[Scalar[dtype]]): Each sample’s x position.
  • y (List[Scalar[dtype]]): Each sample’s y position, one per x entry.
  • z (List[Scalar[dtype]]): The value at each sample, one per x entry.
  • gouraud (Bool): Interpolate each triangle’s color across it from its three vertices instead of filling it flat. Off by default (#398). SVG output approximates it; see _draw_tripcolor_layer.
  • 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[Tripcolor]: 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:

Error: Empty data, or columns of different lengths.