
:orphan:
:html_theme.sidebar_secondary.remove:

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

.. _gallery_scatter_with_shaded_area:


Scatter Plot with Shaded Area
-----------------------------
This example shows a scatter plot with shaded area, 
constructed using :ref:`area mark <user-guide-area-marks>` and :ref:`rect mark <user-guide-rect-marks>`.


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



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

    data = pd.DataFrame({
        "x": np.random.uniform(-4, 5, size=50),
        "y": np.random.uniform(2, 5, size=50),
    })

    rect_data = pd.DataFrame({
        "x1": [-2],
        "x2": [-1]
    })

    # define this interval between y = -x and y = -x 
    df = pd.DataFrame({ 
        "x": range(7),
        "ymin": range(7),
        "ymax": range(1,8)
    })

    points = alt.Chart(data).mark_point().encode(
        x="x",
        y="y"
    )

    interval = alt.Chart(df).mark_area(opacity=0.3).encode(
        x="x:Q",
        y="ymin:Q",
        y2="ymax:Q"
    )


    rect = alt.Chart(rect_data).mark_rect(opacity=0.3).encode(
        x="x1",
        x2="x2",
        color=alt.ColorValue("#FF0000")
    )

    points + interval + rect# No channel encoding options are specified in this chart
    # so the code is the same as for the method-based syntax.


.. tab-set::

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

        .. code:: python



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

            data = pd.DataFrame({
                "x": np.random.uniform(-4, 5, size=50),
                "y": np.random.uniform(2, 5, size=50),
            })

            rect_data = pd.DataFrame({
                "x1": [-2],
                "x2": [-1]
            })

            # define this interval between y = -x and y = -x 
            df = pd.DataFrame({ 
                "x": range(7),
                "ymin": range(7),
                "ymax": range(1,8)
            })

            points = alt.Chart(data).mark_point().encode(
                x="x",
                y="y"
            )

            interval = alt.Chart(df).mark_area(opacity=0.3).encode(
                x="x:Q",
                y="ymin:Q",
                y2="ymax:Q"
            )


            rect = alt.Chart(rect_data).mark_rect(opacity=0.3).encode(
                x="x1",
                x2="x2",
                color=alt.ColorValue("#FF0000")
            )

            points + interval + rect

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

        .. code:: python



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

            data = pd.DataFrame({
                "x": np.random.uniform(-4, 5, size=50),
                "y": np.random.uniform(2, 5, size=50),
            })

            rect_data = pd.DataFrame({
                "x1": [-2],
                "x2": [-1]
            })

            # define this interval between y = -x and y = -x 
            df = pd.DataFrame({ 
                "x": range(7),
                "ymin": range(7),
                "ymax": range(1,8)
            })

            points = alt.Chart(data).mark_point().encode(
                x="x",
                y="y"
            )

            interval = alt.Chart(df).mark_area(opacity=0.3).encode(
                x="x:Q",
                y="ymin:Q",
                y2="ymax:Q"
            )


            rect = alt.Chart(rect_data).mark_rect(opacity=0.3).encode(
                x="x1",
                x2="x2",
                color=alt.ColorValue("#FF0000")
            )

            points + interval + rect# No channel encoding options are specified in this chart
            # so the code is the same as for the method-based syntax.
