
:orphan:
:html_theme.sidebar_secondary.remove:

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

.. _gallery_bar_with_rolling_mean:


Bar Chart with Rolling Mean
---------------------------
A bar chart overlaid with a rolling mean. In this example the average of values over the previous decade is displayed as a line.


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


    import altair as alt
    from altair.datasets import data

    source = data.wheat()

    bar = alt.Chart(source).mark_bar().encode(
        x='year:O',
        y='wheat:Q'
    )

    line = alt.Chart(source).mark_line(color='red').transform_window(
        # The field to average
        rolling_mean='mean(wheat)',
        # The number of values before and after the current value to include.
        frame=[-9, 0]
    ).encode(
        x='year:O',
        y='rolling_mean:Q'
    )

    (bar + line).properties(width=600)
    # 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
            from altair.datasets import data

            source = data.wheat()

            bar = alt.Chart(source).mark_bar().encode(
                x='year:O',
                y='wheat:Q'
            )

            line = alt.Chart(source).mark_line(color='red').transform_window(
                # The field to average
                rolling_mean='mean(wheat)',
                # The number of values before and after the current value to include.
                frame=[-9, 0]
            ).encode(
                x='year:O',
                y='rolling_mean:Q'
            )

            (bar + line).properties(width=600)


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

        .. code:: python


            import altair as alt
            from altair.datasets import data

            source = data.wheat()

            bar = alt.Chart(source).mark_bar().encode(
                x='year:O',
                y='wheat:Q'
            )

            line = alt.Chart(source).mark_line(color='red').transform_window(
                # The field to average
                rolling_mean='mean(wheat)',
                # The number of values before and after the current value to include.
                frame=[-9, 0]
            ).encode(
                x='year:O',
                y='rolling_mean:Q'
            )

            (bar + line).properties(width=600)
            # No channel encoding options are specified in this chart
            # so the code is the same as for the method-based syntax.
