
:orphan:
:html_theme.sidebar_secondary.remove:

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

.. _gallery_histogram_responsive:


Histogram with Responsive Bins
------------------------------
This shows an example of a histogram with bins that are responsive to a
selection domain. Click and drag on the bottom panel to see the bins
change on the top panel.


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


    import altair as alt
    from altair.datasets import data

    source = data.flights_5k.url

    brush = alt.selection_interval(encodings=['x'])

    base = alt.Chart(source).transform_calculate(
        time="hours(datum.date) + minutes(datum.date) / 60"
    ).mark_bar().encode(
        y='count():Q'
    ).properties(
        width=600,
        height=100
    )

    alt.vconcat(
      base.encode(
        alt.X('time:Q',
          bin=alt.Bin(maxbins=30, extent=brush),
          scale=alt.Scale(domain=brush)
        )
      ),
      base.encode(
        alt.X('time:Q', bin=alt.Bin(maxbins=30)),
      ).add_params(brush)
    )


.. tab-set::

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

        .. code:: python


            import altair as alt
            from altair.datasets import data

            source = data.flights_5k.url

            brush = alt.selection_interval(encodings=['x'])

            base = alt.Chart(source).transform_calculate(
                time="hours(datum.date) + minutes(datum.date) / 60"
            ).mark_bar().encode(
                y='count():Q'
            ).properties(
                width=600,
                height=100
            )

            alt.vconcat(
              base.encode(
                alt.X('time:Q')
                  .bin(maxbins=30, extent=brush)
                  .scale(domain=brush)
              ),
              base.encode(
                alt.X('time:Q').bin(maxbins=30),
              ).add_params(brush)
            )


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

        .. code:: python


            import altair as alt
            from altair.datasets import data

            source = data.flights_5k.url

            brush = alt.selection_interval(encodings=['x'])

            base = alt.Chart(source).transform_calculate(
                time="hours(datum.date) + minutes(datum.date) / 60"
            ).mark_bar().encode(
                y='count():Q'
            ).properties(
                width=600,
                height=100
            )

            alt.vconcat(
              base.encode(
                alt.X('time:Q',
                  bin=alt.Bin(maxbins=30, extent=brush),
                  scale=alt.Scale(domain=brush)
                )
              ),
              base.encode(
                alt.X('time:Q', bin=alt.Bin(maxbins=30)),
              ).add_params(brush)
            )
