
:orphan:
:html_theme.sidebar_secondary.remove:

.. This document is auto-generated by the altair-gallery extension. Do not modify directly.

.. _gallery_poly_fit_regression:


Polynomial Fit Plot with Regression Transform
=============================================
This example shows how to overlay data with multiple fitted polynomials using
the regression transform.


.. altair-plot::
    :remove-code:
    



    import numpy as np
    import pandas as pd
    import altair as alt

    # Generate some random data
    rng = np.random.RandomState(1)
    x = rng.rand(40) ** 2
    y = 10 - 1.0 / (x + 0.1) + rng.randn(40)
    source = pd.DataFrame({"x": x, "y": y})

    # Define the degree of the polynomial fits
    degree_list = [1, 3, 5]

    base = alt.Chart(source).mark_circle(color="black").encode(
            alt.X("x"), alt.Y("y")
    )

    polynomial_fit = [
        base.transform_regression(
            "x", "y", method="poly", order=order, as_=["x", str(order)]
        )
        .mark_line()
        .transform_fold([str(order)], as_=["degree", "y"])
        .encode(alt.Color("degree:N"))
        for order in degree_list
    ]

    alt.layer(base, *polynomial_fit)


.. tab-set::

    .. tab-item:: Method syntax
        :sync: method

        .. code:: python



            import numpy as np
            import pandas as pd
            import altair as alt

            # Generate some random data
            rng = np.random.RandomState(1)
            x = rng.rand(40) ** 2
            y = 10 - 1.0 / (x + 0.1) + rng.randn(40)
            source = pd.DataFrame({"x": x, "y": y})

            # Define the degree of the polynomial fits
            degree_list = [1, 3, 5]

            base = alt.Chart(source).mark_circle(color="black").encode(
                alt.X("x"),
                alt.Y("y")
            )

            polynomial_fit = [
                base.transform_regression(
                    "x", "y", method="poly", order=order, as_=["x", str(order)]
                )
                .mark_line()
                .transform_fold([str(order)], as_=["degree", "y"])
                .encode(alt.Color("degree:N"))
                for order in degree_list
            ]

            alt.layer(base, *polynomial_fit)


    .. tab-item:: Attribute syntax
        :sync: attribute

        .. code:: python



            import numpy as np
            import pandas as pd
            import altair as alt

            # Generate some random data
            rng = np.random.RandomState(1)
            x = rng.rand(40) ** 2
            y = 10 - 1.0 / (x + 0.1) + rng.randn(40)
            source = pd.DataFrame({"x": x, "y": y})

            # Define the degree of the polynomial fits
            degree_list = [1, 3, 5]

            base = alt.Chart(source).mark_circle(color="black").encode(
                    alt.X("x"), alt.Y("y")
            )

            polynomial_fit = [
                base.transform_regression(
                    "x", "y", method="poly", order=order, as_=["x", str(order)]
                )
                .mark_line()
                .transform_fold([str(order)], as_=["degree", "y"])
                .encode(alt.Color("degree:N"))
                for order in degree_list
            ]

            alt.layer(base, *polynomial_fit)
