Skip to content
Best-Fit Trend Line

Best-Fit Trend Line

Use this when: You need to overlay a computed best-fit line directly over a scatter plot with Plot.annotate_best_fit().

Best-Fit Trend Line

Usage

from dataviz import Plot, save
from dataviz.core.colors import CORNFLOWERBLUE
from dataviz.core.theme import Theme


def main() raises:
    var x: List[Float64] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
    var y: List[Float64] = [2.3, 4.1, 3.6, 5.8, 5.1, 7.4, 6.9, 8.2, 9.0, 8.6]

    var plot = (
        Plot()
        .mark_point()
        .encode(x=x, y=y)
        .annotate_best_fit(show_equation=True, show_r_squared=True)
        .theme(Theme(mark_color=CORNFLOWERBLUE))
        .labels(
            title="Response Time vs. Load",
            subtitle="OLS best-fit line with its 95% confidence band",
        )
    )
    save(plot, "docs/src/examples/out_best_fit_line.svg")

Related

Related recipes: Error Bars

Relevant API: Plot encodings