:orphan:
:html_theme.sidebar_secondary.remove:
.. This document is auto-generated by the altair-gallery extension. Do not modify directly.
.. _gallery_scatter_with_histogram:
Scatter Plot and Histogram with Interval Selection
==================================================
This example shows how to link a scatter plot and a histogram
together such that an interval selection in the histogram will
plot the selected values in the scatter plot.
Note that both subplots need to know about the `mbin` field created
by the `transform_bin` method. In order to achieve this, the data is
not passed to the `Chart()` instances creating the subplots, but
directly in the `hconcat()` function, which joins the two plots together.
.. altair-plot::
    :remove-code:
    
    import altair as alt
    import pandas as pd
    import numpy as np
    x = np.random.normal(size=100)
    y = np.random.normal(size=100)
    m = np.random.normal(15, 1, size=100)
    source = pd.DataFrame({"x": x, "y":y, "m":m})
    # interval selection in the scatter plot
    pts = alt.selection_interval(encodings=["x"])
    # left panel: scatter plot
    points = alt.Chart().mark_point(filled=True, color="black").encode(
        x='x',
        y='y'
    ).transform_filter(
        pts
    ).properties(
        width=300,
        height=300
    )
    # right panel: histogram
    mag = alt.Chart().mark_bar().encode(
        x='mbin:N',
        y="count()",
        color=alt.when(pts).then(alt.value("black")).otherwise(alt.value("lightgray"))
    ).properties(
        width=300,
        height=300
    ).add_params(pts)
    # build the chart:
    alt.hconcat(
        points,
        mag,
        data=source
    ).transform_bin(
        "mbin",
        field="m",
        bin=alt.Bin(maxbins=20)
    )
    # 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
            x = np.random.normal(size=100)
            y = np.random.normal(size=100)
            m = np.random.normal(15, 1, size=100)
            source = pd.DataFrame({"x": x, "y":y, "m":m})
            # interval selection in the scatter plot
            pts = alt.selection_interval(encodings=["x"])
            # left panel: scatter plot
            points = alt.Chart().mark_point(filled=True, color="black").encode(
                x='x',
                y='y'
            ).transform_filter(
                pts
            ).properties(
                width=300,
                height=300
            )
            # right panel: histogram
            mag = alt.Chart().mark_bar().encode(
                x='mbin:N',
                y="count()",
                color=alt.when(pts).then(alt.value("black")).otherwise(alt.value("lightgray"))
            ).properties(
                width=300,
                height=300
            ).add_params(pts)
            # build the chart:
            alt.hconcat(
                points,
                mag,
                data=source
            ).transform_bin(
                "mbin",
                field="m",
                bin=alt.Bin(maxbins=20)
            )
    .. tab-item:: Attribute syntax
        :sync: attribute
        .. code:: python
            import altair as alt
            import pandas as pd
            import numpy as np
            x = np.random.normal(size=100)
            y = np.random.normal(size=100)
            m = np.random.normal(15, 1, size=100)
            source = pd.DataFrame({"x": x, "y":y, "m":m})
            # interval selection in the scatter plot
            pts = alt.selection_interval(encodings=["x"])
            # left panel: scatter plot
            points = alt.Chart().mark_point(filled=True, color="black").encode(
                x='x',
                y='y'
            ).transform_filter(
                pts
            ).properties(
                width=300,
                height=300
            )
            # right panel: histogram
            mag = alt.Chart().mark_bar().encode(
                x='mbin:N',
                y="count()",
                color=alt.when(pts).then(alt.value("black")).otherwise(alt.value("lightgray"))
            ).properties(
                width=300,
                height=300
            ).add_params(pts)
            # build the chart:
            alt.hconcat(
                points,
                mag,
                data=source
            ).transform_bin(
                "mbin",
                field="m",
                bin=alt.Bin(maxbins=20)
            )
            # No channel encoding options are specified in this chart
            # so the code is the same as for the method-based syntax.